2023-11-19 15:27:10 +00:00
|
|
|
from enum import Enum, auto
|
|
|
|
|
|
|
|
import discord
|
|
|
|
from discord import ui
|
|
|
|
|
2023-11-19 16:32:12 +00:00
|
|
|
from .config import CONFIG
|
|
|
|
|
2023-11-19 15:27:10 +00:00
|
|
|
|
|
|
|
class Action(Enum):
|
|
|
|
"""
|
|
|
|
Was soll mit dem Post geschehen?
|
|
|
|
"""
|
|
|
|
|
|
|
|
PUBLISH = auto()
|
|
|
|
ABORT = auto()
|
|
|
|
TIMEOUT = auto()
|
|
|
|
|
|
|
|
|
|
|
|
class PostConfirm(ui.View):
|
|
|
|
"""
|
|
|
|
Buttons zum Veröffentlichen oder Verwerfen eines Posts
|
|
|
|
"""
|
|
|
|
|
|
|
|
action: Action = Action.TIMEOUT
|
|
|
|
|
|
|
|
async def __resolve(
|
|
|
|
self,
|
|
|
|
interaction: discord.Interaction,
|
|
|
|
msg: str,
|
|
|
|
action: Action,
|
|
|
|
) -> None:
|
|
|
|
await interaction.response.edit_message(content=msg, view=None)
|
|
|
|
self.action = action
|
|
|
|
self.stop()
|
|
|
|
|
|
|
|
@ui.button(label="Veröffentlichen", style=discord.ButtonStyle.success)
|
|
|
|
async def publish(self, interaction: discord.Interaction, button: ui.Button):
|
|
|
|
await self.__resolve(interaction, "Post wird veröffentlicht.", Action.PUBLISH)
|
|
|
|
|
|
|
|
@ui.button(label="Verwerfen", style=discord.ButtonStyle.danger)
|
|
|
|
async def abort(self, interaction: discord.Interaction, button: ui.Button):
|
|
|
|
await self.__resolve(interaction, "Post wird verworfen.", Action.ABORT)
|
|
|
|
|
|
|
|
|
|
|
|
class PostModal(ui.Modal, title="Post verfassen"):
|
|
|
|
"""
|
|
|
|
Eingabefeld zum Verfassen eines Postings
|
|
|
|
"""
|
|
|
|
|
|
|
|
content = ui.TextInput(
|
|
|
|
label="Inhalt",
|
|
|
|
style=discord.TextStyle.long,
|
|
|
|
placeholder="Post-Inhalt hier einfügen ...",
|
|
|
|
)
|
|
|
|
|
|
|
|
async def on_submit(self, interaction: discord.Interaction):
|
|
|
|
await interaction.response.send_message(
|
|
|
|
# Vorschau mit Buttons
|
|
|
|
content=f"## Post-Vorschau\n\n{self.content.value}",
|
|
|
|
view=(view := PostConfirm()),
|
|
|
|
# nur für ausführenden User
|
|
|
|
ephemeral=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# warten auf User-Entscheidung
|
|
|
|
await view.wait()
|
|
|
|
|
|
|
|
if view.action is Action.PUBLISH:
|
|
|
|
# Post veröffentlichen
|
2023-11-19 16:32:12 +00:00
|
|
|
target = CONFIG.post.target.get(interaction.client)
|
|
|
|
await target.send(self.content.value)
|
2023-11-19 15:27:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@discord.app_commands.command()
|
|
|
|
async def post(interaction: discord.Interaction):
|
|
|
|
"""
|
|
|
|
Einen Post im Lenaisten-Bereich verfassen (nur für ausgewählte Mitglieder verfügbar)
|
|
|
|
"""
|
|
|
|
|
2023-11-19 16:32:12 +00:00
|
|
|
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,
|
|
|
|
)
|