-
Notifications
You must be signed in to change notification settings - Fork 33
/
bot.py
341 lines (306 loc) · 14 KB
/
bot.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import discord
from discord.ext import commands
import sqlite3
import re
import os
import random
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
import requests
from keep_alive import keep_alive
conn = sqlite3.connect('bot.db')
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS users(
userid INT,
guildid INT,
email TEXT,
code INT,
verified INT);
""")
c.execute("""CREATE TABLE IF NOT EXISTS guilds(
guildid INT PRIMARY KEY,
domains TEXT,
onjoin INT,
role TEXT);
""")
conn.commit()
def get_guild(guildid):
return c.execute("SELECT * FROM guilds WHERE guildid=?", (guildid,)).fetchone()
def new_guild(guildid, domains="", onjoin=0, role="Verified"):
c.execute("INSERT INTO guilds VALUES (?, ?, ?, ?)", (guildid, domains, onjoin, role))
conn.commit()
def get_user_guild(guildid, userid):
return c.execute("SELECT * FROM users WHERE guildid=? AND userid=?", (guildid, userid)).fetchone()
def get_users_guilds(userid):
return c.execute("SELECT * FROM users WHERE userid=?", (userid,)).fetchall()
def get_emails_guilds(guildid, email):
return c.execute("SELECT * FROM users WHERE guildid=? AND email=? AND verified=1", (guildid, email)).fetchall()
def get_users_codes(userid, code):
return c.execute("SELECT * FROM users WHERE userid=? AND code=?", (userid, code)).fetchall()
def verify_msg(guildname, domains):
if domains == "":
return "{} hasn't setup this bot yet.".format(guildname)
else:
return "To verify yourself on {}, **reply here with your @{} email address**.".format(guildname, domains)
def new_user(userid, guildid, email="", code=0, verified=0):
c.execute("INSERT INTO users VALUES (?, ?, ?, ?, ?)", (userid, guildid, email, code, verified))
conn.commit()
def verify_user(userid, guildid):
c.execute("UPDATE users SET verified=1 WHERE userid=? AND guildid=?", (userid, guildid))
conn.commit()
def get_domains(guildid):
return get_guild(guildid)[1]
def add_domain(domain, guildid):
d_get = get_domains(guildid)
prevdomains = []
if d_get != "":
prevdomains = get_domains(guildid).split('|')
if domain not in prevdomains:
prevdomains.append(domain)
c.execute("UPDATE guilds SET domains=? WHERE guildid=?", ('|'.join(prevdomains), guildid))
conn.commit()
def remove_domain(domain, guildid):
prevdomains = get_domains(guildid).split('|')
if domain in prevdomains:
prevdomains.remove(domain)
c.execute("UPDATE guilds SET domains=? WHERE guildid=?", ('|'.join(prevdomains), guildid))
conn.commit()
def change_role(role, guildid):
c.execute("UPDATE guilds SET role=? WHERE guildid=?", (role, guildid))
conn.commit()
def enable_onjoin(guildid):
c.execute("UPDATE guilds SET onjoin=? WHERE guildid=?", (1, guildid))
conn.commit()
def disable_onjoin(guildid):
c.execute("UPDATE guilds SET onjoin=? WHERE guildid=?", (0, guildid))
conn.commit()
def insert_code(code, userid, guildid):
c.execute("UPDATE users SET code=? WHERE userid=? AND guildid=?", (code, userid, guildid))
conn.commit()
def insert_email(email, userid, guildid):
c.execute("UPDATE users SET email=? WHERE userid=? AND guildid=?", (email, userid, guildid))
conn.commit()
def email_check(email):
regex = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"
if re.search(regex, email):
return True
else:
return False
def mailgun_send(email_address, verification_code):
return requests.post(
"https://api.mailgun.net/v3/{}/messages".format(os.environ.get('MAILGUN_DOMAIN')),
auth=("api", os.environ.get('MAILGUN_API_KEY')),
data={"from": "EmailBot <mailgun@{}>".format(os.environ.get('MAILGUN_DOMAIN')),
"to": email_address,
"subject": "Verify your server email",
"text": str(verification_code)})
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '.', intents=intents)
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
await client.change_presence(activity=discord.Game(name='.vstatus | github.com/gg2001/EmailBot'))
@client.event
async def on_member_join(member):
check_on_join = get_guild(member.guild.id)
if check_on_join == None:
new_guild(member.guild.id)
elif check_on_join[2] == 1:
user_prev_verify = get_user_guild(member.guild.id, member.id)
if user_prev_verify == None:
await member.send(verify_msg(member.guild, check_on_join[1]))
new_user(member.id, member.guild.id)
elif user_prev_verify[4] == 0:
await member.send(verify_msg(member.guild, check_on_join[1]))
elif user_prev_verify[4] == 1:
role = discord.utils.get(member.guild.roles, name=check_on_join[3])
if role:
if role not in member.roles:
await member.add_roles(role)
else:
await member.guild.create_role(name=check_on_join[3])
role = discord.utils.get(member.guild.roles, name=check_on_join[3])
await member.add_roles(role)
@client.event
async def on_message(message):
if message.author == client.user:
return
message_content = message.content.strip()
if (message.guild == None) and email_check(message_content):
users_guilds = get_users_guilds(message.author.id)
if len(users_guilds) >= 1:
guild_list = [i[1] for i in users_guilds if i[4] == 0]
verif_list = []
for i in guild_list:
email_guild = get_emails_guilds(i, message_content)
if len(email_guild) > 0:
continue
guild_domains = get_domains(i)
if len(guild_domains) == 0:
continue
guild_domains = guild_domains.split('|')
if message_content.split("@")[1] in guild_domains:
verif_list.append(i)
if len(verif_list) >= 1:
random_code = random.randint(100000, 999999)
for i in verif_list:
insert_code(random_code, message.author.id, i)
insert_email(message_content, message.author.id, i)
emailmessage = Mail(
from_email=os.environ.get('SENDGRID_EMAIL'),
to_emails=message_content,
subject='Verify your server email',
html_content=str(random_code))
try:
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(emailmessage)
print(response.status_code)
print(response.body)
print(response.headers)
await message.channel.send("Email sent. **Reply here with your verification code**. If you haven't received it, check your spam folder.")
except Exception as e:
mailgun_email = mailgun_send(message_content, random_code)
if mailgun_email.status_code == 200:
await message.channel.send("Email sent. **Reply here with your verification code**. If you haven't received it, check your spam folder.")
else:
await message.channel.send("Email failed to send.")
else:
await message.channel.send("Invalid email.")
else:
await message.channel.send("You have not joined a server.")
elif (len(message_content) == 6) and message_content.isdigit():
verif_code = int(message_content)
prev_codes_f = get_users_codes(message.author.id, verif_code)
prev_codes_g = [i for i in prev_codes_f if i[4] == 0]
prev_codes = []
for i in prev_codes_g:
user_emails = get_emails_guilds(i[1], i[2])
if len(user_emails) >= 1:
continue
else:
prev_codes.append(i)
if len(prev_codes) >= 1:
for i in prev_codes:
verify_user(message.author.id, i[1])
curr_guild = client.get_guild(i[1])
guild_db = get_guild(i[1])
role = discord.utils.get(curr_guild.roles, name=guild_db[3])
if role:
member = curr_guild.get_member(message.author.id)
if role not in member.roles:
await member.add_roles(role)
else:
await curr_guild.create_role(name=guild_db[3])
role = discord.utils.get(curr_guild.roles, name=guild_db[3])
member = curr_guild.get_member(message.author.id)
await member.add_roles(role)
await message.channel.send("You have been verified on " + client.get_guild(i[1]).name + ".")
else:
await message.channel.send("Incorrect code.")
elif message.guild == None:
await message.channel.send("Invalid email.")
await client.process_commands(message)
@client.event
async def on_guild_join(guild):
check_on_join = get_guild(guild.id)
if check_on_join == None:
new_guild(guild.id)
@client.command()
async def rolechange(ctx, role=None):
if role and ctx.guild and ctx.author.guild_permissions.administrator:
role = role.strip()
check_on_join = get_guild(ctx.guild.id)
if check_on_join == None:
new_guild(ctx.guild.id)
check_on_join = get_guild(ctx.guild.id)
role_d = discord.utils.get(ctx.guild.roles, name=check_on_join[3])
if role_d:
await role_d.edit(name=role)
change_role(role, ctx.guild.id)
await ctx.send("```Verified role: " + get_guild(ctx.guild.id)[3] + "```")
else:
role_d2 = discord.utils.get(ctx.guild.roles, name=role)
if role_d2:
change_role(role, ctx.guild.id)
await ctx.send("```Verified role: " + get_guild(ctx.guild.id)[3] + ".```")
else:
await ctx.guild.create_role(name=role)
change_role(role, ctx.guild.id)
await ctx.send("```Verified role: " + get_guild(ctx.guild.id)[3] + ".```")
@client.command()
async def domainadd(ctx, domain=None):
if domain and ctx.guild and ctx.author.guild_permissions.administrator:
domain = domain.strip()
check_on_join = get_guild(ctx.guild.id)
if check_on_join == None:
new_guild(ctx.guild.id)
add_domain(domain, ctx.guild.id)
await ctx.send("```Current email domains: " + get_domains(ctx.guild.id) + "```")
@client.command()
async def domainremove(ctx, domain=None):
if domain and ctx.guild and ctx.author.guild_permissions.administrator:
domain = domain.strip()
check_on_join = get_guild(ctx.guild.id)
if check_on_join == None:
new_guild(ctx.guild.id)
remove_domain(domain, ctx.guild.id)
await ctx.send("```Current email domains: " + get_domains(ctx.guild.id) + "```")
@client.command()
async def enableonjoin(ctx):
if ctx.guild and ctx.author.guild_permissions.administrator:
check_on_join = get_guild(ctx.guild.id)
if check_on_join == None:
new_guild(ctx.guild.id)
enable_onjoin(ctx.guild.id)
await ctx.send("```Verify when a user joins? True```")
@client.command()
async def disableonjoin(ctx):
if ctx.guild and ctx.author.guild_permissions.administrator:
check_on_join = get_guild(ctx.guild.id)
if check_on_join == None:
new_guild(ctx.guild.id)
disable_onjoin(ctx.guild.id)
await ctx.send("```Verify when a user joins? False```")
@client.command()
async def vstatus(ctx):
if ctx.guild:
check_on_join = get_guild(ctx.guild.id)
if check_on_join == None:
new_guild(ctx.guild.id)
check_on_join = get_guild(ctx.guild.id)
on_join = bool(check_on_join[2])
await ctx.send("```" +
"Ping: " + "{0}ms".format(round(client.latency * 1000)) + "\n" +
"User commands: " + "\n" +
" .verify -> Sends a DM to the user to verify their email" + "\n" +
" .vstatus -> This help message" + "\n\n" +
"Admin commands: " + "\n" +
" - A domain must be added before users can be verified." + "\n" +
" - Use .rolechange instead of server settings to change the name of the verified role." + "\n" +
" .enableonjoin -> Enables verifying users on join" + "\n" +
" .disableonjoin -> Disables verifying users on join" + "\n" +
" .domainadd domain -> Adds an email domain" + "\n" +
" .domainremove domain -> Removes an email domain" + "\n" +
" .rolechange role -> Changes the name of the verified role" + "\n\n" +
"Domains: " + check_on_join[1] + "\n" +
"Verify when a user joins? (default=False): " + str(on_join) + "\n" +
"Verified role (default=Verified): " + check_on_join[3] + "```")
@client.command()
async def vping(ctx):
await ctx.send("{0}ms".format(round(client.latency * 1000)))
@client.command()
async def verify(ctx):
if ctx.guild:
check_on_join = get_guild(ctx.guild.id)
if check_on_join == None:
new_guild(ctx.guild.id)
check_on_join = get_guild(ctx.guild.id)
user_prev_verify = get_user_guild(ctx.guild.id, ctx.author.id)
if user_prev_verify == None:
new_user(ctx.author.id, ctx.guild.id)
await ctx.author.send(verify_msg(ctx.guild, check_on_join[1]))
elif user_prev_verify[4] == 0:
await ctx.author.send(verify_msg(ctx.guild, check_on_join[1]))
keep_alive()
client.run(os.environ.get('DISCORD_TOKEN'))