diff --git a/cogs/command_error.py b/cogs/command_error.py index fcac8ee12..ecc751833 100644 --- a/cogs/command_error.py +++ b/cogs/command_error.py @@ -49,14 +49,13 @@ async def on_application_command_error( error_code = error.original.ERROR_CODE elif isinstance(error, CheckAnyFailure): - # TODO: Remove type ignore comments once #349 is resolved # noqa: FIX002 - if CommandChecks.is_interaction_user_in_main_guild_failure(error.checks[0]): # type: ignore[arg-type] + if CommandChecks.is_interaction_user_in_main_guild_failure(error.checks[0]): message = ( f"You must be a member of the {self.bot.group_short_name} Discord server " "to use this command." ) - elif CommandChecks.is_interaction_user_has_committee_role_failure(error.checks[0]): # type: ignore[arg-type] + elif CommandChecks.is_interaction_user_has_committee_role_failure(error.checks[0]): message = ( f"Only {await self.bot.get_mention_string(self.bot.committee_role)} " "members can run this command." diff --git a/cogs/committee_actions_tracking.py b/cogs/committee_actions_tracking.py index 7fe39071d..edadddf99 100644 --- a/cogs/committee_actions_tracking.py +++ b/cogs/committee_actions_tracking.py @@ -193,15 +193,10 @@ async def autocomplete_get_action_status( ctx: "TeXBotAutocompleteContext", # noqa: ARG004 ) -> "AbstractSet[discord.OptionChoice] | AbstractSet[str]": """Autocomplete callable that provides the set of possible Status' of actions.""" - status_options: Sequence[tuple[str, str]] = AssignedCommitteeAction._meta.get_field( - "status" - ).choices # type: ignore[assignment] - - if not status_options: - logger.error("The autocomplete could not find any action Status'!") - return set() - - return {discord.OptionChoice(name=value, value=code) for code, value in status_options} + return { + discord.OptionChoice(name=str(status.label), value=status.value) + for status in AssignedCommitteeAction.Status + } @committee_actions.command( name="create", description="Adds a new action with the specified description." diff --git a/cogs/kill.py b/cogs/kill.py index 2ca4e3b37..51a7466df 100644 --- a/cogs/kill.py +++ b/cogs/kill.py @@ -86,19 +86,18 @@ async def kill(self, ctx: "TeXBotApplicationContext") -> None: interaction.type == discord.InteractionType.component # noqa: CAR180 and interaction.message.id == confirmation_message.id and ((committee_role in interaction.user.roles) if committee_role else True) - and "custom_id" in interaction.data - and interaction.data["custom_id"] in {"shutdown_confirm", "shutdown_cancel"} + and interaction.custom_id in {"shutdown_confirm", "shutdown_cancel"} ), ) - if button_interaction.data["custom_id"] == "shutdown_confirm": # type: ignore[index, typeddict-item] + if button_interaction.custom_id == "shutdown_confirm": await confirmation_message.edit( content="My battery is low and it's getting dark...", view=None, ) await self.bot.perform_kill_and_close(initiated_by_user=ctx.interaction.user) - if button_interaction.data["custom_id"] == "shutdown_cancel": # type: ignore[index, typeddict-item] + if button_interaction.custom_id == "shutdown_cancel": await confirmation_message.edit(content="Shutdown has been cancelled.", view=None) logger.info("Manual shutdown cancelled by %s.", ctx.interaction.user) return diff --git a/cogs/strike.py b/cogs/strike.py index 46fc40ec2..a10eacdae 100644 --- a/cogs/strike.py +++ b/cogs/strike.py @@ -280,12 +280,11 @@ async def _confirm_perform_moderation_action( interaction.type == discord.InteractionType.component # noqa: CAR180 and interaction.user == interaction_user and interaction.channel == button_callback_channel - and "custom_id" in interaction.data - and interaction.data["custom_id"] in {"yes_strike_member", "no_strike_member"} + and interaction.custom_id in {"yes_strike_member", "no_strike_member"} ), ) - if button_interaction.data["custom_id"] == "no_strike_member": # type: ignore[index, typeddict-item] + if button_interaction.custom_id == "no_strike_member": await button_interaction.edit_original_response( content=( "Aborted performing " @@ -296,7 +295,7 @@ async def _confirm_perform_moderation_action( ) return - if button_interaction.data["custom_id"] == "yes_strike_member": # type: ignore[index, typeddict-item] + if button_interaction.custom_id == "yes_strike_member": await perform_moderation_action( strike_user, actual_strike_amount, committee_member=interaction_user ) @@ -594,16 +593,12 @@ async def _confirm_manual_add_strike( # noqa: PLR0915 else (committee_role in interaction.user.roles) ) and interaction.channel == confirmation_message_channel - and "custom_id" in interaction.data - and interaction.data["custom_id"] + and interaction.custom_id in {"yes_out_of_sync_ban_member", "no_out_of_sync_ban_member"} ), ) - if ( - out_of_sync_ban_button_interaction.data["custom_id"] # type: ignore[index, typeddict-item] - == "no_out_of_sync_ban_member" - ): + if out_of_sync_ban_button_interaction.custom_id == "no_out_of_sync_ban_member": await out_of_sync_ban_confirmation_message.edit( content=( f"Aborted performing ban action upon {strike_user.mention}. " @@ -621,10 +616,7 @@ async def _confirm_manual_add_strike( # noqa: PLR0915 await out_of_sync_ban_confirmation_message.delete() return - if ( - out_of_sync_ban_button_interaction.data["custom_id"] # type: ignore[index, typeddict-item] - == "yes_out_of_sync_ban_member" - ): + if out_of_sync_ban_button_interaction.custom_id == "yes_out_of_sync_ban_member": await self._send_strike_user_message(strike_user, member_strikes) await main_guild.ban( strike_user, @@ -691,8 +683,7 @@ async def _confirm_manual_add_strike( # noqa: PLR0915 else (committee_role in interaction.user.roles) ) and interaction.channel == confirmation_message_channel - and "custom_id" in interaction.data - and interaction.data["custom_id"] + and interaction.custom_id in { "yes_manual_moderation_action", "no_manual_moderation_action", @@ -700,7 +691,7 @@ async def _confirm_manual_add_strike( # noqa: PLR0915 ), ) - if button_interaction.data["custom_id"] == "no_manual_moderation_action": # type: ignore[index, typeddict-item] + if button_interaction.custom_id == "no_manual_moderation_action": await confirmation_message.edit( content=( f"Aborted increasing {strike_user.mention}'s strikes " @@ -719,7 +710,7 @@ async def _confirm_manual_add_strike( # noqa: PLR0915 await confirmation_message.delete() return - if button_interaction.data["custom_id"] == "yes_manual_moderation_action": # type: ignore[index, typeddict-item] + if button_interaction.custom_id == "yes_manual_moderation_action": interaction_user: discord.User | None = self.bot.get_user(applied_action_user.id) if not interaction_user: raise StrikeTrackingError diff --git a/config.py b/config.py index 572fdea99..e51df72ff 100644 --- a/config.py +++ b/config.py @@ -13,7 +13,7 @@ import logging import os import re -from collections.abc import Iterable, Mapping +from collections.abc import Collection, Mapping from pathlib import Path from typing import TYPE_CHECKING, final @@ -466,16 +466,13 @@ def _setup_welcome_messages(cls) -> None: if "welcome_messages" not in messages_dict: raise MessagesJSONFileMissingKeyError(missing_key="welcome_messages") - WELCOME_MESSAGES_KEY_IS_VALID: Final[bool] = bool( - isinstance(messages_dict["welcome_messages"], Iterable) - and messages_dict["welcome_messages"] - ) - if not WELCOME_MESSAGES_KEY_IS_VALID: + welcome_messages: object = messages_dict["welcome_messages"] + if not isinstance(welcome_messages, Collection) or not welcome_messages: raise MessagesJSONFileValueError( - dict_key="welcome_messages", invalid_value=messages_dict["welcome_messages"] + dict_key="welcome_messages", invalid_value=welcome_messages ) - cls._settings["WELCOME_MESSAGES"] = set(messages_dict["welcome_messages"]) # type: ignore[call-overload] + cls._settings["WELCOME_MESSAGES"] = set(welcome_messages) @classmethod def _setup_roles_messages(cls) -> None: @@ -486,15 +483,13 @@ def _setup_roles_messages(cls) -> None: if "roles_messages" not in messages_dict: raise MessagesJSONFileMissingKeyError(missing_key="roles_messages") - ROLES_MESSAGES_KEY_IS_VALID: Final[bool] = isinstance( - messages_dict["roles_messages"], Iterable - ) and bool(messages_dict["roles_messages"]) - if not ROLES_MESSAGES_KEY_IS_VALID: + roles_messages: object = messages_dict["roles_messages"] + if not isinstance(roles_messages, Collection) or not roles_messages: raise MessagesJSONFileValueError( - dict_key="roles_messages", invalid_value=messages_dict["roles_messages"] + dict_key="roles_messages", invalid_value=roles_messages ) - cls._settings["ROLES_MESSAGES"] = set(messages_dict["roles_messages"]) # type: ignore[call-overload] + cls._settings["ROLES_MESSAGES"] = set(roles_messages) @classmethod def _setup_organisation_id(cls) -> None: diff --git a/utils/command_checks.py b/utils/command_checks.py index 8977e40ad..0960be092 100644 --- a/utils/command_checks.py +++ b/utils/command_checks.py @@ -10,7 +10,7 @@ from collections.abc import Awaitable, Callable, Sequence from typing import Concatenate - from discord.ext.commands import CheckFailure + from discord.ext.commands import Bot, Context from .tex_bot_base_cog import TeXBotBaseCog from .tex_bot_contexts import TeXBotApplicationContext @@ -66,11 +66,15 @@ async def _check(ctx: "TeXBotApplicationContext") -> bool: )(func) @classmethod - def is_interaction_user_in_main_guild_failure(cls, check: "CheckFailure") -> bool: + def is_interaction_user_in_main_guild_failure( + cls, check: "Callable[[Context[Bot]], bool]" + ) -> bool: """Whether the check failed due to the user not being in your Discord guild.""" - return bool(check.__name__ == cls.check_interaction_user_in_main_guild.__name__) # type: ignore[attr-defined] + return bool(check.__name__ == cls.check_interaction_user_in_main_guild.__name__) @classmethod - def is_interaction_user_has_committee_role_failure(cls, check: "CheckFailure") -> bool: + def is_interaction_user_has_committee_role_failure( + cls, check: "Callable[[Context[Bot]], bool]" + ) -> bool: """Whether the check failed due to the user not having the committee role.""" - return bool(check.__name__ == cls.check_interaction_user_has_committee_role.__name__) # type: ignore[attr-defined] + return bool(check.__name__ == cls.check_interaction_user_has_committee_role.__name__) diff --git a/utils/tex_bot.py b/utils/tex_bot.py index f86749fd6..fc8a14312 100644 --- a/utils/tex_bot.py +++ b/utils/tex_bot.py @@ -89,14 +89,12 @@ def main_guild(self) -> discord.Guild: Raises `GuildDoesNotExist` if the given ID does not link to a valid Discord guild. """ - MAIN_GUILD_EXISTS: Final[bool] = bool( - self._main_guild - and self._check_guild_accessible(settings["_DISCORD_MAIN_GUILD_ID"]) - ) - if not MAIN_GUILD_EXISTS: + if self._main_guild is None or not self._check_guild_accessible( + settings["_DISCORD_MAIN_GUILD_ID"] + ): raise GuildDoesNotExistError(guild_id=settings["_DISCORD_MAIN_GUILD_ID"]) - return self._main_guild # type: ignore[return-value] + return self._main_guild @property async def committee_role(self) -> discord.Role: