From 1353c76f3c4888d89a6ab776e21fcae06019864f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sun, 19 Nov 2023 16:27:10 +0100 Subject: [PATCH] /post command some polish --- lenaverse_bot/core/bot.py | 3 +- lenaverse_bot/core/commands.py | 44 ------------------- lenaverse_bot/core/post.py | 77 ++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 45 deletions(-) create mode 100644 lenaverse_bot/core/post.py diff --git a/lenaverse_bot/core/bot.py b/lenaverse_bot/core/bot.py index cf163c0..28fed11 100644 --- a/lenaverse_bot/core/bot.py +++ b/lenaverse_bot/core/bot.py @@ -2,7 +2,8 @@ import logging import discord -from .commands import lsstuff, post +from .commands import lsstuff +from .post import post _logger = logging.getLogger(__name__) diff --git a/lenaverse_bot/core/commands.py b/lenaverse_bot/core/commands.py index bde9a45..fe6b2e0 100644 --- a/lenaverse_bot/core/commands.py +++ b/lenaverse_bot/core/commands.py @@ -1,48 +1,4 @@ import discord -from discord import ui - - -# Define a simple View that gives us a confirmation menu -class Confirm(ui.View): - value: bool | None = None - - @ui.button(label="Confirm", style=discord.ButtonStyle.green) - async def confirm(self, interaction: discord.Interaction, button: ui.Button): - await interaction.response.edit_message(content="Confirming", view=None) - self.value = True - self.stop() - - @ui.button(label="Cancel", style=discord.ButtonStyle.grey) - async def cancel(self, interaction: discord.Interaction, button: ui.Button): - await interaction.response.edit_message(content="Cancelling", view=None) - self.value = False - self.stop() - - -class PostModal(ui.Modal, title="foobar2000"): - content = ui.TextInput( - label="Hier könnte Ihre Werbung stehen", - style=discord.TextStyle.long, - ) - - async def on_submit(self, interaction: discord.Interaction): - view = Confirm() - - await interaction.response.send_message( - content=f"## Vorschau\n\n{self.content.value}", - view=view, - ephemeral=True, - ) - - await view.wait() - - if view.value: - await interaction.user.send(self.content.value) - - -@discord.app_commands.command() -async def post(interaction: discord.Interaction): - await interaction.response.send_modal(PostModal()) @discord.app_commands.command() diff --git a/lenaverse_bot/core/post.py b/lenaverse_bot/core/post.py new file mode 100644 index 0000000..03b6d71 --- /dev/null +++ b/lenaverse_bot/core/post.py @@ -0,0 +1,77 @@ +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())