diff options
| author | 2020-05-31 18:34:36 +0200 | |
|---|---|---|
| committer | 2020-05-31 18:34:36 +0200 | |
| commit | 0737b1a63ca359e88ef580143e8e4e6a879c482e (patch) | |
| tree | bb90bcd686a592d09648d58bd0d3915c94d9fcc6 | |
| parent | Remove unnecessary conditional. (diff) | |
Add a mod_log.ignore_all context manager.
This new context manager makes it easier to make the mod_log ignore
actions like message deletions. The only existing method is the
`ignore()` method, which requires that you pass all the messages you
want to ignore into it.
This one just ignores everything inside its scope.
This isn't the DRYest approach, but it's low-cost and improves the
readability of clean.py quite a bit. Ideally we should go through and
give modlog a proper cleanup, because it's kinda ugly right now.
| -rw-r--r-- | bot/cogs/moderation/modlog.py | 35 | 
1 files changed, 35 insertions, 0 deletions
| diff --git a/bot/cogs/moderation/modlog.py b/bot/cogs/moderation/modlog.py index 9d28030d9..b3ae8e215 100644 --- a/bot/cogs/moderation/modlog.py +++ b/bot/cogs/moderation/modlog.py @@ -3,6 +3,7 @@ import difflib  import itertools  import logging  import typing as t +from contextlib import contextmanager  from datetime import datetime  from itertools import zip_longest @@ -40,6 +41,7 @@ class ModLog(Cog, name="ModLog"):      def __init__(self, bot: Bot):          self.bot = bot          self._ignored = {event: [] for event in Event} +        self._ignore_all = False          self._cached_deletes = []          self._cached_edits = [] @@ -81,6 +83,15 @@ class ModLog(Cog, name="ModLog"):              if item not in self._ignored[event]:                  self._ignored[event].append(item) +    @contextmanager +    def ignore_all(self) -> None: +        """Ignore all events while inside this context scope.""" +        self._ignore_all = True +        try: +            yield +        finally: +            self._ignore_all = False +      async def send_log_message(          self,          icon_url: t.Optional[str], @@ -191,6 +202,9 @@ class ModLog(Cog, name="ModLog"):              self._ignored[Event.guild_channel_update].remove(before.id)              return +        if self._ignore_all: +            return +          # Two channel updates are sent for a single edit: 1 for topic and 1 for category change.          # TODO: remove once support is added for ignoring multiple occurrences for the same channel.          help_categories = (Categories.help_available, Categories.help_dormant, Categories.help_in_use) @@ -386,6 +400,9 @@ class ModLog(Cog, name="ModLog"):              self._ignored[Event.member_ban].remove(member.id)              return +        if self._ignore_all: +            return +          await self.send_log_message(              Icons.user_ban, Colours.soft_red,              "User banned", f"{member} (`{member.id}`)", @@ -426,6 +443,9 @@ class ModLog(Cog, name="ModLog"):              self._ignored[Event.member_remove].remove(member.id)              return +        if self._ignore_all: +            return +          member_str = escape_markdown(str(member))          await self.send_log_message(              Icons.sign_out, Colours.soft_red, @@ -444,6 +464,9 @@ class ModLog(Cog, name="ModLog"):              self._ignored[Event.member_unban].remove(member.id)              return +        if self._ignore_all: +            return +          member_str = escape_markdown(str(member))          await self.send_log_message(              Icons.user_unban, Colour.blurple(), @@ -462,6 +485,9 @@ class ModLog(Cog, name="ModLog"):              self._ignored[Event.member_update].remove(before.id)              return +        if self._ignore_all: +            return +          diff = DeepDiff(before, after)          changes = []          done = [] @@ -564,6 +590,9 @@ class ModLog(Cog, name="ModLog"):              self._ignored[Event.message_delete].remove(message.id)              return +        if self._ignore_all: +            return +          if author.bot:              return @@ -623,6 +652,9 @@ class ModLog(Cog, name="ModLog"):              self._ignored[Event.message_delete].remove(event.message_id)              return +        if self._ignore_all: +            return +          channel = self.bot.get_channel(event.channel_id)          if channel.category: @@ -797,6 +829,9 @@ class ModLog(Cog, name="ModLog"):              self._ignored[Event.voice_state_update].remove(member.id)              return +        if self._ignore_all: +            return +          # Exclude all channel attributes except the name.          diff = DeepDiff(              before, | 
