code deduplication

This commit is contained in:
Jörn-Michael Miehe 2023-11-22 23:44:55 +01:00
parent 29af971c39
commit 320126fc23
3 changed files with 43 additions and 128 deletions

View file

@ -1,15 +0,0 @@
import discord
from discord.app_commands import locale_str
from discord.utils import MISSING
from .config import CONFIG
def ev_command(
name: str,
description: str | locale_str = MISSING,
):
return discord.app_commands.command(
name=CONFIG.command_prefix + name,
description=description,
)

View file

@ -4,7 +4,6 @@ from enum import Enum, auto
import discord import discord
from discord import ui from discord import ui
from ._helpers import ev_command
from .config import CONFIG from .config import CONFIG
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -93,7 +92,7 @@ class PostModal(ui.Modal, title="Post verfassen"):
) )
@ev_command(name="post") @discord.app_commands.command(name=CONFIG.command_prefix + "post")
async def post(interaction: discord.Interaction) -> None: async def post(interaction: discord.Interaction) -> None:
""" """
Einen Post im Lenaisten-Bereich verfassen (nur für ausgewählte Mitglieder verfügbar) Einen Post im Lenaisten-Bereich verfassen (nur für ausgewählte Mitglieder verfügbar)

View file

@ -1,9 +1,9 @@
import functools
import logging import logging
import discord import discord
from ._helpers import ev_command from .config import CONFIG, FileCommand, InfoCommand
from .config import CONFIG
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -13,122 +13,53 @@ def reply_private(interaction: discord.Interaction, name: str) -> bool:
return interaction.channel_id not in CONFIG.ev_info.channels return interaction.channel_id not in CONFIG.ev_info.channels
@ev_command( @functools.singledispatch
name=CONFIG.ev_info.info.name, def make_command(command) -> discord.app_commands.Command:
description=CONFIG.ev_info.info.description, raise NotImplementedError
)
async def info(interaction: discord.Interaction) -> None:
"""
Allgemeine Infos zum Verein
"""
@make_command.register
def _(command: FileCommand) -> discord.app_commands.Command:
@discord.app_commands.command(
name=CONFIG.command_prefix + command.name,
description=command.description,
)
async def cmd(interaction: discord.Interaction) -> None:
if (file := await command.as_discord_file) is not None:
await interaction.response.send_message( await interaction.response.send_message(
content=CONFIG.ev_info.info.content, content=command.content,
suppress_embeds=True, suppress_embeds=True,
ephemeral=reply_private(interaction, CONFIG.ev_info.info.name), ephemeral=reply_private(interaction, command.name),
file=file,
) )
else:
@ev_command(
name=CONFIG.ev_info.vorstand.name,
description=CONFIG.ev_info.vorstand.description,
)
async def vorstand(interaction: discord.Interaction) -> None:
"""
Wer ist im Vereinsvorstand?
"""
await interaction.response.send_message( await interaction.response.send_message(
content=CONFIG.ev_info.vorstand.content, content=CONFIG.command_failed,
silent=True, ephemeral=True,
suppress_embeds=True,
ephemeral=reply_private(interaction, CONFIG.ev_info.vorstand.name),
) )
return cmd
@ev_command(
name=CONFIG.ev_info.linktree.name,
description=CONFIG.ev_info.linktree.description,
)
async def linktree(interaction: discord.Interaction) -> None:
"""
Links rund um den Verein
"""
@make_command.register
def _(command: InfoCommand) -> discord.app_commands.Command:
@discord.app_commands.command(
name=CONFIG.command_prefix + command.name,
description=command.description,
)
async def cmd(interaction: discord.Interaction) -> None:
await interaction.response.send_message( await interaction.response.send_message(
content=CONFIG.ev_info.linktree.content, content=command.content,
suppress_embeds=True, suppress_embeds=True,
ephemeral=reply_private(interaction, CONFIG.ev_info.linktree.name), ephemeral=reply_private(interaction, command.name),
) )
return cmd
@ev_command(
name=CONFIG.ev_info.beitreten.name,
description=CONFIG.ev_info.beitreten.description,
)
async def beitreten(interaction: discord.Interaction) -> None:
"""
Wie und warum dem Verein beitreten
"""
await interaction.response.send_message(
content=CONFIG.ev_info.beitreten.content,
suppress_embeds=True,
ephemeral=reply_private(interaction, CONFIG.ev_info.beitreten.name),
)
# if (file := await CONFIG.ev_info.beitreten.as_discord_file) is not None:
# await interaction.response.send_message(
# content=CONFIG.ev_info.beitreten.content,
# file=file,
# suppress_embeds=True,
# ephemeral=reply_private(interaction, CONFIG.ev_info.beitreten.name),
# )
# else:
# await interaction.response.send_message(
# content=CONFIG.command_failed,
# ephemeral=True,
# )
@ev_command(
name=CONFIG.ev_info.fest.name,
description=CONFIG.ev_info.fest.description,
)
async def fest(interaction: discord.Interaction) -> None:
"""
Infos zum nächsten Vereinsfest
"""
await interaction.response.send_message(
content=CONFIG.ev_info.fest.content,
suppress_embeds=True,
ephemeral=reply_private(interaction, CONFIG.ev_info.fest.name),
)
@ev_command(
name=CONFIG.ev_info.aktion.name,
description=CONFIG.ev_info.aktion.description,
)
async def aktion(interaction: discord.Interaction) -> None:
"""
Infos zu aktuellen Aktionen
"""
await interaction.response.send_message(
content=CONFIG.ev_info.aktion.content,
suppress_embeds=True,
ephemeral=reply_private(interaction, CONFIG.ev_info.aktion.name),
)
COMMANDS = [ COMMANDS = [
info, make_command(attr)
vorstand, for name in CONFIG.ev_info.model_dump().keys()
linktree, if isinstance(attr := getattr(CONFIG.ev_info, name), InfoCommand)
beitreten,
fest,
aktion,
] ]