diff --git a/.flake8 b/.flake8 index 666e9d4..e5084ca 100644 --- a/.flake8 +++ b/.flake8 @@ -1,4 +1,4 @@ [flake8] max-line-length = 80 select = C,E,F,I,W,B,B950 -extend-ignore = E203, E501 +extend-ignore = E203, E501, W503 diff --git a/lenaverse_bot/core/config.py b/lenaverse_bot/core/config.py index 8b5df2f..5f2e4a5 100644 --- a/lenaverse_bot/core/config.py +++ b/lenaverse_bot/core/config.py @@ -2,11 +2,47 @@ import os import tomllib from typing import Self +import discord from pydantic import BaseModel +class PostTarget(BaseModel): + server: str + channel: str + thread: str + + def get(self, client: discord.Client) -> discord.Thread: + """ + Zielkanal für Posts finden + """ + + guilds = [guild for guild in client.guilds if guild.name == self.server] + assert len(guilds) == 1 + + channels = [ + channel + for channel in guilds[0].channels + if isinstance(channel, discord.TextChannel | discord.ForumChannel) + and channel.name == self.channel + ] + assert len(channels) == 1 + + threads = [ + thread for thread in channels[0].threads if thread.name == self.thread + ] + assert len(threads) == 1 + + return threads[0] + + +class Post(BaseModel): + target: PostTarget + users: list[int] + + class Config(BaseModel): discord_token: str + post: Post @classmethod def get(cls) -> Self: diff --git a/lenaverse_bot/core/post.py b/lenaverse_bot/core/post.py index 03b6d71..2d21e4c 100644 --- a/lenaverse_bot/core/post.py +++ b/lenaverse_bot/core/post.py @@ -3,6 +3,8 @@ from enum import Enum, auto import discord from discord import ui +from .config import CONFIG + class Action(Enum): """ @@ -65,7 +67,8 @@ class PostModal(ui.Modal, title="Post verfassen"): if view.action is Action.PUBLISH: # Post veröffentlichen - await interaction.user.send(self.content.value) + target = CONFIG.post.target.get(interaction.client) + await target.send(self.content.value) @discord.app_commands.command() @@ -74,4 +77,14 @@ async def post(interaction: discord.Interaction): Einen Post im Lenaisten-Bereich verfassen (nur für ausgewählte Mitglieder verfügbar) """ - await interaction.response.send_modal(PostModal()) + if interaction.user.id in CONFIG.post.users: + # Verfassen-Dialog anzeigen + await interaction.response.send_modal(PostModal()) + + else: + await interaction.response.send_message( + # Zugriff verweigern + content="Du bist nicht berechtigt, den `/post`-Befehl zu benutzen!", + # nur für ausführenden User + ephemeral=True, + )