lenaverse-bot/lenaverse_bot/core/post.py

115 lines
3.3 KiB
Python
Raw Normal View History

2023-11-19 16:58:38 +00:00
import logging
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 16:58:38 +00:00
_logger = logging.getLogger(__name__)
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):
2023-11-19 16:58:38 +00:00
post_content = f"{self.content.value}\n> Gepostet von <@{interaction.user.id}>"
2023-11-19 15:27:10 +00:00
await interaction.response.send_message(
# Vorschau mit Buttons
2023-11-19 16:58:38 +00:00
content=f"## Post-Vorschau\n\n{post_content}",
2023-11-19 15:27:10 +00:00
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:58:38 +00:00
_logger.info(
f"User {interaction.user.name}({interaction.user.id}) finished a /post"
)
2023-11-19 16:32:12 +00:00
target = CONFIG.post.target.get(interaction.client)
2023-11-19 16:58:38 +00:00
await target.send(post_content)
elif view.action is Action.ABORT:
_logger.info(
f"User {interaction.user.name}({interaction.user.id}) aborted a /post"
)
elif view.action is Action.TIMEOUT:
_logger.info(
f"User {interaction.user.name}({interaction.user.id}) timeout during /post"
)
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
2023-11-19 16:58:38 +00:00
_logger.info(
f"User {interaction.user.name}({interaction.user.id}) started a /post"
)
2023-11-19 16:32:12 +00:00
await interaction.response.send_modal(PostModal())
else:
2023-11-19 16:58:38 +00:00
_logger.warning(
f"User {interaction.user.name}({interaction.user.id}) tried to /post"
)
2023-11-19 16:32:12 +00:00
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,
)