-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
executable file
·149 lines (114 loc) · 5.16 KB
/
run.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
"""Main file of the bot."""
import sys
import traceback
import discord
from discord.ext import commands
from discord.ext.commands.errors import (
BadArgument,
CommandInvokeError,
CommandNotFound,
MissingAnyRole,
MissingRequiredArgument,
NoPrivateMessage,
UserNotFound,
)
import configs
from bot import tasks, util
from bot.botcommands import BotConfigsCog, MemberCog, ModerationCog, ReactionRoleCog
from bot.interactions import TicketCloseInteraction, TicketOpeningInteraction
from bot.interactions.errors import NoReplyException
from bot.management import LoggingCog, StrikesCog, WelcomeCog
class AdeptClient(commands.Bot):
"""Main class of the bot."""
def __init__(self, prefix: str, intents: discord.Intents):
super().__init__(prefix, intents=intents, case_insensitive=True)
async def on_ready(self):
"""Called when the bot is ready."""
util.logger.info(
"\nLogged in with account @%s ID:%s \n------------------------------------", self.user.name, self.user.id
)
await self.change_presence(activity=discord.Activity(name="for bad boys!", type=discord.ActivityType.watching))
await tasks.load_tasks(self)
async def setup_hook(self) -> None:
# Register cogs
await self.add_cog(BotConfigsCog())
await self.add_cog(LoggingCog(self))
await self.add_cog(MemberCog())
await self.add_cog(ModerationCog())
await self.add_cog(StrikesCog())
await self.add_cog(WelcomeCog(self))
await self.add_cog(ReactionRoleCog(self))
# Register persistent views
self.add_view(TicketOpeningInteraction())
self.add_view(TicketCloseInteraction())
# Inject itself to the util module
util.load(self)
async def on_message(self, message: discord.Message, /):
if message.author.bot:
return
if message.content.startswith(f"<@!{self.user.id}>") or message.content.startswith(f"<@{self.user.id}>"):
message.content = message.content.replace(f"<@!{self.user.id}>", configs.PREFIX, 1)
message.content = message.content.replace(f"<@{self.user.id}>", configs.PREFIX, 1)
if message.content.startswith(configs.PREFIX):
await self.process_commands(message)
async def say(self, channel: discord.abc.Messageable | str, *args, **kwargs):
"""
Send a message to a channel.
Parameters
----------
- channel: The channel to send the message to.
- *args: The arguments to pass to the send method.
- **kwargs: The keyword arguments to pass to the send
method.
Returns
-------
The message sent.
"""
if isinstance(channel, str):
# channel_id/server_id
channel_id, server_id = channel.split("/")
channel = self.get_guild(int(server_id)).get_channel(int(channel_id))
try:
return await channel.send(*args, **kwargs)
except discord.Forbidden as send_error:
util.logger.warning(send_error)
async def on_error(self, _, *args):
ctx: commands.Context = args[0] if len(args) == 1 else None
error = sys.exc_info()[1]
if ctx:
await self.on_command_error(ctx, error) # Sketchy but works flawlessly (:
return
util.logger.error(error)
async def on_command_error(self, ctx: commands.Context, exception: commands.errors.CommandError, /) -> None:
if isinstance(exception, CommandInvokeError):
exception = exception.original
if isinstance(ctx, CommandNotFound):
# We don't care
return
if isinstance(exception, NoPrivateMessage):
await ctx.send("Cette commande ne peut pas être utilisée en message privé.")
elif isinstance(exception, UserNotFound):
await ctx.send("Utilisateur introuvable.")
elif isinstance(exception, MissingAnyRole):
await ctx.send("Vous n'avez pas la permission d'utiliser cette commande.")
elif isinstance(exception, MissingRequiredArgument):
await ctx.send(f"Argument manquant: {exception.param.name}")
elif isinstance(exception, BadArgument):
await ctx.send(f"Argument invalide: {exception.param.name}")
elif isinstance(exception, (NoReplyException)): # , InsufficientPermissionsError)):
await exception.channel.send(exception.message)
elif isinstance(exception, discord.Forbidden):
# Check if the error is about not being able to send a DM
if exception.code == 50007:
await ctx.reply(configs.WELCOME_CANT_DM)
elif isinstance(exception, util.AdeptBotException):
await ctx.send(exception.message)
else:
# Log any uncatched error in the logging channel
await self.say(configs.LOGS_CHANNEL, f"```py\n{traceback.format_exc()[-1500:]}```")
await super().on_command_error(ctx, exception)
if __name__ == "__main__":
util.logger.info("Starting the bot!")
all_intents = discord.Intents.all()
client = AdeptClient(configs.PREFIX, all_intents)
client.run(configs.TOKEN)