only count streaks in the bot's own server

Messages in other servers the bot is in and in DMs no longer count
towards streaks, and catching up on missed messages at startup only
reads that server. Streaks started elsewhere made /streak se look up a
channel in another server, which crashed with InvalidData.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
leanderandClaude Opus 5.5 committed 2026-11-01 20:34:43 +01:00
1 parent 154d4de881
commit 562c30f45e
1 file changed
+17 -16
+17 -16
View File
@@ -101,7 +101,7 @@ class Streak(commands.Cog):
message (discord.Message): The message
"""
if message.author.bot:
if message.author.bot or message.guild is None or message.guild.id != self.bot.guild_id:
return
self.record_message(message)
@@ -143,25 +143,26 @@ class Streak(commands.Cog):
await self.bot.wait_until_ready()
if not (guild := self.bot.get_guild(self.bot.guild_id)):
self.bot.logger.warning("Could not find the bot's server, so missed messages won't count towards streaks")
return
since = discord.utils.utcnow().astimezone(self.bot.timezone).replace(hour=0, minute=0, second=0, microsecond=0)
counted = 0
for guild in self.bot.guilds:
channels = [c for c in guild.channels if isinstance(c, discord.abc.Messageable)] + list(guild.threads)
for channel in channels:
if not channel.permissions_for(guild.me).read_message_history:
continue
channels = [c for c in guild.channels if isinstance(c, discord.abc.Messageable)] + list(guild.threads)
for channel in channels:
if not channel.permissions_for(guild.me).read_message_history:
continue
try:
async for message in channel.history(limit=None, after=since):
if not message.author.bot:
self.record_message(message)
counted += 1
except discord.HTTPException as e:
self.bot.logger.warning(
f"Could not read the history of {channel} while catching up on streaks: {e}"
)
try:
async for message in channel.history(limit=None, after=since):
if not message.author.bot:
self.record_message(message)
counted += 1
except discord.HTTPException as e:
self.bot.logger.warning(f"Could not read the history of {channel} while catching up on streaks: {e}")
self.bot.logger.info(f"Counted {counted} messages since {since} towards streaks")
self.bot.logger.info(f"Counted {counted} messages in {guild} since {since} towards streaks")
async def insert_cache(self):
"""