From 9db3f25f91ef9d3bf8e66bf9f06e411f8e76ebc2 Mon Sep 17 00:00:00 2001 From: eesoymilk Date: Mon, 20 May 2024 02:25:00 +0800 Subject: [PATCH] chore: remove junk --- .env.example | 1 - .github/workflows/main.yml | 10 ---- bot.py | 51 +++++++-------------- extensions/help.py | 21 ++++----- extensions/waifu.py | 72 ++++++++++++++--------------- utils/waifu_im.py | 93 +++++++++++++++++++++----------------- 6 files changed, 111 insertions(+), 137 deletions(-) diff --git a/.env.example b/.env.example index e68ebdd..9a92a31 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,2 @@ TOKEN= -MONGODB_CONNECTION_STR= OPENAI_API_KEY= diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 49c0ebc..e01fe42 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -53,16 +53,6 @@ jobs: tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - # - name: Copy docker-compose.yml to Kamatera - # run: | - # echo "${{ secrets.KAMATERA_KEY }}" > private_key.pem - # chmod 600 private_key.pem - # scp -o StrictHostKeyChecking=no \ - # -i private_key.pem \ - # -P ${{ env.KAMATERA_PORT }} \ - # ./docker-compose.yml \ - # ${{ env.KAMATERA_USER }}@${{ secrets.KAMATERA_HOST }}:${{ env.KAMATERA_PATH }} - deploy: needs: build-and-push runs-on: ubuntu-latest diff --git a/bot.py b/bot.py index a90f295..0e7f396 100644 --- a/bot.py +++ b/bot.py @@ -16,35 +16,21 @@ log = get_lumberjack(__name__) initial_extensions = ( - 'extensions.help', - 'extensions.inspect', - 'extensions.emoji_kitchen', - 'extensions.waifu', - 'extensions.listeners', - 'extensions.reaction_poll', - 'extensions.soy_commands', - 'extensions.chatbot', - 'extensions.admin', + "extensions.help", + "extensions.inspect", + "extensions.emoji_kitchen", + "extensions.waifu", + "extensions.listeners", + "extensions.reaction_poll", + "extensions.soy_commands", + "extensions.chatbot", + "extensions.admin", ) -# TODO: more flexible command prefix - - -def _prefix_callable(bot: Bot, msg: Message): - user_id = bot.user.id - base = [f'<@!{user_id}> ', f'<@{user_id}> '] - if msg.guild is None: - base.append('!') - base.append('?') - else: - base.extend(bot.prefixes.get(msg.guild.id, ['?', '!'])) - return base - class Soybot(Bot): - def __init__(self, *args, **kwargs): - activity = Game(name='Your Mom') + activity = Game(name="πŸ₯› Drinking Soymilk πŸ₯›") intents = Intents( guilds=True, members=True, @@ -55,11 +41,7 @@ def __init__(self, *args, **kwargs): reactions=True, message_content=True, ) - super().__init__( - intents=intents, - activity=activity, - **kwargs - ) + super().__init__(intents=intents, activity=activity, **kwargs) async def setup_hook(self): self.bot_app_info = await self.application_info() @@ -70,9 +52,9 @@ async def setup_hook(self): for ext in initial_extensions: try: await self.load_extension(ext) - log.info(f'{ext} loaded') + log.info(f"{ext} loaded") except Exception as e: - log.exception(f'Failed to load extension {ext}.') + log.exception(f"Failed to load extension {ext}: {e}") self.tree.on_error = self.on_app_command_error await self.tree.set_translator(SoybotTranslator()) @@ -82,13 +64,12 @@ def owner(self) -> User: return self.bot_app_info.owner async def on_app_command_error( - self, - intx: Interaction, - err: AppCommandError + self, intx: Interaction, err: AppCommandError ): if isinstance(err, CommandOnCooldown): await intx.response.send_message( f'冷卻中...\nθ«‹η¨εΎŒ**{str(round(err.retry_after, 1)).rstrip("0").rstrip(".")}**秒再試', - ephemeral=True) + ephemeral=True, + ) else: log.exception(err) diff --git a/extensions/help.py b/extensions/help.py index c715f4d..f4aeece 100644 --- a/extensions/help.py +++ b/extensions/help.py @@ -3,26 +3,23 @@ from utils import get_lumberjack, cd_but_soymilk log = get_lumberjack(__name__) -help_attributes = { - 'aliases': ['h', 'helps'], - 'cooldown': cd_but_soymilk -} +help_attributes = {"aliases": ["h", "helps"], "cooldown": cd_but_soymilk} class SoyHelp(MinimalHelpCommand): async def send_bot_help(self, mapping: dict[Cog, list[Command]]): - embed = Embed(title='Help') + embed = Embed(title="Help") for cog, commands in mapping.items(): filtered = await self.filter_commands(commands, sort=True) command_signatures = [ self.get_command_signature(c) for c in filtered ] if command_signatures: - cog_name = getattr(cog, 'qualified_name', 'No Category') + cog_name = getattr(cog, "qualified_name", "No Category") embed.add_field( name=cog_name, - value='\n'.join(command_signatures), - inline=False + value="\n".join(command_signatures), + inline=False, ) channel = self.get_destination() @@ -30,13 +27,11 @@ async def send_bot_help(self, mapping: dict[Cog, list[Command]]): async def send_command_help(self, command: Command): embed = Embed(title=self.get_command_signature(command)) - embed.add_field(name='Help', value=command.help) + embed.add_field(name="Help", value=command.help) alias = command.aliases if alias: embed.add_field( - name='Aliases', - value=', '.join(alias), - inline=False + name="Aliases", value=", ".join(alias), inline=False ) channel = self.get_destination() @@ -56,7 +51,7 @@ def __init__(self, bot: Bot): bot.help_command.command_attrs = help_attributes bot.help_command.cog = self - def cog_unload(self): + async def cog_unload(self): self.bot.help_command = self._original_help_command diff --git a/extensions/waifu.py b/extensions/waifu.py index 1c67e3a..394b54b 100644 --- a/extensions/waifu.py +++ b/extensions/waifu.py @@ -20,34 +20,34 @@ log = get_lumberjack(__name__) -class WaifuGroup(Group, name='waifu'): - BASE_URL = 'https://api.waifu.im/' +class WaifuGroup(Group, name="waifu"): + BASE_URL = "https://api.waifu.im/" SFW_CHOICES = [ Choice(name=opt, value=tag) for opt, tag in { - 'waifu-sfw_maid': 'maid', - 'waifu-sfw_waifu': 'waifu', - 'waifu-sfw_marin-kitagawa': 'marin-kitagawa', - 'waifu-sfw_mori-calliope': 'mori-calliope', - 'waifu-sfw_raiden-shogun': 'raiden-shogun', - 'waifu-sfw_oppai': 'oppai', - 'waifu-sfw_selfies': 'selfies', - 'waifu-sfw_uniform': 'uniform', - 'waifu-sfw_kamisato-ayaka': 'kamisato-ayaka', + "waifu-sfw_maid": "maid", + "waifu-sfw_waifu": "waifu", + "waifu-sfw_marin-kitagawa": "marin-kitagawa", + "waifu-sfw_mori-calliope": "mori-calliope", + "waifu-sfw_raiden-shogun": "raiden-shogun", + "waifu-sfw_oppai": "oppai", + "waifu-sfw_selfies": "selfies", + "waifu-sfw_uniform": "uniform", + "waifu-sfw_kamisato-ayaka": "kamisato-ayaka", }.items() ] NSFW_CHOICES = [ Choice(name=opt, value=tag) for opt, tag in { - 'waifu-nsfw_hentai': 'hentai', - 'waifu-nsfw_milf': 'milf', - 'waifu-nsfw_oral': 'oral', - 'waifu-nsfw_paizuri': 'paizuri', - 'waifu-nsfw_ecchi': 'ecchi', - 'waifu-nsfw_ass': 'ass', - 'waifu-nsfw_ero': 'ero', + "waifu-nsfw_hentai": "hentai", + "waifu-nsfw_milf": "milf", + "waifu-nsfw_oral": "oral", + "waifu-nsfw_paizuri": "paizuri", + "waifu-nsfw_ecchi": "ecchi", + "waifu-nsfw_ass": "ass", + "waifu-nsfw_ero": "ero", }.items() ] @@ -56,35 +56,35 @@ async def _fetch( ) -> dict: params = dict() if is_nsfw: - params |= {'is_nsfw': 'true'} + params |= {"is_nsfw": "true"} if tag is not None: - params |= {'included_tags': tag} + params |= {"included_tags": tag} url = f'{urljoin(self.BASE_URL, "/search")}?{urlencode(params)}' async with cs.get(url) as resp: data = await resp.json() - image = data['images'][0] + image = data["images"][0] return image def _get_embed_view(self, title: str, image: dict) -> tuple[Embed, View]: - tags = [t['name'] for t in image['tags']] + tags = [t["name"] for t in image["tags"]] embed = ( Embed( title=title, - description=''.join([f'#{t}' for t in tags]), - color=Color.from_str(image['dominant_color']), - timestamp=datetime.fromisoformat(image['uploaded_at']), + description="".join([f"#{t}" for t in tags]), + color=Color.from_str(image["dominant_color"]), + timestamp=datetime.fromisoformat(image["uploaded_at"]), ) .set_image( - url=image['url'], + url=image["url"], ) - .set_footer(text='uploaded at') + .set_footer(text="uploaded at") ) view = View().add_item( Button( style=ButtonStyle.link, - url=image['source'], - label='ζŸ₯ηœ‹εœ–ζΊ', + url=image["source"], + label="ζŸ₯ηœ‹εœ–ζΊ", ) ) @@ -101,7 +101,7 @@ async def _run( tag = tag.value title = await intx.translate(tag) else: - title = await intx.translate(_T('random', shared=True)) + title = await intx.translate(_T("random", shared=True)) bot: Soybot = intx.client image = await self._fetch(bot.cs, tag=tag) @@ -109,10 +109,10 @@ async def _run( await intx.followup.send(embed=embed, view=view) except (KeyError, AttributeError) as err: log.exception(err) - await intx.followup.send(await intx.translate('error')) + await intx.followup.send(await intx.translate("error")) - @ac.command(name='waifu-sfw') - @ac.describe(tag='tag') + @ac.command(name="waifu-sfw") + @ac.describe(tag="tag") @ac.choices(tag=SFW_CHOICES) @ac.checks.dynamic_cooldown(cd_but_soymilk) async def sfw_coro( @@ -120,8 +120,8 @@ async def sfw_coro( ): await self._run(intx, tag, False) - @ac.command(name='waifu-nsfw') - @ac.describe(tag='tag') + @ac.command(name="waifu-nsfw") + @ac.describe(tag="tag") @ac.choices(tag=NSFW_CHOICES) @ac.checks.dynamic_cooldown(cd_but_soymilk) async def nsfw_coro( @@ -130,7 +130,7 @@ async def nsfw_coro( # manually check if the channel is nsfw due to discord limitation if not intx.channel.nsfw: return await intx.response.send_message( - await intx.translate('no_horny'), ephemeral=True + await intx.translate("no_horny"), ephemeral=True ) await self._run(intx, tag, True) diff --git a/utils/waifu_im.py b/utils/waifu_im.py index 9483bc8..64aae6d 100644 --- a/utils/waifu_im.py +++ b/utils/waifu_im.py @@ -1,4 +1,5 @@ from datetime import datetime +from typing import Optional from urllib.parse import urljoin, urlencode from discord import Embed, Color, ButtonStyle @@ -8,64 +9,72 @@ class WaifuIm: - BASE_URL = 'https://api.waifu.im/' + BASE_URL = "https://api.waifu.im/" - SFW_CHOICES = [Choice(name=opt, value=tag) for opt, tag in { - 'waifu-sfw_waifu': 'waifu', - 'waifu-sfw_uniform': 'uniform', - 'waifu-sfw_maid': 'maid', - 'waifu-sfw_mori-calliope': 'mori-calliope', - 'waifu-sfw_marin-kitagawa': 'marin-kitagawa', - 'waifu-sfw_raiden-shogun': 'raiden-shogun', - 'waifu-sfw_oppai': 'oppai', - 'waifu-sfw_selfies': 'selfies', - }.items()] + SFW_CHOICES = [ + Choice(name=opt, value=tag) + for opt, tag in { + "waifu-sfw_waifu": "waifu", + "waifu-sfw_uniform": "uniform", + "waifu-sfw_maid": "maid", + "waifu-sfw_mori-calliope": "mori-calliope", + "waifu-sfw_marin-kitagawa": "marin-kitagawa", + "waifu-sfw_raiden-shogun": "raiden-shogun", + "waifu-sfw_oppai": "oppai", + "waifu-sfw_selfies": "selfies", + }.items() + ] - NSFW_CHOICES = [Choice(name=opt, value=tag) for opt, tag in { - 'waifu-nsfw_hentai': 'hentai', - 'waifu-nsfw_milf': 'milf', - 'waifu-nsfw_oral': 'oral', - 'waifu-nsfw_paizuri': 'paizuri', - 'waifu-nsfw_ecchi': 'ecchi', - 'waifu-nsfw_ass': 'ass', - 'waifu-nsfw_ero': 'ero', - }.items()] + NSFW_CHOICES = [ + Choice(name=opt, value=tag) + for opt, tag in { + "waifu-nsfw_hentai": "hentai", + "waifu-nsfw_milf": "milf", + "waifu-nsfw_oral": "oral", + "waifu-nsfw_paizuri": "paizuri", + "waifu-nsfw_ecchi": "ecchi", + "waifu-nsfw_ass": "ass", + "waifu-nsfw_ero": "ero", + }.items() + ] @staticmethod async def fetch( - cs: ClientSession, - is_nsfw: bool = False, - tag: str = None + cs: ClientSession, is_nsfw: bool = False, tag: Optional[str] = None ) -> dict: params = dict() if is_nsfw: - params |= {'is_nsfw': 'true'} + params |= {"is_nsfw": "true"} if tag is not None: - params |= {'included_tags': tag} + params |= {"included_tags": tag} url = f'{urljoin(WaifuIm.BASE_URL, "/search")}?{urlencode(params)}' async with cs.get(url) as resp: data = await resp.json() - image = data['images'][0] + image = data["images"][0] return image - + @staticmethod def build_embed_view(title: str, image: dict) -> tuple[Embed, View]: - tags = [t['name'] for t in image['tags']] - embed = Embed( - title=title, - description=''.join([f'#{t}' for t in tags]), - color=Color.from_str(image['dominant_color']), - timestamp=datetime.fromisoformat(image['uploaded_at']), - ).set_image( - url=image['url'], - ).set_footer( - text='uploaded at' + tags = [t["name"] for t in image["tags"]] + embed = ( + Embed( + title=title, + description="".join([f"#{t}" for t in tags]), + color=Color.from_str(image["dominant_color"]), + timestamp=datetime.fromisoformat(image["uploaded_at"]), + ) + .set_image( + url=image["url"], + ) + .set_footer(text="uploaded at") + ) + view = View().add_item( + Button( + style=ButtonStyle.link, + url=image["source"], + label="ζŸ₯ηœ‹εœ–ζΊ", + ) ) - view = View().add_item(Button( - style=ButtonStyle.link, - url=image['source'], - label='ζŸ₯ηœ‹εœ–ζΊ', - )) return embed, view