Skip to content

Commit

Permalink
Defer responses to interactions
Browse files Browse the repository at this point in the history
Handles cases when the hosting server is slow and takes longer than 3s for a
command to run. In this case, Discord raises a 404 Not Found error.
  • Loading branch information
AbhijeetKrishnan committed Mar 13, 2024
1 parent 8444b89 commit c864be7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/framedb/framedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ def get_moves_by_move_type(self, character: CharacterName, move_type_query: str)
move_list = self.frames[character].movelist.values()
move_type = FrameDb._correct_move_type(move_type_query)
if move_type:
moves = list(filter(lambda x: (move_type.value.lower() in x.notes.lower()), move_list))
moves = list(
filter(lambda x: (move_type.value.lower() in x.notes.lower()), move_list)
) # TODO: revisit this logic for throws (and perhaps others)
else:
moves = []

Expand Down
26 changes: 15 additions & 11 deletions src/heihachi/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ async def on_ready(self) -> None:
self.add_view(button.DoneButton(action_channel))
logger.info(f"Logged on as {self.user}")

def _character_command_factory(self, name: str) -> Callable[[Interaction, str], Coroutine[Any, Any, None]]:
async def command(interaction: discord.Interaction, move: str) -> None:
def _character_command_factory(self, name: str) -> Callable[[Interaction["FrameDataBot"], str], Coroutine[Any, Any, None]]:
async def command(interaction: discord.Interaction["FrameDataBot"], move: str) -> None:
await interaction.response.defer()
if not (self._is_user_blacklisted(str(interaction.user.id)) or self._is_author_newly_created(interaction)):
logger.info(
f"Received character command from {interaction.user.name} in {interaction.guild}: /fd {name} {move}"
)
embed = get_frame_data_embed(self.framedb, self.frame_service, name, move)
await interaction.response.send_message(embed=embed, ephemeral=False)
await interaction.followup.send(embed=embed, ephemeral=False)

return command

Expand All @@ -78,7 +79,7 @@ def _is_user_blacklisted(self, user_id: str | int) -> bool:
else:
return False

def _is_author_newly_created(self, interaction: discord.Interaction) -> bool:
def _is_author_newly_created(self, interaction: discord.Interaction["FrameDataBot"]) -> bool:
"Check if author of an interaction is newly created"

today = datetime.datetime.strptime(datetime.datetime.now().isoformat(), "%Y-%m-%dT%H:%M:%S.%f")
Expand All @@ -98,7 +99,7 @@ async def on_message(self, message: discord.Message) -> None:
logger.debug(f"Message from {message.author.name} in {message.guild} is not a valid command")

async def _character_name_autocomplete(
self, interaction: discord.Interaction, current: str
self, interaction: discord.Interaction["FrameDataBot"], current: str
) -> List[discord.app_commands.Choice[str]]:
"""
Autocomplete function for character names
Expand All @@ -117,19 +118,21 @@ def _add_bot_commands(self) -> None:

@self.tree.command(name="fd", description="Frame data from a character move")
@discord.app_commands.autocomplete(character=self._character_name_autocomplete)
async def _frame_data_cmd(interaction: discord.Interaction, character: str, move: str) -> None:
async def _frame_data_cmd(interaction: discord.Interaction["FrameDataBot"], character: str, move: str) -> None:
logger.info(f"Received command from {interaction.user.name} in {interaction.guild}: /fd {character} {move}")
await interaction.response.defer()
character_name_query = character
move_query = move
if not (self._is_user_blacklisted(str(interaction.user.id)) or self._is_author_newly_created(interaction)):
embed = get_frame_data_embed(self.framedb, self.frame_service, character_name_query, move_query)
await interaction.response.send_message(embed=embed, ephemeral=False)
await interaction.followup.send(embed=embed, ephemeral=False)

if self.config.feedback_channel_id and self.config.action_channel_id:

@self.tree.command(name="feedback", description="Send feedback to the authors in case of incorrect data")
async def _feedback_cmd(interaction: discord.Interaction, message: str) -> None:
async def _feedback_cmd(interaction: discord.Interaction["FrameDataBot"], message: str) -> None:
logger.info(f"Received command from {interaction.user.name} in {interaction.guild}: /feedback {message}")
await interaction.response.defer()
if not (self._is_user_blacklisted(str(interaction.user.id)) or self._is_author_newly_created(interaction)):
# TODO: possible way to refactor these checks using discord.py library?
# discord.ext.commands.Bot.check()
Expand All @@ -154,13 +157,14 @@ async def _feedback_cmd(interaction: discord.Interaction, message: str) -> None:
except Exception as e:
result = embed.get_error_embed(f"Feedback couldn't be sent, caused by: {traceback.format_exc()}")

await interaction.response.send_message(embed=result, ephemeral=False)
await interaction.followup.send(embed=result, ephemeral=False)
else:
logger.warning("Feedback or Action channel ID is not set. Disabling feedback command.")

@self.tree.command(name="help", description="Show help")
async def _help_command(interaction: discord.Interaction) -> None:
async def _help_command(interaction: discord.Interaction["FrameDataBot"]) -> None:
logger.info(f"Received command from {interaction.user.name} in {interaction.guild}: /help")
await interaction.response.defer()
if not (self._is_user_blacklisted(str(interaction.user.id)) or self._is_author_newly_created(interaction)):
help_embed = embed.get_help_embed(self.frame_service)
await interaction.response.send_message(embed=help_embed, ephemeral=True)
await interaction.followup.send(embed=help_embed, ephemeral=True)

0 comments on commit c864be7

Please sign in to comment.