Skip to content

Commit

Permalink
Reduce code duplication by making a new private method for interactio…
Browse files Browse the repository at this point in the history
…n check failures to respond
  • Loading branch information
TrustyJAID committed May 6, 2024
1 parent 83758b5 commit f7137e0
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions redbot/core/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,43 +334,36 @@ async def on_error(
else:
log.exception(type(error).__name__, exc_info=error)

async def _send_interaction_check_failure(
self, interaction: discord.Interaction, message: str
):
"""Handles responding to interaction check failures.
Mainly used for when an interaction is an autocomplete and
providing the message in the autocomplete response.
"""
if interaction.type is discord.InteractionType.autocomplete:
await interaction.response.autocomplete(
[discord.app_commands.Choice(name=message[:80], value="None")]
)
return
await interaction.response.send_message(message, ephemeral=True)

async def interaction_check(self, interaction: discord.Interaction):
"""Global checks for app commands."""
if interaction.user.bot:
return False

if interaction.guild:
if not (await self.client.ignored_channel_or_guild(interaction)):
if interaction.type is discord.InteractionType.autocomplete:
await interaction.response.autocomplete(
[
discord.app_commands.Choice(
name=_("This channel or server is ignored."), value="None"
)
]
)
return False
await interaction.response.send_message(
"This channel or server is ignored.", ephemeral=True
await self._send_interaction_check_failure(
interaction, _("This channel or server is ignored.")
)
return False

if not (await self.client.allowed_by_whitelist_blacklist(interaction.user)):
if interaction.type is discord.InteractionType.autocomplete:
await interaction.response.autocomplete(
[
discord.app_commands.Choice(
name=_(
"You are not permitted to use commands because of an allowlist or blocklist."
),
value="None",
)
]
)
return False
await interaction.response.send_message(
"You are not permitted to use commands because of an allowlist or blocklist.",
ephemeral=True,
await self._send_interaction_check_failure(
interaction,
_("You are not permitted to use commands because of an allowlist or blocklist."),
)
return False

Expand Down

0 comments on commit f7137e0

Please sign in to comment.