1
0
Fork 0
mirror of https://code.lenaisten.de/Lenaisten/lenaverse-bot.git synced 2024-11-22 23:13:01 +00:00
lenaverse-bot/lenaverse_bot/commands/post.py

123 lines
3.6 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-12-02 22:56:11 +00:00
from ..core.config import CONFIG
2023-11-19 16:32:12 +00:00
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 18:12:06 +00:00
# Vorschau mit Buttons
2023-11-19 22:37:51 +00:00
view = PostConfirm()
2023-11-19 15:27:10 +00:00
await interaction.response.send_message(
2023-11-19 16:58:38 +00:00
content=f"## Post-Vorschau\n\n{post_content}",
2023-11-19 22:37:51 +00:00
view=view,
2023-11-19 15:27:10 +00:00
# nur für ausführenden User
ephemeral=True,
)
# warten auf User-Entscheidung
2023-11-19 22:37:51 +00:00
if await view.wait() is True:
# Entscheidungszeit abgelaufen
2023-11-20 00:27:33 +00:00
await interaction.delete_original_response()
await interaction.user.send("Zeit abgelaufen, Post verworfen")
2023-11-19 22:37:51 +00:00
_logger.info(
f"User {interaction.user.name}({interaction.user.id}) timeout during /post"
)
2023-11-19 15:27:10 +00:00
2023-11-19 22:37:51 +00:00
elif view.action is Action.PUBLISH:
2023-11-19 15:27:10 +00:00
# Post veröffentlichen
2023-11-19 16:58:38 +00:00
_logger.info(
2023-11-19 18:01:06 +00:00
f"User {interaction.user.name}({interaction.user.id}) published a /post"
2023-11-19 16:58:38 +00:00
)
2023-11-19 18:36:29 +00:00
target = CONFIG.post.get_channel(interaction.client)
2023-11-19 16:58:38 +00:00
await target.send(post_content)
elif view.action is Action.ABORT:
2023-11-19 22:37:51 +00:00
# Post abbrechen
2023-11-19 16:58:38 +00:00
_logger.info(
f"User {interaction.user.name}({interaction.user.id}) aborted a /post"
)
2023-11-19 15:27:10 +00:00
2023-11-22 22:44:55 +00:00
@discord.app_commands.command(name=CONFIG.command_prefix + "post")
async def post(interaction: discord.Interaction) -> None:
2023-11-19 15:27:10 +00:00
"""
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 18:01:06 +00:00
_logger.debug(
2023-11-19 16:58:38 +00:00
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 18:12:06 +00:00
# Zugriff verweigern
2023-11-19 23:26:23 +00:00
_logger.info(
2023-11-19 16:58:38 +00:00
f"User {interaction.user.name}({interaction.user.id}) tried to /post"
)
2023-11-19 16:32:12 +00:00
await interaction.response.send_message(
2023-12-02 23:00:16 +00:00
content=f"Du bist nicht berechtigt, den `/{CONFIG.command_prefix}post`-Befehl zu benutzen!",
2023-11-19 16:32:12 +00:00
# nur für ausführenden User
ephemeral=True,
)
2023-11-19 23:43:50 +00:00
COMMANDS = [
post,
2023-11-19 23:43:50 +00:00
]