2023-11-22 22:44:55 +00:00
|
|
|
import functools
|
2023-11-19 23:26:23 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import discord
|
|
|
|
|
2023-12-02 22:56:11 +00:00
|
|
|
from ..core.config import CONFIG, FileCommand, InfoCommand
|
2023-11-19 23:26:23 +00:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-11-20 12:37:36 +00:00
|
|
|
def reply_private(interaction: discord.Interaction, name: str) -> bool:
|
|
|
|
_logger.debug(f"User {interaction.user.name}({interaction.user.id}) used /{name}")
|
2023-12-02 17:18:35 +00:00
|
|
|
|
|
|
|
# öffentliche Antwort in DM channels und in "allowed" channels
|
|
|
|
in_dm_channel = interaction.channel is not None and isinstance(
|
|
|
|
interaction.channel, discord.DMChannel
|
|
|
|
)
|
|
|
|
in_allowed_channel = interaction.channel_id in CONFIG.ev_info.channels
|
|
|
|
|
|
|
|
# private Antwort sonst
|
|
|
|
return not (in_dm_channel or in_allowed_channel)
|
2023-11-19 23:26:23 +00:00
|
|
|
|
|
|
|
|
2023-11-22 22:44:55 +00:00
|
|
|
@functools.singledispatch
|
|
|
|
def make_command(command) -> discord.app_commands.Command:
|
|
|
|
raise NotImplementedError
|
2023-11-19 23:26:23 +00:00
|
|
|
|
|
|
|
|
2023-11-22 22:44:55 +00:00
|
|
|
@make_command.register
|
|
|
|
def _(command: FileCommand) -> discord.app_commands.Command:
|
|
|
|
@discord.app_commands.command(
|
|
|
|
name=CONFIG.command_prefix + command.name,
|
|
|
|
description=command.description,
|
2023-11-22 21:37:30 +00:00
|
|
|
)
|
2023-11-22 22:44:55 +00:00
|
|
|
async def cmd(interaction: discord.Interaction) -> None:
|
|
|
|
if (file := await command.as_discord_file) is not None:
|
|
|
|
await interaction.response.send_message(
|
|
|
|
content=command.content,
|
|
|
|
suppress_embeds=True,
|
|
|
|
ephemeral=reply_private(interaction, command.name),
|
|
|
|
file=file,
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
await interaction.response.send_message(
|
|
|
|
content=CONFIG.command_failed,
|
|
|
|
ephemeral=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
|
|
|
@make_command.register
|
|
|
|
def _(command: InfoCommand) -> discord.app_commands.Command:
|
|
|
|
@discord.app_commands.command(
|
|
|
|
name=CONFIG.command_prefix + command.name,
|
|
|
|
description=command.description,
|
2023-11-20 12:55:20 +00:00
|
|
|
)
|
2023-11-22 22:44:55 +00:00
|
|
|
async def cmd(interaction: discord.Interaction) -> None:
|
|
|
|
await interaction.response.send_message(
|
|
|
|
content=command.content,
|
|
|
|
suppress_embeds=True,
|
|
|
|
ephemeral=reply_private(interaction, command.name),
|
|
|
|
)
|
2023-11-20 12:55:20 +00:00
|
|
|
|
2023-11-22 22:44:55 +00:00
|
|
|
return cmd
|
2023-11-20 12:55:20 +00:00
|
|
|
|
|
|
|
|
2023-11-19 23:43:50 +00:00
|
|
|
COMMANDS = [
|
2023-11-22 22:44:55 +00:00
|
|
|
make_command(attr)
|
|
|
|
for name in CONFIG.ev_info.model_dump().keys()
|
|
|
|
if isinstance(attr := getattr(CONFIG.ev_info, name), InfoCommand)
|
2023-11-19 23:43:50 +00:00
|
|
|
]
|