-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
423 lines (342 loc) · 13.1 KB
/
client.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import json
import os
import platform
import random
import sys
import discord
from discord.ext import tasks, commands
from discord.ext.commands import Bot
from discord.ext.commands import Context
import asyncio
from validate_email import validate_email
from helpers.checks import reaction_check, is_author
from helpers.embed import custom_embed
from helpers.requests import email_in_endpoint
from mailing.api import send_code
if not os.path.isfile("credentials.json"):
sys.exit("'credentials.json' not found! Please add it and try again.")
else:
with open("credentials.json") as file:
credentials = json.load(file)
if not os.path.isfile("config.json"):
sys.exit("'config.json' not found! Please add it and try again.")
else:
with open("config.json") as file:
config = json.load(file)
intents = discord.Intents.default()
intents.members = True
intents.reactions = True
intents.message_content = True
client = Bot(command_prefix=config["prefix"], intents=intents)
# Removes the default help command of discord.py to be able to create our custom help command.
client.remove_command("help")
@client.event
async def on_ready() -> None:
"""
The code in this even is executed when the client is ready
"""
print(f"Logged in as {client.user.name}")
print(f"Discord API version: {discord.__version__}")
print(f"Python version: {platform.python_version()}")
print(f"Running on: {platform.system()} {platform.release()} ({os.name})")
print("-------------------")
status_task.start()
# Assign unchecked role to the members who joined while the bot was offline
for guild in client.guilds:
uncheckedRole = discord.utils.get(guild.roles, name=config["uncheckedRoleName"])
checkedRole = discord.utils.get(guild.roles, name=config["checkedRoleName"])
if not uncheckedRole or not checkedRole:
continue
for member in guild.members:
if uncheckedRole not in member.roles and checkedRole not in member.roles and member != client.user:
await member.add_roles(uncheckedRole)
@tasks.loop(minutes=1.0)
async def status_task() -> None:
"""
Setup the game status task of the client
"""
statuses = ["YOU SHALL NOT PASS!"]
await client.change_presence(activity=discord.Game(random.choice(statuses)))
async def load_commands(command_type: str) -> None:
for file in os.listdir(f"./cogs/{command_type}"):
if file.endswith(".py"):
extension = file[:-3]
try:
await client.load_extension(f"cogs.{command_type}.{extension}")
print(f"Loaded extension '{extension}'")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
print(f"Failed to load extension {extension}\n{exception}")
@client.event
async def on_message(message: discord.Message) -> None:
"""
The code in this event is executed every time someone sends a message, with or without the prefix
:param message: The message that was sent.
"""
if message.author == client.user or message.author.bot:
return
await client.process_commands(message)
@client.event
async def on_command_error(ctx: Context, error) -> None:
"""
The code in this event is executed every time a normal valid command catches an error
:param ctx: The normal command that failed executing.
:param error: The error that has been faced.
"""
if isinstance(error, commands.CommandOnCooldown):
minutes, seconds = divmod(error.retry_after, 60)
hours, minutes = divmod(minutes, 60)
hours = hours % 24
embed = discord.Embed(
title="Hey, please slow down!",
description=f"You can use this command again in {f'{round(hours)} hours' if round(hours) > 0 else ''} {f'{round(minutes)} minutes' if round(minutes) > 0 else ''} {f'{round(seconds)} seconds' if round(seconds) > 0 else ''}.",
color=0xE02B2B,
)
await ctx.send(embed=embed)
elif isinstance(error, commands.MissingPermissions):
embed = discord.Embed(
title="Error!",
description="You are missing the permission(s) `"
+ ", ".join(error.missing_permissions)
+ "` to execute this command!",
color=0xE02B2B,
)
await ctx.send(embed=embed)
elif isinstance(error, commands.MissingRequiredArgument):
embed = discord.Embed(
title="Error!",
description=str(error).capitalize(),
# We need to capitalize because the command arguments have no capital letter in the code.
color=0xE02B2B,
)
await ctx.send(embed=embed)
raise error
@client.event
async def on_guild_join(guild) -> None:
client.dispatch("setup", guild)
@client.event
async def on_member_join(member) -> None:
uncheckedRole = discord.utils.get(member.guild.roles, name=config["uncheckedRoleName"])
if not uncheckedRole:
return
await member.add_roles(uncheckedRole)
@client.event
async def on_change_state(state) -> None:
"""
The code in this event is executed every time the on_change_state event is called
"""
with open("state.json", "w") as file:
if state.lower() == "disable":
json.dump({"isEnabled": False}, file)
elif state.lower() == "enable":
json.dump({"isEnabled": True}, file)
@client.event
async def on_setup(guild) -> bool:
"""
The code in this event is executed every time the on_setup event is called
"""
uncheckedRole = discord.utils.get(guild.roles, name=config["uncheckedRoleName"])
checkedRole = discord.utils.get(guild.roles, name=config["checkedRoleName"])
participantRole = discord.utils.get(guild.roles, name=config["participantRoleName"])
checkChannel = discord.utils.get(guild.channels, name=config["checkChannelName"])
if not uncheckedRole:
uncheckedRole = await guild.create_role(name=config["uncheckedRoleName"])
if not checkedRole:
checkedRole = await guild.create_role(name=config["checkedRoleName"])
if not participantRole:
participantRole = await guild.create_role(name=config["participantRoleName"])
for role in guild.roles:
# Remove all perms for default role
if role.name == "@everyone":
perms = discord.Permissions()
perms.update(
view_channel=False,
)
await role.edit(reason=None, permissions=perms)
# Give all perms to checked role
if role.id == checkedRole.id:
perms = discord.Permissions()
perms.update(
read_messages=True,
read_message_history=True,
connect=True,
speak=True,
send_messages=True,
change_nickname=False,
view_channel=True,
)
await role.edit(reason=None, permissions=perms)
# Only allow access to check channel for unchecked role
elif role.id == uncheckedRole.id:
perms = discord.Permissions()
perms.update(
read_messages=True,
read_message_history=True,
connect=False,
speak=False,
send_messages=False,
change_nickname=False,
view_channel=False,
)
await role.edit(reason=None, permissions=perms)
if checkChannel:
await checkChannel.delete()
@client.event
async def on_raw_reaction_add(payload):
"""
The code in this event is executed every time the on_raw_reaction_add event is called
"""
if payload.member == client.user:
return
channel_payload = client.get_channel(payload.channel_id)
channel_check = discord.utils.get(channel_payload.guild.channels, name=config["checkChannelName"])
if channel_payload.id == channel_check.id and str(payload.emoji) == "✅":
message = await channel_payload.fetch_message(payload.message_id)
await message.remove_reaction("✅", payload.member)
client.dispatch("check_started", payload)
@client.event
async def on_check_started(payload):
"""
The code in this event is executed every time the on_check_started event is called
"""
member = payload.member
guild = client.get_guild(payload.guild_id)
# Ask for user's email
embed = discord.Embed(
title="checkmate | Check Process 1/2",
description=config["checkProcessAskEmailMessage"],
color=0xF6E6CC,
)
await member.send(embed=embed)
try:
email = await client.wait_for("message", check=is_author(member), timeout=60 * 10)
except asyncio.TimeoutError:
await custom_embed(
config["checkProcessTimeOutErrorMessage"],
member,
False,
)
# Check if the email is valid
try:
isValid = validate_email(email.content)
except:
isValid = False
if isValid:
# Check if the user has an account on the website
userRoles = email_in_endpoint(config["userInDbEndpoint"], email.content)
if userRoles:
pass
else:
await custom_embed(
config["checkProcessAccountErrorMessage"],
member,
False,
)
return
# Ask the user the verification code they received
embed = discord.Embed(
title=f"checkmate | Check Process 2/2",
description=config["checkProcessAskCodeMessage"],
color=0xF6E6CC,
)
await member.send(embed=embed)
# Send a verification code to the user
realCode = send_code(email.content, guild.name, member.name, config["SENDGRID_KEY"])
try:
userCode = await client.wait_for("message", check=is_author(member), timeout=60 * 10)
except asyncio.TimeoutError:
await custom_embed(
config["checkProcessTimeOutErrorMessage"],
member,
False,
)
# Check if the generated code and the code entered by theuser are the same
if realCode == userCode.content:
client.dispatch("check_completed", guild, member, email, userRoles)
# Throw an error if the code is not valid
else:
await custom_embed(
config["checkProcessCodeErrorMessage"],
member,
False,
)
# Throw an error if the email is not valid
else:
await custom_embed(
config["checkProcessEmailErrorMessage"],
member,
False,
)
@client.event
async def on_check_completed(guild, member, email, userRoles) -> None:
"""
The code in this event is executed every time the on_check_completed event is called
"""
uncheckedRole = discord.utils.get(guild.roles, name=config["uncheckedRoleName"])
checkedRole = discord.utils.get(guild.roles, name=config["checkedRoleName"])
# Give checked role to the user
if checkedRole:
await member.add_roles(checkedRole)
else:
await custom_embed(
config["basicErrorMessage"],
member,
False,
)
return
# Remove unchecked role from the user
if uncheckedRole:
await member.remove_roles(uncheckedRole)
else:
await custom_embed(
config["basicErrorMessage"],
member,
False,
)
return
for roleName in userRoles:
role = discord.utils.get(guild.roles, name=roleName)
if not role:
role = await guild.create_role(name=roleName)
await member.add_roles(role)
await custom_embed(
config["checkProcessCompletedMessage"],
member,
True,
)
embed = discord.Embed(
title="",
description=config["checkProcessAskAttendanceMessage"],
color=0xF6E6CC,
)
msg = await member.send(embed=embed)
await msg.add_reaction("✅")
await msg.add_reaction("❌")
confirmation = await client.wait_for("reaction_add", check=reaction_check(client, msg.id))
if confirmation[0].emoji == "✅":
participantRole = discord.utils.get(member.guild.roles, name=config["participantRoleName"])
if not participantRole:
return
await member.add_roles(participantRole)
await custom_embed(
"You were added the " + config["participantRoleName"] + " role!",
member,
True,
)
else:
await custom_embed(
config["checkProcessNotAttendingMessage"],
member,
False,
)
await msg.delete()
async def main() -> None:
async with client:
await load_commands("normal")
await client.start(credentials["token"])
if __name__ == "__main__":
"""
This will automatically load slash commands and normal commands located in their respective folder.
If you want to remove slash commands, which is not recommended due to the Message Intent being a privileged intent, you can remove the loading of slash command.
"""
asyncio.run(main())