mirror of
https://code.lenaisten.de/Lenaisten/lenaverse-bot.git
synced 2024-11-21 06:23:00 +00:00
/post command target and authorization
This commit is contained in:
parent
8d68a5bec8
commit
b11c39da18
3 changed files with 52 additions and 3 deletions
2
.flake8
2
.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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue