-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot_captcha.py
93 lines (73 loc) · 3.32 KB
/
bot_captcha.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from decouple import config
import discord
from discord.ext import commands
from captcha.image import ImageCaptcha
import random
import io
import os
from PIL import Image
import asyncio
token = config("DISCORD_BOT_TOKEN")
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
captcha_generator = ImageCaptcha()
captchas = {}
@bot.event
async def on_ready():
print(f'Bot is ready as {bot.user}')
@bot.event
async def on_member_join(member):
await send_captcha(member)
async def send_captcha(member):
while True: # Continues until correct CAPTCHA or timeout
data = ''.join(random.choices('ABCDEFGHJKMNPQRSTUVWXYZ123456789', k=5))
captcha_image = captcha_generator.generate(data)
captcha_bytes = captcha_image.getvalue()
temp_file = f"captcha_{member.id}.png"
with open(temp_file, "wb") as f:
f.write(captcha_bytes)
captchas[member.id] = data
dm_channel = await member.create_dm()
image = Image.open(temp_file)
image.close()
file = discord.File(fp=temp_file, filename="captcha.png")
embed = discord.Embed(title="Verification", description="Solve the CAPTCHA to get verified!")
await dm_channel.send(embed=embed, file=file)
fail_count = 0
timeout = 180
start_time = asyncio.get_event_loop().time()
while (asyncio.get_event_loop().time() - start_time) < timeout:
try:
response = await bot.wait_for('message', check=lambda m: m.author == member and isinstance(m.channel, discord.DMChannel), timeout=timeout)
if response.content == captchas[member.id]:
role = discord.utils.get(member.guild.roles, name="Verified")
await member.add_roles(role)
await dm_channel.send(f"Welcome {member.mention}, you are now verified and have been given the 'Verified' role!")
del captchas[member.id]
return
else:
fail_count += 1
if fail_count < 3:
await dm_channel.send(f"Sorry {member.mention}, that's incorrect. You have {3 - fail_count} attempts left. A new captcha will be sent after your remaining attempts run out.")
else:
await dm_channel.send(f"Sorry {member.mention}, that's incorrect. Generating a new CAPTCHA for you...")
break # Exit the inner loop to generate a new CAPTCHA
except asyncio.TimeoutError:
await dm_channel.send(f"Verification timed out. Please request a new CAPTCHA if you wish to verify.")
return
os.remove(temp_file)
@bot.event
async def on_message(message):
if message.author.bot:
return
if message.guild is None:
return
if message.author.id in captchas:
if message.content == captchas[message.author.id]:
await message.author.send(f"Welcome {message.author.mention}, you are now verified!")
role = discord.utils.get(message.guild.roles, name="Verified")
await message.author.add_roles(role)
del captchas[message.author.id]
else:
await message.author.send(f"{message.author.mention}, that's incorrect. Please try again.")
bot.run(token)