Skip to content

Commit

Permalink
update Pre-commit stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TLNBS2405 committed Mar 11, 2024
1 parent f64c195 commit 8444b89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ classifiers = [
requires-python = ">= 3.10"
dependencies = [
"pre-commit",
"mypy",
"discord.py",
"beautifulsoup4",
"Requests",
Expand Down
36 changes: 19 additions & 17 deletions src/heihachi/bot.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import datetime
import logging
import traceback
from typing import List, Callable, Any, Coroutine
from typing import Any, Callable, Coroutine, List

import discord
import discord.ext.commands
from discord import Interaction

from framedb import FrameDb, FrameService
from framedb.const import CharacterName
from heihachi import button, embed
from heihachi.configurator import Configurator
from heihachi.embed import get_frame_data_embed
from framedb.const import CharacterName

logger = logging.getLogger("main")


class FrameDataBot(discord.Client):
def __init__(
self,
framedb: FrameDb,
frame_service: FrameService,
config: Configurator,
self,
framedb: FrameDb,
frame_service: FrameService,
config: Configurator,
) -> None:
intents = discord.Intents.default()
intents.message_content = False
Expand All @@ -35,12 +35,13 @@ def __init__(

self._add_bot_commands()
for char in CharacterName:
self.tree.command(name=char.value, description=f"Frame data from {char.value}")(self._character_command_factory(char.value))
self.tree.command(name=char.value, description=f"Frame data from {char.value}")(
self._character_command_factory(char.value)
)

logger.debug(f"Bot command tree: {[command.name for command in self.tree.get_commands()]}")

async def on_ready(self) -> None:

await self.wait_until_ready()
if not self.synced:
await self.tree.sync()
Expand All @@ -55,7 +56,9 @@ async def on_ready(self) -> None:
def _character_command_factory(self, name: str) -> Callable[[Interaction, str], Coroutine[Any, Any, None]]:
async def command(interaction: discord.Interaction, move: str) -> None:
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}")
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)

Expand Down Expand Up @@ -95,7 +98,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, current: str
) -> List[discord.app_commands.Choice[str]]:
"""
Autocomplete function for character names
Expand All @@ -106,11 +109,12 @@ async def _character_name_autocomplete(
current = current.lower() # autocomplete is case-sensitive
choices = self.framedb.autocomplete.search(word=current, max_cost=3, size=3)
return [discord.app_commands.Choice(name=choice[0].title(), value=choice[0]) for choice in choices][
:25
] # Discord has a max choice number of 25 (https://github.com/Rapptz/discord.py/discussions/9241)
:25
] # Discord has a max choice number of 25 (https://github.com/Rapptz/discord.py/discussions/9241)

def _add_bot_commands(self) -> None:
"Add all frame commands to the bot"

@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:
Expand All @@ -125,8 +129,7 @@ async def _frame_data_cmd(interaction: discord.Interaction, character: str, move

@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:
logger.info(
f"Received command from {interaction.user.name} in {interaction.guild}: /feedback {message}")
logger.info(f"Received command from {interaction.user.name} in {interaction.guild}: /feedback {message}")
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 @@ -149,8 +152,7 @@ async def _feedback_cmd(interaction: discord.Interaction, message: str) -> None:
await feedback_channel.send(content=feedback_message, view=button.DoneButton(actioned_channel))
result = embed.get_success_embed("Feedback sent")
except Exception as e:
result = embed.get_error_embed(
f"Feedback couldn't be sent, caused by: {traceback.format_exc()}")
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)
else:
Expand All @@ -161,4 +163,4 @@ async def _help_command(interaction: discord.Interaction) -> None:
logger.info(f"Received command from {interaction.user.name} in {interaction.guild}: /help")
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.response.send_message(embed=help_embed, ephemeral=True)

0 comments on commit 8444b89

Please sign in to comment.