config rework, command_prefix, remove clutter

- _helpers.ev_command respects CONFIG.command_prefix
- restrict public replies to channel ids
This commit is contained in:
Jörn-Michael Miehe 2023-11-20 13:37:36 +01:00
parent 2978d7f98f
commit 64cb7bd5a9
6 changed files with 83 additions and 51 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
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)

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 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
join: FileCommand
class Config(BaseModel):
discord_token: str
command_prefix: str
post: Post
ev_info: ClubInfo

View file

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

View file

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