/post command some polish

This commit is contained in:
Jörn-Michael Miehe 2023-11-19 16:27:10 +01:00
parent 8a328270cd
commit 1353c76f3c
3 changed files with 79 additions and 45 deletions

View file

@ -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__)

View file

@ -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()

View file

@ -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())