From dd26c7501f6a9173a5672c01664c85937c194eda Mon Sep 17 00:00:00 2001 From: MattyTheHacker <18513864+MattyTheHacker@users.noreply.github.com> Date: Sun, 13 Apr 2025 15:10:21 +0100 Subject: [PATCH 1/3] Add exception handling --- cogs/strike.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/cogs/strike.py b/cogs/strike.py index a3db5714a..9b427b793 100644 --- a/cogs/strike.py +++ b/cogs/strike.py @@ -8,8 +8,6 @@ from typing import TYPE_CHECKING import discord - -# noinspection SpellCheckingInspection from asyncstdlib.builtins import any as asyncany from discord.ui import View @@ -275,20 +273,23 @@ async def _send_strike_user_message( actual_strike_amount: int = min(3, member_strikes.strikes) - await strike_user.send( - "Hi, a recent incident occurred in which you may have broken one or more of " - f"the {self.bot.group_short_name} Discord server's rules.\n" - "We have increased the number of strikes associated with your account " - f"to {actual_strike_amount} and " - "the corresponding moderation action will soon be applied to you. " - "To find what moderation action corresponds to which strike level, " - "you can view " - f"the {self.bot.group_short_name} Discord server moderation document " - f"[here](<{settings.MODERATION_DOCUMENT_URL}>)\nPlease ensure you have read " - f"the rules in {rules_channel_mention} so that your future behaviour adheres " - f"to them.{includes_ban_message}\n\nA committee member will be in contact " - "with you shortly, to discuss this further.", - ) + try: + await strike_user.send( + "Hi, a recent incident occurred in which you may have broken one or more of " + f"the {self.bot.group_short_name} Discord server's rules.\n" + "We have increased the number of strikes associated with your account " + f"to {actual_strike_amount} and " + "the corresponding moderation action will soon be applied to you. " + "To find what moderation action corresponds to which strike level, " + "you can view " + f"the {self.bot.group_short_name} Discord server moderation document " + f"[here](<{settings.MODERATION_DOCUMENT_URL}>)\nPlease ensure you have read " + f"the rules in {rules_channel_mention} so that your future behaviour adheres " + f"to them.{includes_ban_message}\n\nA committee member will be in contact " + "with you shortly, to discuss this further.", + ) + except discord.Forbidden: + logger.warning("Unable to send strike message to %s", strike_user) async def _confirm_perform_moderation_action( self, @@ -392,7 +393,6 @@ async def _confirm_increase_strike( ) if not perform_action: - # noinspection SpellCheckingInspection await message_sender_component.send( content=( f"{confirm_strike_message}\n" @@ -648,7 +648,6 @@ async def _confirm_manual_add_strike( "with number of strikes**" ), ) - # noinspection SpellCheckingInspection await out_of_sync_ban_confirmation_message.edit( content=( f"Successfully banned {strike_user.mention}.\n" From 028f1fac8907204c5802dac0ccfbab29f7f6e922 Mon Sep 17 00:00:00 2001 From: MattyTheHacker <18513864+MattyTheHacker@users.noreply.github.com> Date: Sun, 13 Apr 2025 15:10:43 +0100 Subject: [PATCH 2/3] yeet that wank --- cogs/induct.py | 4 ---- cogs/make_member.py | 2 -- cogs/send_get_roles_reminders.py | 2 -- cogs/send_introduction_reminders.py | 1 - 4 files changed, 9 deletions(-) diff --git a/cogs/induct.py b/cogs/induct.py index 92d49fd7a..636c933bb 100644 --- a/cogs/induct.py +++ b/cogs/induct.py @@ -88,12 +88,10 @@ async def on_member_update(self, before: discord.Member, after: discord.Member) reason="Delete introduction reminders after member is inducted.", ) - # noinspection PyUnusedLocal rules_channel_mention: str = "**`#welcome`**" with contextlib.suppress(RulesChannelDoesNotExistError): rules_channel_mention = (await self.bot.rules_channel).mention - # noinspection PyUnusedLocal roles_channel_mention: str = "**`#roles`**" with contextlib.suppress(RolesChannelDoesNotExistError): roles_channel_mention = (await self.bot.roles_channel).mention @@ -229,7 +227,6 @@ async def _perform_induction( if not silent: general_channel: discord.TextChannel = await self.bot.general_channel - # noinspection PyUnusedLocal roles_channel_mention: str = "**`#roles`**" with contextlib.suppress(RolesChannelDoesNotExistError): roles_channel_mention = (await self.bot.roles_channel).mention @@ -245,7 +242,6 @@ async def _perform_induction( reason=INDUCT_AUDIT_MESSAGE, ) - # noinspection PyUnusedLocal applicant_role: discord.Role | None = None with contextlib.suppress(ApplicantRoleDoesNotExistError): applicant_role = await ctx.bot.applicant_role diff --git a/cogs/make_member.py b/cogs/make_member.py index 1171adedd..62c4a0f78 100644 --- a/cogs/make_member.py +++ b/cogs/make_member.py @@ -160,7 +160,6 @@ async def make_member(self, ctx: "TeXBotApplicationContext", group_member_id: st ) ).aexists() if GROUP_MEMBER_ID_IS_ALREADY_USED: - # noinspection PyUnusedLocal committee_mention: str = "committee" with contextlib.suppress(CommitteeRoleDoesNotExistError): committee_mention = (await self.bot.committee_role).mention @@ -279,7 +278,6 @@ async def make_member(self, ctx: "TeXBotApplicationContext", group_member_id: st reason='TeX Bot slash-command: "/makemember"', ) - # noinspection PyUnusedLocal applicant_role: discord.Role | None = None with contextlib.suppress(ApplicantRoleDoesNotExistError): applicant_role = await ctx.bot.applicant_role diff --git a/cogs/send_get_roles_reminders.py b/cogs/send_get_roles_reminders.py index e67b61825..32508bd89 100644 --- a/cogs/send_get_roles_reminders.py +++ b/cogs/send_get_roles_reminders.py @@ -131,10 +131,8 @@ async def send_get_roles_reminders(self) -> None: if sent_get_roles_reminder_member_exists: continue - # noinspection PyUnusedLocal guest_role_received_time: datetime.datetime | None = None with contextlib.suppress(StopIteration, StopAsyncIteration): - # noinspection PyTypeChecker guest_role_received_time = await anext( log.created_at async for log in main_guild.audit_logs( diff --git a/cogs/send_introduction_reminders.py b/cogs/send_introduction_reminders.py index 79f48c41b..bd4bbe71c 100644 --- a/cogs/send_introduction_reminders.py +++ b/cogs/send_introduction_reminders.py @@ -132,7 +132,6 @@ async def send_introduction_reminders(self) -> None: continue async for message in member.history(): - # noinspection PyUnresolvedReferences MESSAGE_CONTAINS_OPT_IN_OUT_BUTTON: bool = bool( bool(message.components) and isinstance(message.components[0], discord.ActionRow) From c2aa23fde946eed0e68e945a272845889f8d69bf Mon Sep 17 00:00:00 2001 From: MattyTheHacker <18513864+MattyTheHacker@users.noreply.github.com> Date: Mon, 14 Apr 2025 14:38:33 +0100 Subject: [PATCH 3/3] put it back --- cogs/strike.py | 53 +++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/cogs/strike.py b/cogs/strike.py index 94faa470e..670222af6 100644 --- a/cogs/strike.py +++ b/cogs/strike.py @@ -254,30 +254,35 @@ class BaseStrikeCog(TeXBotBaseCog): async def _send_strike_user_message( self, strike_user: discord.User | discord.Member, member_strikes: DiscordMemberStrikes ) -> None: - await strike_user.send( - "Hi, a recent incident occurred in which you may have broken one or more of " - f"the {self.bot.group_short_name} Discord server's rules.\n" - "We have increased the number of strikes associated with your account " - f"to {min(3, member_strikes.strikes)} and " - "the corresponding moderation action will soon be applied to you. " - "To find what moderation action corresponds to which strike level, " - "you can view " - f"the {self.bot.group_short_name} Discord server moderation document " - f"[here](<{settings.MODERATION_DOCUMENT_URL}>)\nPlease ensure you have read " - f"the rules in {await self.bot.get_mention_string(self.bot.rules_channel)} so " - "that your future behaviour adheres to them." - f"{ - ( - '\nBecause you now have been given 3 strikes, you have been banned from ' - f'the {self.bot.group_short_name} Discord server ' - f'and we have contacted {self.bot.group_moderation_contact} for ' - 'further action & advice.' - ) - if member_strikes.strikes >= 3 - else '' - }\n\n" - "A committee member will be in contact with you shortly, to discuss this further." - ) + try: + await strike_user.send( + "Hi, a recent incident occurred in which you may have broken one or more of " + f"the {self.bot.group_short_name} Discord server's rules.\n" + "We have increased the number of strikes associated with your account " + f"to {min(3, member_strikes.strikes)} and " + "the corresponding moderation action will soon be applied to you. " + "To find what moderation action corresponds to which strike level, " + "you can view " + f"the {self.bot.group_short_name} Discord server moderation document " + f"[here](<{settings.MODERATION_DOCUMENT_URL}>)\nPlease ensure you have read " + f"the rules in {await self.bot.get_mention_string(self.bot.rules_channel)} so " + "that your future behaviour adheres to them." + f"{ + ( + '\nBecause you now have been given 3 strikes, ' + 'you have been banned from ' + f'the {self.bot.group_short_name} Discord server ' + f'and we have contacted {self.bot.group_moderation_contact} for ' + 'further action & advice.' + ) + if member_strikes.strikes >= 3 + else '' + }\n\n" + "A committee member will be in contact with you shortly, " + "to discuss this further." + ) + except discord.Forbidden: + logger.warning("Failed to send strike message to %s", strike_user) async def _confirm_perform_moderation_action( self,