From 4697076a8de1b2ffd554e959414fe9ac9f3af60c Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 1 Sep 2022 18:16:10 +0100 Subject: Send filter alerts to #filter-log instead of #mod-meta This is to reduce the amount of spam in #mod-meta. --- bot/constants.py | 1 + bot/exts/filters/filter_lists.py | 4 ++-- config-default.yml | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/bot/constants.py b/bot/constants.py index 68a96876f..830ba049c 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -420,6 +420,7 @@ class Channels(metaclass=YAMLGetter): how_to_get_help: int attachment_log: int + filter_log: int message_log: int mod_log: int nomination_archive: int diff --git a/bot/exts/filters/filter_lists.py b/bot/exts/filters/filter_lists.py index c643f9a84..8cb83c19f 100644 --- a/bot/exts/filters/filter_lists.py +++ b/bot/exts/filters/filter_lists.py @@ -117,9 +117,9 @@ class FilterLists(Cog): ) raise - # If it is an autoban trigger we send a warning in #mod-meta + # If it is an autoban trigger we send a warning in #filter-log if comment and "[autoban]" in comment: - await self.bot.get_channel(Channels.mod_meta).send( + await self.bot.get_channel(Channels.filter_log).send( f":warning: Heads-up! The new `{list_type}` filter " f"`{content}` (`{comment}`) will automatically ban users." ) diff --git a/config-default.yml b/config-default.yml index a12b680e1..e84a6045b 100644 --- a/config-default.yml +++ b/config-default.yml @@ -178,6 +178,7 @@ guild: # Logs attachment_log: &ATTACH_LOG 649243850006855680 + filter_log: &FILTER_LOG 1014943924185473094 message_log: &MESSAGE_LOG 467752170159079424 mod_log: &MOD_LOG 282638479504965634 nomination_archive: 833371042046148738 @@ -257,6 +258,7 @@ guild: - *MESSAGE_LOG - *MOD_LOG - *STAFF_VOICE + - *FILTER_LOG reminder_whitelist: - *BOT_CMD -- cgit v1.2.3 From fe86fb97e2206840927fa2642d5d512e98f4f025 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 1 Sep 2022 20:56:42 +0100 Subject: Namespace discord import in filter_lists --- bot/exts/filters/filter_lists.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/exts/filters/filter_lists.py b/bot/exts/filters/filter_lists.py index 8cb83c19f..808307e39 100644 --- a/bot/exts/filters/filter_lists.py +++ b/bot/exts/filters/filter_lists.py @@ -1,13 +1,13 @@ import re from typing import Optional +import discord from botcore.site_api import ResponseCodeError -from discord import Colour, Embed from discord.ext.commands import BadArgument, Cog, Context, IDConverter, group, has_any_role from bot import constants from bot.bot import Bot -from bot.constants import Channels +from bot.constants import Channels, Colours from bot.converters import ValidDiscordServerInvite, ValidFilterListType from bot.log import get_logger from bot.pagination import LinePaginator @@ -179,9 +179,9 @@ class FilterLists(Cog): # Build the embed list_type_plural = list_type.lower().replace("_", " ").title() + "s" - embed = Embed( + embed = discord.Embed( title=f"{allow_type.title()}ed {list_type_plural} ({len(result)} total)", - colour=Colour.blue() + colour=Colours.blue ) log.trace(f"Trying to list {len(result)} items from the {list_type.lower()} {allow_type}") -- cgit v1.2.3 From 4f56106a07039517660e24261352d5cd1ac91481 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 1 Sep 2022 20:57:58 +0100 Subject: Report on all autoban filters added in the last 7 days This adds both a command to runt he report, and schedules a report to be automatically once a week. --- bot/exts/filters/filter_lists.py | 56 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/bot/exts/filters/filter_lists.py b/bot/exts/filters/filter_lists.py index 808307e39..2d8698fcc 100644 --- a/bot/exts/filters/filter_lists.py +++ b/bot/exts/filters/filter_lists.py @@ -1,9 +1,13 @@ +import datetime import re +from collections import defaultdict from typing import Optional +import arrow import discord from botcore.site_api import ResponseCodeError -from discord.ext.commands import BadArgument, Cog, Context, IDConverter, group, has_any_role +from discord.ext import tasks +from discord.ext.commands import BadArgument, Cog, Context, IDConverter, command, group, has_any_role from bot import constants from bot.bot import Bot @@ -13,6 +17,7 @@ from bot.log import get_logger from bot.pagination import LinePaginator log = get_logger(__name__) +WEEKLY_REPORT_ISO_DAY = 3 # 1=Monday, 7=Sunday class FilterLists(Cog): @@ -33,6 +38,7 @@ class FilterLists(Cog): async def cog_load(self) -> None: """Add the valid FilterList types to the docstrings, so they'll appear in !help invocations.""" await self.bot.wait_until_guild_available() + self.weekly_autoban_report_task.start() # Add valid filterlist types to the docstrings valid_types = await ValidFilterListType.get_valid_types(self.bot) @@ -287,10 +293,58 @@ class FilterLists(Cog): """Syncs both allowlists and denylists with the API.""" await self._sync_data(ctx) + @command(name="filter_report") + async def force_send_weekly_report(self, ctx: Context) -> None: + """Send a list of autobans added in the last 7 days to #mod-meta.""" + await self.send_weekly_autoban_report() + + @tasks.loop(time=datetime.time(hour=18)) + async def weekly_autoban_report_task(self) -> None: + """Trigger autoban report to be sent if it is the desired day of the week (WEEKLY_REPORT_ISO_DAY).""" + if arrow.utcnow().isoweekday() != WEEKLY_REPORT_ISO_DAY: + return + + await self.send_weekly_autoban_report() + + async def send_weekly_autoban_report(self, channel: discord.abc.Messageable = None) -> None: + """Send a list of autobans added in the last 7 days to #mod-meta.""" + seven_days_ago = arrow.utcnow().shift(days=-7) + if not channel: + channel = self.bot.get_channel(Channels.mod_meta) + + added_autobans = defaultdict(list) + # Extract all autoban filters added in the past 7 days from each filter type + for filter_list, filters in self.bot.filter_list_cache.items(): + filter_type, allow = filter_list.split(".") + allow_type = "Allow list" if allow.lower() == "true" else "Deny list" + + for filter_content, filter_details in filters.items(): + created_at = arrow.get(filter_details["created_at"]) + updated_at = arrow.get(filter_details["updated_at"]) + # Default to empty string so that the in check below doesn't error on None type + comment = filter_details["comment"] or "" + if max(created_at, updated_at) > seven_days_ago and "[autoban]" in comment: + line = f"`{filter_content}`: {comment}" + added_autobans[f"**{filter_type} {allow_type}**"].append(line) + + # Nicely format the output so each filter list type is grouped + lines = [f"**Autoban filters added since {seven_days_ago.format('YYYY-MM-DD')}**"] + for filter_list, recently_added_autobans in added_autobans.items(): + lines.append("\n".join([filter_list]+recently_added_autobans)) + + if len(lines) == 1: + lines.append("Nothing to show") + + await channel.send("\n\n".join(lines)) + async def cog_check(self, ctx: Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" return await has_any_role(*constants.MODERATION_ROLES).predicate(ctx) + async def cog_unload(self) -> None: + """Cancel the weekly autoban filter report on cog unload.""" + self.weekly_autoban_report_task.cancel() + async def setup(bot: Bot) -> None: """Load the FilterLists cog.""" -- cgit v1.2.3 From f2d05d554f012598bcd42b4888400ced6513cb0d Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 11 Sep 2022 01:18:47 +0100 Subject: Send weekly_autoban_report to ctx channel when manually invoked --- bot/exts/filters/filter_lists.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bot/exts/filters/filter_lists.py b/bot/exts/filters/filter_lists.py index 2d8698fcc..edc2d09f3 100644 --- a/bot/exts/filters/filter_lists.py +++ b/bot/exts/filters/filter_lists.py @@ -295,8 +295,8 @@ class FilterLists(Cog): @command(name="filter_report") async def force_send_weekly_report(self, ctx: Context) -> None: - """Send a list of autobans added in the last 7 days to #mod-meta.""" - await self.send_weekly_autoban_report() + """Respond with a list of autobans added in the last 7 days.""" + await self.send_weekly_autoban_report(ctx.channel) @tasks.loop(time=datetime.time(hour=18)) async def weekly_autoban_report_task(self) -> None: @@ -307,7 +307,11 @@ class FilterLists(Cog): await self.send_weekly_autoban_report() async def send_weekly_autoban_report(self, channel: discord.abc.Messageable = None) -> None: - """Send a list of autobans added in the last 7 days to #mod-meta.""" + """ + Send a list of autobans added in the last 7 days to the specified channel. + + If chanel is not specified, it is sent to #mod-meta. + """ seven_days_ago = arrow.utcnow().shift(days=-7) if not channel: channel = self.bot.get_channel(Channels.mod_meta) -- cgit v1.2.3 From 2142d824272c738c3d4ac9f20dd59cc4f02e345e Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 2 Oct 2022 20:19:49 +0100 Subject: Make autoban report silently fail if output will be sent to a non-mod channel --- bot/exts/filters/filter_lists.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bot/exts/filters/filter_lists.py b/bot/exts/filters/filter_lists.py index edc2d09f3..c429b0eb9 100644 --- a/bot/exts/filters/filter_lists.py +++ b/bot/exts/filters/filter_lists.py @@ -15,6 +15,7 @@ from bot.constants import Channels, Colours from bot.converters import ValidDiscordServerInvite, ValidFilterListType from bot.log import get_logger from bot.pagination import LinePaginator +from bot.utils.channel import is_mod_channel log = get_logger(__name__) WEEKLY_REPORT_ISO_DAY = 3 # 1=Monday, 7=Sunday @@ -315,6 +316,9 @@ class FilterLists(Cog): seven_days_ago = arrow.utcnow().shift(days=-7) if not channel: channel = self.bot.get_channel(Channels.mod_meta) + elif not is_mod_channel(channel): + # Silently fail if output is going to be a non-mod channel. + return added_autobans = defaultdict(list) # Extract all autoban filters added in the past 7 days from each filter type -- cgit v1.2.3