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 | class Events:
"""
Collection of all registrable event handlers
For a more detailed documentation on these events, please refer to
https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#event-reference
"""
@staticmethod
async def on_ready(_: Bot) -> None:
await call_event_handlers("ready")
@staticmethod
async def on_typing(_: Bot, channel: Messageable, user: User | Member, when: datetime) -> None:
await call_event_handlers("typing", channel, user, when, identifier=user.id)
@staticmethod
async def on_message(bot: Bot, message: Message) -> None:
if message.author == bot.user:
await call_event_handlers("self_message", message, identifier=message.id)
return
if not await call_event_handlers("message", message, identifier=message.id):
return
# detect whether the message contains just a mention of the bot
# and call the bot_ping event
if match := re.match(r"^<@[&!]?(\d+)>$", message.content.strip()):
mentions = {cast(ClientUser, bot.user).id}
# find managed role of this bot
if message.guild is not None:
for role in message.guild.me.roles: # type: Role
if role.managed:
mentions.add(role.id)
# call bot_ping if bot has been mentioned
if int(match.group(1)) in mentions:
await call_event_handlers("bot_ping", message, identifier=message.id)
return
await bot.process_commands(message)
@staticmethod
async def on_message_delete(bot: Bot, message: Message) -> None:
await call_event_handlers("message_delete", message, identifier=message.id)
# delete bot responses if message contained a command
await handle_delete(bot, message.channel.id, message.id)
@staticmethod
async def on_raw_message_delete(bot: Bot, event: RawMessageDeleteEvent) -> None:
if event.cached_message is not None:
return
await call_event_handlers("raw_message_delete", event, identifier=event.message_id)
# delete bot responses if message contained a command
await handle_delete(bot, event.channel_id, event.message_id)
@staticmethod
async def on_message_edit(bot: Bot, before: Message, after: Message) -> None:
if before.content != after.content:
await handle_edit(bot, after)
await call_event_handlers("message_edit", before, after, identifier=after.id)
if before.content != after.content and not after.author.bot:
await bot.process_commands(after)
@staticmethod
async def on_raw_message_edit(bot: Bot, event: RawMessageUpdateEvent) -> None:
if event.cached_message is not None:
return
prepared = []
async def prepare() -> tuple[Messageable, Message] | None:
"""Extract channel and message from event"""
channel = cast(Messageable | None, bot.get_channel(event.channel_id))
if channel is None:
return None
try:
message: Message = await channel.fetch_message(event.message_id)
except NotFound:
return None
# delete bot responses if old message contained a command
await handle_edit(bot, message)
prepared.append(message)
return channel, message
await call_event_handlers("raw_message_edit", identifier=event.message_id, prepare=prepare)
if not prepared:
return
msg = prepared.pop()
if not msg.author.bot:
# execute command if new message contains one
await bot.process_commands(msg)
@staticmethod
async def on_raw_reaction_add(bot: Bot, event: RawReactionActionEvent) -> None:
async def prepare() -> ReactionEventData | None:
return await extract_from_raw_reaction_event(bot, event)
await call_event_handlers("raw_reaction_add", identifier=event.message_id, prepare=prepare)
@staticmethod
async def on_raw_reaction_remove(bot: Bot, event: RawReactionActionEvent) -> None:
async def prepare() -> ReactionEventData | None:
return await extract_from_raw_reaction_event(bot, event)
await call_event_handlers("raw_reaction_remove", identifier=event.message_id, prepare=prepare)
@staticmethod
async def on_raw_reaction_clear(bot: Bot, event: RawReactionClearEvent) -> None:
async def prepare() -> tuple[Message] | None:
"""Extract message from event."""
channel = cast(Messageable | None, bot.get_channel(event.channel_id))
if channel is None:
return None
try:
return (await channel.fetch_message(event.message_id),)
except NotFound:
return None
await call_event_handlers("raw_reaction_clear", identifier=event.message_id, prepare=prepare)
@staticmethod
async def on_raw_reaction_clear_emoji(bot: Bot, event: RawReactionClearEmojiEvent) -> None:
async def prepare() -> tuple[Message, PartialEmoji] | None:
"""Extract message and emoji from event."""
channel = cast(Messageable | None, bot.get_channel(event.channel_id))
if channel is None:
return None
try:
return await channel.fetch_message(event.message_id), event.emoji
except NotFound:
return None
await call_event_handlers("raw_reaction_clear_emoji", identifier=event.message_id, prepare=prepare)
@staticmethod
async def on_member_join(_: Bot, member: Member) -> None:
await call_event_handlers("member_join", member, identifier=member.id)
@staticmethod
async def on_member_remove(_: Bot, member: Member) -> None:
await call_event_handlers("member_remove", member, identifier=member.id)
@staticmethod
async def on_member_update(_: Bot, before: Member, after: Member) -> None:
# check if nickname has been updated
if before.nick != after.nick:
await call_event_handlers("member_nick_update", before, after, identifier=before.id)
# check if roles have been added or removed
roles_before = set(before.roles)
roles_after = set(after.roles)
for role in roles_before:
if role not in roles_after:
await call_event_handlers("member_role_remove", after, role, identifier=before.id)
for role in roles_after:
if role not in roles_before:
await call_event_handlers("member_role_add", after, role, identifier=before.id)
@staticmethod
async def on_user_update(_: Bot, before: User, after: User) -> None:
await call_event_handlers("user_update", before, after, identifier=before.id)
@staticmethod
async def on_voice_state_update(_: Bot, member: Member, before: VoiceState, after: VoiceState) -> None:
await call_event_handlers("voice_state_update", member, before, after, identifier=member.id)
@staticmethod
async def on_member_ban(_: Bot, guild: Guild, user: User | Member) -> None:
await call_event_handlers("member_ban", guild, user, identifier=user.id)
@staticmethod
async def on_member_unban(_: Bot, guild: Guild, user: User) -> None:
await call_event_handlers("member_unban", guild, user, identifier=user.id)
@staticmethod
async def on_invite_create(_: Bot, invite: Invite) -> None:
await call_event_handlers("invite_create", invite, identifier=invite.code)
@staticmethod
async def on_invite_delete(_: Bot, invite: Invite) -> None:
await call_event_handlers("invite_delete", invite, identifier=invite.code)
@staticmethod
async def on_command_error(_: Bot, ctx: Context[Bot], error: CommandError) -> None:
await call_event_handlers("command_error", ctx, error, identifier=ctx.message.id)
@staticmethod
async def on_thread_join(_: Bot, thread: Thread) -> None:
await call_event_handlers("thread_join", thread, identifier=thread.id)
|