Advertisement
MarkiPro

Captcha Verification with Python (meant for discord bots)

Dec 13th, 2020 (edited)
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.78 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. import random
  4. from PIL import Image, ImageDraw, ImageFont
  5. import os
  6. import asyncio
  7. import datetime
  8. import string
  9.  
  10.  
  11. class Verification(commands.Cog):
  12.     def __init__(self, bot):
  13.         self.bot = bot
  14.  
  15.     @commands.command()
  16.     async def verify(self, ctx):
  17.         verified_role = ctx.guild.get_role(695328817157373992)
  18.         log_channel = ctx.guild.get_channel(745904248461590549)
  19.         verification_channel = ctx.guild.get_channel(745331129535561758)
  20.  
  21.         if ctx.author in verified_role.members:
  22.             return await ctx.send("You are already verified!")
  23.         else:
  24.             pass
  25.         if ctx.channel != verification_channel:
  26.             return await ctx.send(
  27.                 "Please go to verify in <@#745331129535561758>, and if you need assistance, you should first read over <@#713020247543906368>!")
  28.         else:
  29.             pass
  30.  
  31.         getit = lambda: (random.randrange(5, 295), random.randrange(5, 95))
  32.  
  33.         color = (200, 200, 200)
  34.         shadow_color = (0, 0, 0)
  35.  
  36.         line_color = (50, 50, 50)
  37.  
  38.         def random_string():
  39.             N = 8
  40.             s = string.ascii_uppercase + string.ascii_lowercase + string.digits
  41.             random_string = ''.join(random.choices(s, k=N))
  42.             return random_string
  43.  
  44.         captcha_str = random_string()
  45.         file_name = f"{ctx.message.id}.png"
  46.  
  47.         img = Image.new('RGB', (300, 100), color=(95, 104, 222))
  48.         draw = ImageDraw.Draw(img)
  49.  
  50.         text_color = color
  51.         font = "langar.ttf"
  52.         font_name = f'fonts/{font}'
  53.         font = ImageFont.truetype(font=font_name, size=50)
  54.         w, h = draw.textsize(captcha_str, font)
  55.         draw.text(((300 - w) / 2 + 5, (100 - h) / 2 + 5), captcha_str, fill=shadow_color, font=font)
  56.         draw.text(((300 - w) / 2, (100 - h) / 2), captcha_str, fill=text_color, font=font)
  57.  
  58.         for i in range(5, random.randrange(10, 20)):
  59.             start = (0, (100 - random.randrange(0, 101)))
  60.             end = (300, (100 - random.randrange(0, 101)))
  61.             draw.line((start, end), fill=shadow_color, width=2)
  62.  
  63.         for i in range(50, random.randrange(100, 300)):
  64.             draw.point((random.randrange(0, 300), random.randrange(0, 100)), fill=color)
  65.  
  66.         img.save(file_name)
  67.  
  68.         f = discord.File(file_name, filename=file_name)
  69.  
  70.         verif_embed = discord.Embed(
  71.             title="**Welcome to Content Creators**",
  72.             description="Please send the captcha code here, you have 5 minutes to do so.\n\nHello! You are required to complete a captcha before entering the server.\n\n*NOTE: This is **Case Sensitive***.\n\n**Why?**\nThis is to protect the server against targeted attacks using automated user accounts.\n\n**Your Captcha:**",
  73.             timestamp=datetime.datetime.utcnow(),
  74.             color=0x0064ff
  75.         )
  76.  
  77.         verif_embed.set_image(url=f"attachment://{file_name}")
  78.  
  79.         await ctx.author.send(embed=verif_embed, file=f)
  80.  
  81.         def check_dm(m):
  82.             if isinstance(m.channel, discord.DMChannel):
  83.                 if m.author == ctx.author:
  84.                     return True
  85.                 else:
  86.                     return False
  87.             else:
  88.                 return False
  89.  
  90.         try:
  91.             answer_message = await self.bot.wait_for('message', check=check_dm, timeout=300)
  92.             answer = answer_message.content
  93.             if answer == captcha_str:
  94.                 await ctx.author.send(
  95.                     "You did the captcha correctly! Good job! You now have the verified role and full access to the server.")
  96.                 log_embed2 = discord.Embed(
  97.                     title="**Verification Success**",
  98.                     description=f"Verification for {ctx.author.mention}({ctx.author})",
  99.                     color=0x00fa00,
  100.                     timestamp=datetime.datetime.utcnow()
  101.                 )
  102.                 log_embed2.add_field(name="Looking for", value=f"{captcha_str}", inline=True)
  103.                 log_embed2.add_field(name="Given", value=f"{answer}", inline=True)
  104.                 await ctx.author.add_roles(verified_role)
  105.                 await log_channel.send(embed=log_embed2)
  106.             elif answer != captcha_str:
  107.                 await ctx.author.send(
  108.                     "You failed the captcha, please run the `>verify` command again in <#745331129535561758> and try again.")
  109.                 log_embed3 = discord.Embed(
  110.                     title="**Verification Failed**",
  111.                     description=f"Verification for {ctx.author.mention}({ctx.author})",
  112.                     color=0xff0000,
  113.                     timestamp=datetime.datetime.utcnow()
  114.                 )
  115.                 log_embed3.add_field(name="Looking for", value=f"{captcha_str}", inline=True)
  116.                 log_embed3.add_field(name="Given", value=f"{answer}", inline=True)
  117.                 log_embed3.add_field(name="Reason", value=f"Sent the wrong code!", inline=True)
  118.                 await log_channel.send(embed=log_embed3)
  119.         except asyncio.TimeoutError:
  120.             await ctx.author.send(
  121.                 "You ran out of time, please run the `>verify` command again in <#745331129535561758> and try again.")
  122.             log_embed1 = discord.Embed(
  123.                 title="**Verification Failed**",
  124.                 description=f"Verification for {ctx.author.mention}({ctx.author})",
  125.                 color=0xff0000,
  126.                 timestamp=datetime.datetime.utcnow()
  127.             )
  128.             log_embed1.add_field(name="Looking for", value=f"{captcha_str}", inline=True)
  129.             log_embed1.add_field(name="Reason", value=f"Ran out of time!", inline=True)
  130.             await log_channel.send(embed=log_embed1)
  131.         os.remove(file_name)
  132.  
  133.  
  134. def setup(bot):
  135.     bot.add_cog(Verification(bot))
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement