From b11c39da187df44a2fa90cfa09b2a10b397ca7c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sun, 19 Nov 2023 17:32:12 +0100 Subject: [PATCH] /post command target and authorization --- .flake8 | 2 +- lenaverse_bot/core/config.py | 36 ++++++++++++++++++++++++++++++++++++ lenaverse_bot/core/post.py | 17 +++++++++++++++-- 3 files changed, 52 insertions(+), 3 deletions(-) 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, + )