-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbot.py
340 lines (301 loc) · 12.1 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
"""
Copyright © Krypton 2019-2023 - https://github.com/kkrypt0nn (https://krypton.ninja)
Description:
🐍 A simple template to start to code your own and personalized discord bot in Python
programming language.
Version: 6.1.0
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an " AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
"""
import logging
import os
import platform
from dataclasses import dataclass
from typing import Any
import aiohttp
import discord
import topgg
from beanie.odm.operators.update.general import Set
from discord.ext import commands
from html2image import Html2Image
from constants import GLOBAL_LEADERBOARD_ID
from database.models import Profile, Server
from database.setup import initialise_mongodb_conn
from utils.dev import ChannelLogger, dev_commands
from utils.http_client import HttpClient
from utils.neetcode import NeetcodeSolutions
from utils.ratings import Ratings
from utils.schedules import (
schedule_prune_members_and_guilds,
schedule_question_and_stats_update,
schedule_update_neetcode_solutions,
schedule_update_zerotrac_ratings,
)
from utils.users import delete_user, unlink_user_from_server
@dataclass
class Config:
DISCORD_TOKEN: str
MONGODB_URI: str
TOPGG_TOKEN: str
BROWSER_EXECUTABLE_PATH: str
GOOGLE_APPLICATION_CREDENTIALS: str
LOGGING_CHANNEL_ID: int
DEVELOPER_DISCORD_ID: int
PRODUCTION: bool
class DiscordBot(commands.Bot):
def __init__(
self, intents: discord.Intents, config: Config, logger: logging.Logger
) -> None:
super().__init__(
command_prefix=",",
intents=intents,
help_command=None,
)
"""
Creates custom bot variables so that we can access these variables in cogs
more easily.
"""
self.tree.on_error = self.tree_on_error
self.config = config
self.logger = logger
self.html2image = Html2Image(browser_executable=config.BROWSER_EXECUTABLE_PATH)
self.channel_logger = ChannelLogger(self, self.config.LOGGING_CHANNEL_ID)
self.ratings = Ratings(self)
self.neetcode = NeetcodeSolutions(self)
self.http_client: HttpClient | None = None
self.topggpy: topgg.DBLClient | None = None
async def on_autopost_success(self) -> None:
"""
Runs when stats are posted to topgg
"""
self.logger.info(
f"Posted server count ({self.topggpy.guild_count}), shard count "
f"({self.shard_count})",
)
self.logger.info(
f"Total bot member count ({ len(set(self.get_all_members()))})"
)
async def init_topgg(self) -> None:
"""
Initialises the topgg client.
"""
if self.config.PRODUCTION:
self.topggpy = topgg.DBLClient(
self, self.config.TOPGG_TOKEN, autopost=True, post_shard_count=True
)
async def load_cogs(self) -> None:
"""
The code in this function is executed whenever the bot will start.
"""
for file in os.listdir(f"{os.path.realpath(os.path.dirname(__file__))}/cogs"):
if file.endswith(".py"):
extension = file[:-3]
try:
await self.load_extension(f"cogs.{extension}")
self.logger.info(f"Loaded extension '{extension}'")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
self.logger.error(
f"Failed to load extension {extension}\n{exception}"
)
async def on_ready(self) -> None:
"""
Called when the client is done preparing the data received from Discord.
"""
self.logger.info("Ready")
async def setup_hook(self) -> None:
"""
Called only once to setup the bot.
"""
self.logger.info(f"Logged in as {self.user.name}")
self.logger.info(f"discord.py API version: {discord.__version__}")
self.logger.info(f"Python version: {platform.python_version()}")
self.logger.info(
f"Running on: {platform.system()} {platform.release()} ({os.name})"
)
self.logger.info("-------------------")
self.http_client = HttpClient(self, aiohttp.ClientSession())
await initialise_mongodb_conn(self.config.MONGODB_URI, GLOBAL_LEADERBOARD_ID)
await self.load_cogs()
await self.init_topgg()
schedule_update_zerotrac_ratings.start(self)
schedule_update_neetcode_solutions.start(self)
schedule_question_and_stats_update.start(self)
schedule_prune_members_and_guilds.start(self)
async def on_interaction(self, interaction: discord.Interaction) -> None:
"""
The code in this event is executed every time a normal command has been
*successfully* executed.
"""
if not interaction.command:
return
full_command_name = interaction.command.qualified_name
split = full_command_name.split(" ")
executed_command = str(split[0])
if interaction.guild is not None:
self.logger.info(
f"Executed /{executed_command} command in {interaction.guild.name} "
f"(ID: {interaction.guild.id}) by {interaction.user.name} "
f"(ID: {interaction.user.id})"
)
else:
self.logger.info(
f"Executed /{executed_command} command by {interaction.user.name} "
f"(ID: {interaction.user.id}) in DMs"
)
async def on_guild_remove(self, guild: discord.Guild) -> None:
"""
Called when a guild gets deleted.
Delete the server and all it's profiles.
"""
self.logger.info(
f"Guild {guild.name} (ID: {guild.id}) discord account removed",
)
await Profile.find_many(Profile.server_id == guild.id).delete()
await Server.find_one(Server.id == guild.id).delete()
async def on_raw_member_remove(self, payload: discord.RawMemberRemoveEvent) -> None:
"""
Called when a member leaves a guild.
Delete the profile of the user.
"""
self.logger.info(
f"Member {payload.user.name} (ID: {payload.user.id}) in Guild "
f"(ID: {payload.guild_id}) discord account removed",
)
await unlink_user_from_server(payload.guild_id, payload.user.id)
profiles = await Profile.find_many(
Profile.user_id == payload.user.id,
Profile.server_id != GLOBAL_LEADERBOARD_ID,
).to_list()
if len(profiles) == 0:
await delete_user(payload.user.id)
async def on_member_update(
self, before: discord.Member, after: discord.Member
) -> None:
"""
Called when a member updates their guild specific information, such as nickname.
Update the user profile's preference name to the new nickname.
"""
if before.display_name == after.display_name:
return
await Profile.find_one(
Profile.user_id == before.id,
Profile.server_id == before.guild.id,
).update(Set({Profile.preference.name: after.display_name}))
async def on_user_update(self, before: discord.User, after: discord.User) -> None:
"""
Called a user updated their account information, such as username.
Update the user global profile's preference name to the new username.
"""
if before.display_name == after.display_name:
return
await Profile.find_one(
Profile.user_id == before.id,
Profile.server_id == GLOBAL_LEADERBOARD_ID,
).update(Set({Profile.preference.name: after.display_name}))
async def on_message(self, message: discord.Message) -> None:
"""
Called whenever a message is sent. However, due to not having messages intent,
only messages that mention the bot will give us access to the message's
contents.
"""
if not (
(message.author.id == self.config.DEVELOPER_DISCORD_ID)
and self.user.mentioned_in(message)
):
return
await dev_commands(self, message)
async def close(self) -> None:
"""
Closes the connection to Discord, gracefully closes the session, and reboots
the device.
"""
try:
await self.http_client.session.close()
await super().close()
finally:
if self.config.PRODUCTION:
os.system("sudo reboot")
async def on_error(self, event_method: str, /, *args: Any, **kwargs: Any) -> None:
"""
Event raised errors.
Currently this causes an outage, and therefore will log the error and restart
the bot.
"""
self.logger.critical(
"Critical error in %s.\nBot is closing/restarting.", event_method
)
await self.close()
@staticmethod
async def tree_on_error(
interaction: discord.Interaction, error: discord.app_commands.AppCommandError
) -> None:
"""
The code in this event is executed every time a normal valid command catches an
error.
"""
if isinstance(error, discord.app_commands.errors.CommandOnCooldown):
minutes, seconds = divmod(error.retry_after, 60)
hours, minutes = divmod(minutes, 60)
hours = hours % 24
embed = discord.Embed(
description=f"**Please slow down** - You can use this command again in "
f"{f'{round(hours)} hours' if round(hours) > 0 else ''}"
f"{f'{round(minutes)} minutes' if round(minutes) > 0 else ''}"
f"{f'{round(seconds)} seconds' if round(seconds) > 0 else ''}.",
colour=0xE02B2B,
)
await interaction.response.send_message(embed=embed)
elif isinstance(error, discord.app_commands.errors.MissingPermissions):
embed = discord.Embed(
description="You are missing the permission(s) `"
+ ", ".join(error.missing_permissions)
+ "` to execute this command!",
colour=0xE02B2B,
)
await interaction.response.send_message(embed=embed)
elif isinstance(error, discord.app_commands.errors.BotMissingPermissions):
embed = discord.Embed(
description="I am missing the permission(s) `"
+ ", ".join(error.missing_permissions)
+ "` to fully perform this command!",
colour=0xE02B2B,
)
await interaction.response.send_message(embed=embed)
else:
raise error
class LoggingFormatter(logging.Formatter):
# Colours
black = "\x1b[30m"
red = "\x1b[31m"
green = "\x1b[32m"
yellow = "\x1b[33m"
blue = "\x1b[34m"
gray = "\x1b[38m"
# Styles
reset = "\x1b[0m"
bold = "\x1b[1m"
COLOURS = {
logging.DEBUG: gray + bold,
logging.INFO: blue + bold,
logging.WARNING: yellow + bold,
logging.ERROR: red,
logging.CRITICAL: red + bold,
}
def format(self, record) -> None:
log_colour = self.COLOURS[record.levelno]
format = (
"(black){asctime}(reset) (levelcolour){levelname:<8}(reset) "
"(green){name}(reset) {message}"
)
format = format.replace("(black)", self.black + self.bold)
format = format.replace("(reset)", self.reset)
format = format.replace("(levelcolour)", log_colour)
format = format.replace("(green)", self.green + self.bold)
formatter = logging.Formatter(format, "%Y-%m-%d %H:%M:%S", style="{")
return formatter.format(record)