-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathverification.py
249 lines (213 loc) · 8.47 KB
/
verification.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import discord
from discord.ext import commands
import asyncio
from captcha.image import ImageCaptcha
import string
import random
import os
from core import checks
from core.models import PermissionLevel
class CaptchaVerification(commands.Cog):
"""Add captcha verification to your server!"""
def __init__(self, bot):
self.bot = bot
self.db = bot.plugin_db.get_partition(self)
self.role = dict()
self.length = int()
self.captchas = dict()
self.casesensitive = bool()
asyncio.create_task(self._set_val())
self.true = ["t", "true", "yes", "y", "on"]
self.false = ["f", "false", "no", "n", "off"]
async def _update_db(self):
await self.db.find_one_and_update(
{"_id": "config"},
{
"$set": {
"roles": self.role,
"length": self.length,
"case-sensitive": self.casesensitive,
}
},
upsert=True,
)
await self.db.find_one_and_update(
{"_id": "captchas"},
{"$set": {"current_captchas": self.captchas}},
upsert=True,
)
async def _set_val(self):
config = await self.db.find_one({"_id": "config"})
captchas = await self.db.find_one({"_id": "captchas"})
if config is None:
await self.db.find_one_and_update(
{"_id": "config"},
{"$set": {"roles": dict(), "length": 7, "case-sensitive": False,}},
upsert=True,
)
config = await self.db.find_one({"_id": "config"})
if captchas is None:
await self.db.find_one_and_update(
{"_id": "captchas"}, {"$set": {"current_captchas": dict()}}, upsert=True
)
captchas = await self.db.find_one({"_id": "captchas"})
self.role = config.get("roles", dict())
self.length = config.get("length", 7)
self.casesensitive = config.get("case-sensitive", False)
self.captchas = captchas["current_captchas"]
@commands.command()
async def captcha(self, ctx, code=None):
def replace_similar(text):
return text.replace("0", "o")
if str(ctx.guild.id) in self.role:
if code is None:
code = replace_similar(
"".join(
random.choices(
string.ascii_letters + string.digits, k=self.length
)
)
)
image = ImageCaptcha()
image.write(
code, os.path.join(os.path.dirname(__file__), "captcha.png")
)
embed = discord.Embed(
colour=self.bot.main_color,
title="Your captcha, good sir (or ma'am).",
)
embed.set_footer(
text=f"Use {self.bot.prefix}captcha <code> in a channel (not this DM) to solve it."
)
embed.set_image(url="attachment://captcha.png")
file = discord.File(
os.path.join(os.path.dirname(__file__), "captcha.png"),
filename="captcha.png",
)
await ctx.author.send(file=file, embed=embed)
await ctx.send(
f"{ctx.author.mention}, sent you a DM containing the CAPTCHA!"
)
self.captchas[str(ctx.author.id)] = code
elif str(ctx.author.id) in self.captchas:
if replace_similar(code) == self.captchas[str(ctx.author.id)]:
solved = True
elif (
not self.casesensitive
and replace_similar(code.lower())
== self.captchas[str(ctx.author.id)].lower()
):
solved = True
else:
await ctx.send(
embed=discord.Embed(
title=f"That is incorrect. Please try again. Use `{self.bot.prefix}captcha`",
description=f"It is {None if self.casesensitive else 'not '}case-sensitive.",
colour=self.bot.error_color,
)
)
solved = False
if solved:
try:
role = discord.utils.get(
ctx.guild.roles, id=int(self.role[str(ctx.guild.id)])
)
await ctx.author.add_roles(role)
await ctx.send(f"You got the role: `{role.name}`")
except discord.Forbidden:
await ctx.send(
"I don't have the permissions to give you the role."
)
except:
await ctx.send("I couldn't give you the role.")
self.captchas.pop(str(ctx.author.id))
else:
await ctx.send(
embed=discord.Embed(
colour=self.bot.error_color,
title="You are not doing a captcha.",
)
)
else:
await ctx.send(
embed=discord.Embed(
colour=self.bot.error_color, title="No role set for doing captcha."
)
)
@checks.has_permissions(PermissionLevel.ADMIN)
@commands.group(invoke_without_command=True)
async def captchaconfig(self, ctx):
"""
Config your captcha!
To view your settings, use [p]captchaconfig.
To edit, use [p]captchaconfig <thingyouwanttoedit> <newvalue>
"""
role = str(self.role.get(str(ctx.guild.id), "No role specified."))
embed = discord.Embed(colour=self.bot.main_color)
embed.set_author(
name="Captcha Configurations:", icon_url=self.bot.user.avatar_url
)
embed.add_field(name="Role", value=f"`{role}`", inline=False)
embed.add_field(name="Code Length", value=f"`{self.length}`", inline=False)
embed.add_field(
name="Case Sensitive", value=f"`{self.casesensitive}`", inline=False
)
embed.set_footer(
text=f"To change use {self.bot.prefix}captchaconfig <thing> <value>. Use {self.bot.prefix}help captchaconfig for the list of things you want to change."
)
await ctx.send(embed=embed)
@checks.has_permissions(PermissionLevel.ADMIN)
@captchaconfig.command()
async def role(self, ctx, role: discord.Role):
"""
Set the role you get when you complete the captcha.
**Usage**:
[p]captchaconfig role @Done
[p]captchaconfig role 682773117680091148
[p]captchaconfig role Verified Member
"""
self.role[str(ctx.guild.id)] = role.id
await self._update_db()
await ctx.send("Ok.")
@checks.has_permissions(PermissionLevel.ADMIN)
@captchaconfig.command()
async def length(self, ctx, length=7):
"""
Set the length of the randomly generated code.
**Usage**:
[p]captchaconfig length 2
[p]captchaconfig length 15
It has to be between 1 and 20
"""
if length > 0 and length < 21:
self.length = length
await self._update_db()
await ctx.send("Ok.")
else:
await ctx.send("Too big or too small. 1-20 please.")
@checks.has_permissions(PermissionLevel.ADMIN)
@captchaconfig.command(aliases=["cs"])
async def casesensitive(self, ctx, trueorfalse):
"""
Set if codes are case sensitive.
**Usage**:
[p]captchaconfig cs true
[p]captchaconfig casesensitive fAlse
**True**:
t, true, y, yes, on
**False**:
f, false, n, no, off
True or falses are not case sensitive.
"""
if trueorfalse.lower() in self.true:
self.casesensitive = True
await self._update_db()
await ctx.send(f"Case sensitive is now set to `{self.casesensitive}`")
elif trueorfalse.lower() in self.false:
self.casesensitive = False
await self._update_db()
await ctx.send(f"Case sensitive is now set to `{self.casesensitive}`")
else:
await ctx.send("I don't understand.")
async def setup(bot):
await bot.add_cog(CaptchaVerification(bot))