From 8444b8914f38f775aa18750bed75225d10e3dfe5 Mon Sep 17 00:00:00 2001 From: lnguy Date: Mon, 11 Mar 2024 14:25:43 +0100 Subject: [PATCH] update Pre-commit stuff --- pyproject.toml | 1 + src/heihachi/bot.py | 36 +++++++++++++++++++----------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c1852f8..0f84438 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ classifiers = [ requires-python = ">= 3.10" dependencies = [ "pre-commit", + "mypy", "discord.py", "beautifulsoup4", "Requests", diff --git a/src/heihachi/bot.py b/src/heihachi/bot.py index 10e867b..351bceb 100644 --- a/src/heihachi/bot.py +++ b/src/heihachi/bot.py @@ -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 @@ -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() @@ -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) @@ -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 @@ -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: @@ -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() @@ -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: @@ -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) \ No newline at end of file + await interaction.response.send_message(embed=help_embed, ephemeral=True)