From 1a1d7c79b06ed52616b5f681270cd2238b32e792 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 14 Oct 2021 16:28:59 +0100 Subject: Ignore channels mod team can't view in modlog The main purpose of this is so that we don't need to keep adding to the modlog_blacklist for every new channel we want to ignore, since it now implicitly ignores channels mods don't have read perms to. This is done by making use of Discord.py 2.0's updated behaviour of Channel.permissions_for() where it can now accept a role and determines the permissions of users in that role for a given channel. This takes into account default permissions as well as channel overrides. Co-authored-by: Xithrius --- bot/exts/moderation/modlog.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/bot/exts/moderation/modlog.py b/bot/exts/moderation/modlog.py index 3e19083ac..375956dd3 100644 --- a/bot/exts/moderation/modlog.py +++ b/bot/exts/moderation/modlog.py @@ -527,17 +527,28 @@ class ModLog(Cog, name="ModLog"): return self.is_raw_message_blacklisted(message.guild.id, message.channel.id) - def is_raw_message_blacklisted(self, guild_id: t.Optional[int], channel_id: int) -> bool: - """Return true if the message constructed from raw parameter is in a blacklisted thread or channel.""" - # Ignore DMs or messages outside of the main guild - if not guild_id or guild_id != GuildConstant.id: - return True - + def is_raw_message_blacklisted(self, channel_id: int) -> bool: + """ + Return true if the channel, or parent channel in the case of threads, passed should be ignored by modlog. + + Currently ignored channels are: + 1. Channels not in the guild we care about (constants.Guild.id). + 2. Channels that mods do not have view permissions to + 3. Channels in constants.Guild.modlog_blacklist + """ channel = self.bot.get_channel(channel_id) - # Look at the parent channel of a thread + # Ignore not found channels, DMs, and messages outside of the main guild. + if not channel or channel.guild and channel.guild.id != GuildConstant.id: + return True + + # Look at the parent channel of a thread. if isinstance(channel, Thread): - return channel.parent.id in GuildConstant.modlog_blacklist + channel = channel.parent + + # Mod team doesn't have view permission to the channel. + if not channel.permissions_for(channel.guild.get_role(Roles.mod_team)).view_channel: + return True return channel.id in GuildConstant.modlog_blacklist -- cgit v1.2.3 From db7857d8afc55244a26fce6687c172fefc5dc47c Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 14 Oct 2021 16:30:01 +0100 Subject: Refactor: Update modlog ignore function name This new name better describes what this function actually does --- bot/exts/moderation/modlog.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/exts/moderation/modlog.py b/bot/exts/moderation/modlog.py index 375956dd3..0dc2e84b5 100644 --- a/bot/exts/moderation/modlog.py +++ b/bot/exts/moderation/modlog.py @@ -525,9 +525,9 @@ class ModLog(Cog, name="ModLog"): if message.author.bot or not message.guild: return True - return self.is_raw_message_blacklisted(message.guild.id, message.channel.id) + return self.is_channel_ignored(message.channel.id) - def is_raw_message_blacklisted(self, channel_id: int) -> bool: + def is_channel_ignored(self, channel_id: int) -> bool: """ Return true if the channel, or parent channel in the case of threads, passed should be ignored by modlog. @@ -613,7 +613,7 @@ class ModLog(Cog, name="ModLog"): @Cog.listener() async def on_raw_message_delete(self, event: discord.RawMessageDeleteEvent) -> None: """Log raw message delete event to message change log.""" - if self.is_raw_message_blacklisted(event.guild_id, event.channel_id): + if self.is_channel_ignored(event.channel_id): return await asyncio.sleep(1) # Wait here in case the normal event was fired -- cgit v1.2.3 From 457cd6e34212fae08ef74b1b08dba2984e57a665 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 14 Oct 2021 16:30:21 +0100 Subject: Use modlog ignore function for voice events too --- bot/exts/moderation/modlog.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/exts/moderation/modlog.py b/bot/exts/moderation/modlog.py index 0dc2e84b5..b09eb2d14 100644 --- a/bot/exts/moderation/modlog.py +++ b/bot/exts/moderation/modlog.py @@ -838,7 +838,8 @@ class ModLog(Cog, name="ModLog"): """Log member voice state changes to the voice log channel.""" if ( member.guild.id != GuildConstant.id - or (before.channel and before.channel.id in GuildConstant.modlog_blacklist) + or (before.channel and self.is_channel_ignored(before.channel.id)) + or (after.channel and self.is_channel_ignored(after.channel.id)) ): return -- cgit v1.2.3 From 2b8807b448415e9c08cff136e3343f15d81a3037 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 14 Oct 2021 16:31:25 +0100 Subject: Remove unneeded config entries Since channels that mods can't read are now implicitly ignored, there is no need to explicitly ignore them. --- config-default.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/config-default.yml b/config-default.yml index d77eacc7e..b61d9c99c 100644 --- a/config-default.yml +++ b/config-default.yml @@ -248,15 +248,13 @@ guild: - *ADMIN_SPAM - *MODS - # Modlog cog ignores events which occur in these channels + # Modlog cog explicitly ignores events which occur in these channels. + # This is on top of implicitly ignoring events in channels that the mod team cannot view. modlog_blacklist: - - *ADMINS - - *ADMINS_VOICE - *ATTACH_LOG - *MESSAGE_LOG - *MOD_LOG - *STAFF_VOICE - - *DEV_CORE_VOTING reminder_whitelist: - *BOT_CMD -- cgit v1.2.3