Compare commits

...

2 commits

Author SHA1 Message Date
f64cfbf1c0 InfoCommands: info, fest, aktion 2023-11-20 13:55:20 +01:00
64cb7bd5a9 config rework, command_prefix, remove clutter
- _helpers.ev_command respects CONFIG.command_prefix
- restrict public replies to channel ids
2023-11-20 13:40:25 +01:00
6 changed files with 135 additions and 52 deletions

View file

@ -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,
)

View file

@ -3,7 +3,6 @@ import logging
import discord import discord
from . import post, verein from . import post, verein
from .commands import lsstuff
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -13,8 +12,6 @@ class LenaverseBot(discord.Client):
super().__init__(intents=discord.Intents.default()) super().__init__(intents=discord.Intents.default())
self.tree = discord.app_commands.CommandTree(self) self.tree = discord.app_commands.CommandTree(self)
self.tree.add_command(lsstuff)
commands = post.COMMANDS + verein.COMMANDS commands = post.COMMANDS + verein.COMMANDS
for command in commands: for command in commands:
self.tree.add_command(command) self.tree.add_command(command)

View file

@ -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)

View file

@ -1,9 +1,11 @@
import os import os
import tomllib import tomllib
from typing import Self from typing import Annotated, Self
import discord import discord
from pydantic import BaseModel from pydantic import BaseModel, StringConstraints
StrippedStr = Annotated[str, StringConstraints(strip_whitespace=True)]
class Post(BaseModel): class Post(BaseModel):
@ -26,14 +28,29 @@ class Post(BaseModel):
return channel return channel
class InfoCommand(BaseModel):
description: StrippedStr = "..."
content: StrippedStr = ""
class FileCommand(InfoCommand):
filename: str = ""
class ClubInfo(BaseModel): class ClubInfo(BaseModel):
linktree: str = "" channels: list[int]
join_file: str = "Aufnahmeantrag.pdf"
join_message: str = "" info: InfoCommand
linktree: InfoCommand
join: FileCommand
fest: InfoCommand
aktion: InfoCommand
class Config(BaseModel): class Config(BaseModel):
discord_token: str discord_token: str
command_prefix: str
post: Post post: Post
ev_info: ClubInfo ev_info: ClubInfo

View file

@ -4,6 +4,7 @@ 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__)
@ -92,8 +93,8 @@ class PostModal(ui.Modal, title="Post verfassen"):
) )
@discord.app_commands.command() @ev_command(name="post")
async def ev_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)
""" """
@ -118,5 +119,5 @@ async def ev_post(interaction: discord.Interaction) -> None:
COMMANDS = [ COMMANDS = [
ev_post, post,
] ]

View file

@ -1,46 +1,99 @@
import logging import logging
from pathlib import Path
import discord import discord
from lenaverse_bot import __file__ as module_file from ._helpers import ev_command, get_files_path
from .config import CONFIG from .config import CONFIG
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
def get_files_path() -> Path: def reply_private(interaction: discord.Interaction, name: str) -> bool:
module_path = Path(module_file) _logger.debug(f"User {interaction.user.name}({interaction.user.id}) used /{name}")
return interaction.channel_id not in CONFIG.ev_info.channels
if module_path.is_file():
module_path = module_path.parent
result = module_path / "files"
assert result.is_dir()
return result
@discord.app_commands.command() @ev_command(
async def ev_linktree(interaction: discord.Interaction) -> None: name="info",
_logger.debug(f"User {interaction.user.name}({interaction.user.id}) used /linktree") description=CONFIG.ev_info.info.description,
)
async def info(interaction: discord.Interaction) -> None:
"""
Allgemeine Infos zum Verein
"""
await interaction.response.send_message( await interaction.response.send_message(
content=CONFIG.ev_info.linktree.strip(), content=CONFIG.ev_info.info.content,
suppress_embeds=True, ephemeral=reply_private(interaction, "info"),
) )
@discord.app_commands.command() @ev_command(
async def ev_join(interaction: discord.Interaction) -> None: name="linktree",
_logger.debug(f"User {interaction.user.name}({interaction.user.id}) used /join") description=CONFIG.ev_info.linktree.description,
)
async def linktree(interaction: discord.Interaction) -> None:
"""
Links rund um den Verein
"""
await interaction.response.send_message( await interaction.response.send_message(
content=CONFIG.ev_info.join_message.strip(), content=CONFIG.ev_info.linktree.content,
file=discord.File(get_files_path() / CONFIG.ev_info.join_file), suppress_embeds=True,
ephemeral=reply_private(interaction, "linktree"),
)
@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.content,
file=discord.File(get_files_path() / CONFIG.ev_info.join.filename),
ephemeral=reply_private(interaction, "join"),
)
@ev_command(
name="fest",
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,
ephemeral=reply_private(interaction, "fest"),
)
@ev_command(
name="aktion",
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,
ephemeral=reply_private(interaction, "aktion"),
) )
COMMANDS = [ COMMANDS = [
ev_linktree, info,
ev_join, linktree,
join,
fest,
aktion,
] ]