1
0
Fork 0
mirror of https://code.lenaisten.de/Lenaisten/lenaverse-bot.git synced 2024-11-22 06:53:00 +00:00

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 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,26 @@ 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 = "" linktree: InfoCommand
join: FileCommand
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,51 @@
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="linktree",
_logger.debug(f"User {interaction.user.name}({interaction.user.id}) used /linktree") 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.linktree.strip(), content=CONFIG.ev_info.linktree.content,
suppress_embeds=True, suppress_embeds=True,
ephemeral=reply_private(interaction, "linktree"),
) )
@discord.app_commands.command() @ev_command(
async def ev_join(interaction: discord.Interaction) -> None: name="join",
_logger.debug(f"User {interaction.user.name}({interaction.user.id}) used /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( await interaction.response.send_message(
content=CONFIG.ev_info.join_message.strip(), content=CONFIG.ev_info.join.content,
file=discord.File(get_files_path() / CONFIG.ev_info.join_file), file=discord.File(get_files_path() / CONFIG.ev_info.join.filename),
ephemeral=reply_private(interaction, "join"),
) )
COMMANDS = [ COMMANDS = [
ev_linktree, linktree,
ev_join, join,
] ]