aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/exts/filtering/filtering.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/bot/exts/filtering/filtering.py b/bot/exts/filtering/filtering.py
index 3fb627a82..bf38a2818 100644
--- a/bot/exts/filtering/filtering.py
+++ b/bot/exts/filtering/filtering.py
@@ -50,6 +50,7 @@ from bot.utils.message_cache import MessageCache
log = get_logger(__name__)
WEBHOOK_ICON_URL = r"https://github.com/python-discord/branding/raw/main/icons/filter/filter_pfp.png"
+WEBHOOK_NAME = "Filtering System"
CACHE_SIZE = 1000
HOURS_BETWEEN_NICKNAME_ALERTS = 1
OFFENSIVE_MSG_DELETE_TIME = datetime.timedelta(days=7)
@@ -96,7 +97,7 @@ class Filtering(Cog):
example_list = loaded_list
# The webhook must be generated by the bot to send messages with components through it.
- self.webhook = await self._generate_webhook()
+ self.webhook = await self._fetch_or_generate_filtering_webhook()
self.collect_loaded_types(example_list)
await self.schedule_offending_messages_deletion()
@@ -861,8 +862,15 @@ class Filtering(Cog):
self.filter_lists[list_name] = filter_list_types[list_name](self)
return self.filter_lists[list_name].add_list(list_data)
- async def _generate_webhook(self) -> discord.Webhook | None:
+ async def _fetch_or_generate_filtering_webhook(self) -> discord.Webhook | None:
"""Generate a webhook with the filtering avatar."""
+ alerts_channel = self.bot.get_guild(Guild.id).get_channel(Channels.mod_alerts)
+ # Try to find an existing webhook.
+ for webhook in await alerts_channel.webhooks():
+ if webhook.name == WEBHOOK_NAME and webhook.user == self.bot.user and webhook.is_authenticated():
+ log.trace(f"Found existing filters webhook with ID {webhook.id}.")
+ return webhook
+
# Download the filtering avatar from the branding repository.
webhook_icon = None
async with self.bot.http_session.get(WEBHOOK_ICON_URL, params=PARAMS, headers=HEADERS) as response:
@@ -872,11 +880,13 @@ class Filtering(Cog):
else:
log.warning(f"Failed to fetch filtering webhook icon due to status: {response.status}")
- alerts_channel = self.bot.get_guild(Guild.id).get_channel(Channels.mod_alerts)
+ # Generate a new webhook.
try:
- return await alerts_channel.create_webhook(name="Filtering System", avatar=webhook_icon)
- except HTTPException:
- log.error("Failed to create filters webhook.")
+ webhook = await alerts_channel.create_webhook(name=WEBHOOK_NAME, avatar=webhook_icon)
+ log.trace(f"Generated new filters webhook with ID {webhook.id},")
+ return webhook
+ except HTTPException as e:
+ log.error(f"Failed to create filters webhook: {e}")
return None
async def _resolve_action(