Преглед на файлове

Optimized raw_reaction_add for mod actions to make fewer requests. Errors now logged with bot_log

master
Rocketsoup преди 1 година
родител
ревизия
57cf1bdd8e
променени са 3 файла, в които са добавени 35 реда и са изтрити 29 реда
  1. 6
    7
      bot.py
  2. 23
    16
      rocketbot/cogs/basecog.py
  3. 6
    6
      rocketbot/cogs/logcog.py

+ 6
- 7
bot.py Целия файл

@@ -42,8 +42,7 @@ class Rocketbot(commands.Bot):
42 42
 		super().__init__(command_prefix, **kwargs)
43 43
 
44 44
 	async def on_command_error(self, context: commands.Context, exception):
45
-		for line in traceback.format_stack():
46
-			print(line.strip())
45
+		bot_log(None, None, 'Command error:\n' + '\n'.join(traceback.format_stack()))
47 46
 		if context.guild is None or \
48 47
 				context.message.channel is None or \
49 48
 				context.message.author.bot:
@@ -61,11 +60,11 @@ class Rocketbot(commands.Bot):
61 60
 		# traceback.print_exception(type(exception), exception, exception.__traceback__)
62 61
 
63 62
 	async def on_error(self, event_method, args=None, kwargs=None):
64
-		print('Event caused error')
65
-		print(f'	event method: {event_method}')
66
-		print(f'	event args: {args}')
67
-		print(f'	event kwargs: {kwargs}')
68
-		print(traceback.format_exc())
63
+		bot_log(None, None, 'Event caused error\n' + \
64
+			f'	event method: {event_method}' + \
65
+			f'	event args: {args}' + \
66
+			f'	event kwargs: {kwargs}' + \
67
+			traceback.format_exc())
69 68
 
70 69
 async def start_bot():
71 70
 	intents = Intents.default()

+ 23
- 16
rocketbot/cogs/basecog.py Целия файл

@@ -180,29 +180,19 @@ class BaseCog(commands.Cog):
180 180
 	@commands.Cog.listener()
181 181
 	async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
182 182
 		'Event handler'
183
+		# Avoid any unnecessary requests. Gets called for every reaction
184
+		# multiplied by every active cog.
183 185
 		if payload.user_id == self.bot.user.id:
184 186
 			# Ignore bot's own reactions
185 187
 			return
186
-		member: Member = payload.member
187
-		if member is None:
188
-			return
189
-		guild: Guild = self.bot.get_guild(payload.guild_id)
188
+
189
+		guild: Guild = self.bot.get_guild(payload.guild_id) or await self.bot.fetch_guild(payload.guild_id)
190 190
 		if guild is None:
191 191
 			# Possibly a DM
192 192
 			return
193
-		channel: GuildChannel = guild.get_channel(payload.channel_id)
194
-		if channel is None:
195
-			# Possibly a DM
196
-			return
197
-		message: Message = await channel.fetch_message(payload.message_id)
198
-		if message is None:
199
-			# Message deleted?
200
-			return
201
-		if message.author.id != self.bot.user.id:
202
-			# Bot didn't author this
203
-			return
193
+
204 194
 		guild_messages = self.__bot_messages(guild)
205
-		bot_message = guild_messages.get(message.id)
195
+		bot_message = guild_messages.get(payload.message_id)
206 196
 		if bot_message is None:
207 197
 			# Unknown message (expired or was never tracked)
208 198
 			return
@@ -213,9 +203,26 @@ class BaseCog(commands.Cog):
213 203
 		if reaction is None or not reaction.is_enabled:
214 204
 			# Can't use this reaction with this message
215 205
 			return
206
+
207
+		channel: GuildChannel = guild.get_channel(payload.channel_id) or await guild.fetch_channel(payload.channel_id)
208
+		if channel is None:
209
+			# Possibly a DM
210
+			return
211
+		member: Member = payload.member
212
+		if member is None:
213
+			return
216 214
 		if not channel.permissions_for(member).ban_members:
217 215
 			# Not a mod (could make permissions configurable per BotMessageReaction some day)
218 216
 			return
217
+
218
+		message: Message = await channel.fetch_message(payload.message_id)
219
+		if message is None:
220
+			# Message deleted?
221
+			return
222
+		if message.author.id != self.bot.user.id:
223
+			# Bot didn't author this
224
+			return
225
+
219 226
 		await self.on_mod_react(bot_message, reaction, member)
220 227
 
221 228
 	async def on_mod_react(self,

+ 6
- 6
rocketbot/cogs/logcog.py Целия файл

@@ -524,12 +524,12 @@ class LoggingCog(BaseCog, name='Logging'):
524 524
 		"""
525 525
 		if payload.cached_message:
526 526
 			return  # already handled by on_message_edit
527
-		guild = await self.bot.fetch_guild(payload.guild_id)
527
+		guild = self.bot.get_guild(payload.guild_id) or await self.bot.fetch_guild(payload.guild_id)
528 528
 		if not guild:
529 529
 			return
530 530
 		if not self.get_guild_setting(guild, self.SETTING_ENABLED):
531 531
 			return
532
-		channel = await guild.fetch_channel(payload.channel_id)
532
+		channel = guild.get_channel(payload.channel_id) or await guild.fetch_channel(payload.channel_id)
533 533
 		if not channel:
534 534
 			return
535 535
 		message = await channel.fetch_message(payload.message_id)
@@ -568,12 +568,12 @@ class LoggingCog(BaseCog, name='Logging'):
568 568
 			bot_message = BotMessage(message.guild, text, BotMessage.TYPE_LOG, suppress_embeds=True)
569 569
 			await bot_message.update()
570 570
 		else:
571
-			guild = await self.bot.fetch_guild(payload.guild_id)
571
+			guild = self.bot.get_guild(payload.guild_id) or await self.bot.fetch_guild(payload.guild_id)
572 572
 			if not guild:
573 573
 				return
574 574
 			if not self.get_guild_setting(guild, self.SETTING_ENABLED):
575 575
 				return
576
-			channel = await guild.fetch_channel(payload.channel_id)
576
+			channel = guild.get_channel(payload.channel_id) or await guild.fetch_channel(payload.channel_id)
577 577
 			if not channel:
578 578
 				return
579 579
 			text = f'Message {payload.message_id} deleted in ' + channel.mention + ' but content and author not available in cache.'
@@ -592,12 +592,12 @@ class LoggingCog(BaseCog, name='Logging'):
592 592
 
593 593
 		https://discordpy.readthedocs.io/en/stable/api.html#discord.on_raw_bulk_message_delete
594 594
 		"""
595
-		guild = await self.bot.fetch_guild(payload.guild_id)
595
+		guild = self.bot.get_guild(payload.guild_id) or await self.bot.fetch_guild(payload.guild_id)
596 596
 		if not guild:
597 597
 			return
598 598
 		if not self.get_guild_setting(guild, self.SETTING_ENABLED):
599 599
 			return
600
-		channel = await guild.fetch_channel(payload.channel_id)
600
+		channel = guild.get_channel(payload.channel_id) or await guild.fetch_channel(payload.channel_id)
601 601
 		count = len(payload.message_ids)
602 602
 		cached_count = len(payload.cached_messages)
603 603
 		uncached_count = count - cached_count

Loading…
Отказ
Запис