diff options
Diffstat (limited to '')
| -rw-r--r-- | bot/bot.py | 4 | ||||
| -rw-r--r-- | bot/exts/moderation/infraction/infractions.py | 9 | ||||
| -rw-r--r-- | bot/exts/moderation/watchchannels/_watchchannel.py | 56 | 
3 files changed, 40 insertions, 29 deletions
| diff --git a/bot/bot.py b/bot/bot.py index e6d77344e..9a60474b3 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -136,7 +136,9 @@ class Bot(commands.Bot):      def _remove_extensions(self) -> None:          """Remove all extensions to trigger cog unloads.""" -        for ext in self.extensions.keys(): +        extensions = list(self.extensions.keys()) + +        for ext in extensions:              try:                  self.unload_extension(ext)              except Exception: diff --git a/bot/exts/moderation/infraction/infractions.py b/bot/exts/moderation/infraction/infractions.py index ccddd4530..b638f4dc6 100644 --- a/bot/exts/moderation/infraction/infractions.py +++ b/bot/exts/moderation/infraction/infractions.py @@ -242,8 +242,13 @@ class Infractions(InfractionScheduler, commands.Cog):          async def action() -> None:              try:                  await user.add_roles(self._muted_role, reason=reason) -            except discord.NotFound: -                log.info(f"User {user} ({user.id}) left from guild. Can't give Muted role.") +            except discord.HTTPException as e: +                if e.code == 10007: +                    log.info(f"User {user} ({user.id}) left from guild. Can't give Muted role.") +                else: +                    log.warning( +                        f"Got response {e.code} (HTTP {e.status}) while giving muted role to {user} ({user.id})." +                    )              else:                  log.trace(f"Attempting to kick {user} from voice because they've been muted.")                  await user.move_to(None, reason=reason) diff --git a/bot/exts/moderation/watchchannels/_watchchannel.py b/bot/exts/moderation/watchchannels/_watchchannel.py index 4715dce14..b576f2888 100644 --- a/bot/exts/moderation/watchchannels/_watchchannel.py +++ b/bot/exts/moderation/watchchannels/_watchchannel.py @@ -171,38 +171,32 @@ class WatchChannel(metaclass=CogABCMeta):      async def consume_messages(self, delay_consumption: bool = True) -> None:          """Consumes the message queues to log watched users' messages.""" -        try: -            if delay_consumption: -                self.log.trace(f"Sleeping {BigBrotherConfig.log_delay} seconds before consuming message queue") -                await asyncio.sleep(BigBrotherConfig.log_delay) +        if delay_consumption: +            self.log.trace(f"Sleeping {BigBrotherConfig.log_delay} seconds before consuming message queue") +            await asyncio.sleep(BigBrotherConfig.log_delay) -            self.log.trace("Started consuming the message queue") +        self.log.trace("Started consuming the message queue") -            # If the previous consumption Task failed, first consume the existing comsumption_queue -            if not self.consumption_queue: -                self.consumption_queue = self.message_queue.copy() -                self.message_queue.clear() +        # If the previous consumption Task failed, first consume the existing comsumption_queue +        if not self.consumption_queue: +            self.consumption_queue = self.message_queue.copy() +            self.message_queue.clear() -            for user_channel_queues in self.consumption_queue.values(): -                for channel_queue in user_channel_queues.values(): -                    while channel_queue: -                        msg = channel_queue.popleft() +        for user_channel_queues in self.consumption_queue.values(): +            for channel_queue in user_channel_queues.values(): +                while channel_queue: +                    msg = channel_queue.popleft() -                        self.log.trace(f"Consuming message {msg.id} ({len(msg.attachments)} attachments)") -                        await self.relay_message(msg) +                    self.log.trace(f"Consuming message {msg.id} ({len(msg.attachments)} attachments)") +                    await self.relay_message(msg) -            self.consumption_queue.clear() +        self.consumption_queue.clear() -            if self.message_queue: -                self.log.trace("Channel queue not empty: Continuing consuming queues") -                self._consume_task = self.bot.loop.create_task(self.consume_messages(delay_consumption=False)) -            else: -                self.log.trace("Done consuming messages.") -        except asyncio.CancelledError as e: -            self.log.exception( -                "The consume task was canceled. Messages may be lost.", -                exc_info=e -            ) +        if self.message_queue: +            self.log.trace("Channel queue not empty: Continuing consuming queues") +            self._consume_task = self.bot.loop.create_task(self.consume_messages(delay_consumption=False)) +        else: +            self.log.trace("Done consuming messages.")      async def webhook_send(          self, @@ -348,4 +342,14 @@ class WatchChannel(metaclass=CogABCMeta):          """Takes care of unloading the cog and canceling the consumption task."""          self.log.trace("Unloading the cog")          if self._consume_task and not self._consume_task.done(): +            def done_callback(task: asyncio.Task) -> None: +                """Send exception when consuming task have been cancelled.""" +                try: +                    task.exception() +                except asyncio.CancelledError: +                    self.log.error( +                        f"The consume task of {type(self).__name__} was canceled. Messages may be lost." +                    ) + +            self._consume_task.add_done_callback(done_callback)              self._consume_task.cancel() | 
