/post command target and authorization

This commit is contained in:
Jörn-Michael Miehe 2023-11-19 17:32:12 +01:00
parent 8d68a5bec8
commit b11c39da18
3 changed files with 52 additions and 3 deletions

View file

@ -1,4 +1,4 @@
[flake8] [flake8]
max-line-length = 80 max-line-length = 80
select = C,E,F,I,W,B,B950 select = C,E,F,I,W,B,B950
extend-ignore = E203, E501 extend-ignore = E203, E501, W503

View file

@ -2,11 +2,47 @@ import os
import tomllib import tomllib
from typing import Self from typing import Self
import discord
from pydantic import BaseModel 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): class Config(BaseModel):
discord_token: str discord_token: str
post: Post
@classmethod @classmethod
def get(cls) -> Self: def get(cls) -> Self:

View file

@ -3,6 +3,8 @@ from enum import Enum, auto
import discord import discord
from discord import ui from discord import ui
from .config import CONFIG
class Action(Enum): class Action(Enum):
""" """
@ -65,7 +67,8 @@ class PostModal(ui.Modal, title="Post verfassen"):
if view.action is Action.PUBLISH: if view.action is Action.PUBLISH:
# Post veröffentlichen # 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() @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) 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,
)