diff --git a/lenaverse_bot/core/_helpers.py b/lenaverse_bot/core/_helpers.py new file mode 100644 index 0000000..17b154f --- /dev/null +++ b/lenaverse_bot/core/_helpers.py @@ -0,0 +1,31 @@ +from pathlib import Path + +import discord +from discord.app_commands import locale_str +from discord.utils import MISSING + +from lenaverse_bot import __file__ as module_file + +from .config import CONFIG + + +def get_files_path() -> Path: + module_path = Path(module_file) + + if module_path.is_file(): + module_path = module_path.parent + + result = module_path / "files" + assert result.is_dir() + + return result + + +def ev_command( + name: str, + description: str | locale_str = MISSING, +): + return discord.app_commands.command( + name=CONFIG.command_prefix + name, + description=description, + ) diff --git a/lenaverse_bot/core/bot.py b/lenaverse_bot/core/bot.py index e06a1ba..8542c0b 100644 --- a/lenaverse_bot/core/bot.py +++ b/lenaverse_bot/core/bot.py @@ -3,7 +3,6 @@ import logging import discord from . import post, verein -from .commands import lsstuff _logger = logging.getLogger(__name__) @@ -13,8 +12,6 @@ class LenaverseBot(discord.Client): super().__init__(intents=discord.Intents.default()) self.tree = discord.app_commands.CommandTree(self) - self.tree.add_command(lsstuff) - commands = post.COMMANDS + verein.COMMANDS for command in commands: self.tree.add_command(command) diff --git a/lenaverse_bot/core/commands.py b/lenaverse_bot/core/commands.py deleted file mode 100644 index fe6b2e0..0000000 --- a/lenaverse_bot/core/commands.py +++ /dev/null @@ -1,16 +0,0 @@ -import discord - - -@discord.app_commands.command() -async def lsstuff(interaction: discord.Interaction): - msg = "" - for guild in interaction.client.guilds: - msg += f"\n- {guild.name}" - for channel in guild.channels: - msg += f"\n - {channel.name}" - - if isinstance(channel, discord.ForumChannel | discord.TextChannel): - for thread in channel.threads: - msg += f"\n - {thread.name}" - - await interaction.response.send_message(msg, ephemeral=True) diff --git a/lenaverse_bot/core/config.py b/lenaverse_bot/core/config.py index 6d0058c..66a22fe 100644 --- a/lenaverse_bot/core/config.py +++ b/lenaverse_bot/core/config.py @@ -1,9 +1,11 @@ import os import tomllib -from typing import Self +from typing import Annotated, Self import discord -from pydantic import BaseModel +from pydantic import BaseModel, StringConstraints + +StrippedStr = Annotated[str, StringConstraints(strip_whitespace=True)] class Post(BaseModel): @@ -26,14 +28,26 @@ class Post(BaseModel): return channel +class InfoCommand(BaseModel): + description: StrippedStr = "..." + content: StrippedStr = "" + + +class FileCommand(InfoCommand): + filename: str = "" + + class ClubInfo(BaseModel): - linktree: str = "" - join_file: str = "Aufnahmeantrag.pdf" - join_message: str = "" + channels: list[int] + + linktree: InfoCommand = InfoCommand() + join: FileCommand = FileCommand(filename="Aufnahmeantrag.pdf") class Config(BaseModel): discord_token: str + command_prefix: str + post: Post ev_info: ClubInfo diff --git a/lenaverse_bot/core/post.py b/lenaverse_bot/core/post.py index 894c888..2ba8cb6 100644 --- a/lenaverse_bot/core/post.py +++ b/lenaverse_bot/core/post.py @@ -4,6 +4,7 @@ from enum import Enum, auto import discord from discord import ui +from ._helpers import ev_command from .config import CONFIG _logger = logging.getLogger(__name__) @@ -92,8 +93,8 @@ class PostModal(ui.Modal, title="Post verfassen"): ) -@discord.app_commands.command() -async def ev_post(interaction: discord.Interaction) -> None: +@ev_command(name="post") +async def post(interaction: discord.Interaction) -> None: """ Einen Post im Lenaisten-Bereich verfassen (nur für ausgewählte Mitglieder verfügbar) """ @@ -118,5 +119,5 @@ async def ev_post(interaction: discord.Interaction) -> None: COMMANDS = [ - ev_post, + post, ] diff --git a/lenaverse_bot/core/verein.py b/lenaverse_bot/core/verein.py index 76f57b4..993db72 100644 --- a/lenaverse_bot/core/verein.py +++ b/lenaverse_bot/core/verein.py @@ -1,46 +1,51 @@ import logging -from pathlib import Path import discord -from lenaverse_bot import __file__ as module_file - +from ._helpers import ev_command, get_files_path from .config import CONFIG _logger = logging.getLogger(__name__) -def get_files_path() -> Path: - module_path = Path(module_file) - - if module_path.is_file(): - module_path = module_path.parent - - result = module_path / "files" - assert result.is_dir() - - return result +def reply_private(interaction: discord.Interaction, name: str) -> bool: + _logger.debug(f"User {interaction.user.name}({interaction.user.id}) used /{name}") + return interaction.channel_id not in CONFIG.ev_info.channels -@discord.app_commands.command() -async def ev_linktree(interaction: discord.Interaction) -> None: - _logger.debug(f"User {interaction.user.name}({interaction.user.id}) used /linktree") +@ev_command( + name="linktree", + description=CONFIG.ev_info.linktree.description, +) +async def linktree(interaction: discord.Interaction) -> None: + """ + Links rund um den Verein + """ + await interaction.response.send_message( - content=CONFIG.ev_info.linktree.strip(), + content=CONFIG.ev_info.linktree.content, suppress_embeds=True, + ephemeral=reply_private(interaction, "linktree"), ) -@discord.app_commands.command() -async def ev_join(interaction: discord.Interaction) -> None: - _logger.debug(f"User {interaction.user.name}({interaction.user.id}) used /join") +@ev_command( + name="join", + description=CONFIG.ev_info.join.description, +) +async def join(interaction: discord.Interaction) -> None: + """ + Wie und warum dem Verein beitreten + """ + await interaction.response.send_message( - content=CONFIG.ev_info.join_message.strip(), - file=discord.File(get_files_path() / CONFIG.ev_info.join_file), + content=CONFIG.ev_info.join.content, + file=discord.File(get_files_path() / CONFIG.ev_info.join.filename), + ephemeral=reply_private(interaction, "join"), ) COMMANDS = [ - ev_linktree, - ev_join, + linktree, + join, ]