mirror of
https://code.lenaisten.de/Lenaisten/lenaverse-bot.git
synced 2024-11-22 23:13:01 +00:00
78 lines
2.1 KiB
Python
78 lines
2.1 KiB
Python
|
from enum import Enum, auto
|
||
|
|
||
|
import discord
|
||
|
from discord import ui
|
||
|
|
||
|
|
||
|
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
|
||
|
await interaction.user.send(self.content.value)
|
||
|
|
||
|
|
||
|
@discord.app_commands.command()
|
||
|
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())
|