Jörn-Michael Miehe
876d8fe20a
- _helpers.ev_command respects CONFIG.command_prefix - restrict public replies to channel ids
123 lines
3.6 KiB
Python
123 lines
3.6 KiB
Python
import logging
|
|
from enum import Enum, auto
|
|
|
|
import discord
|
|
from discord import ui
|
|
|
|
from ._helpers import ev_command
|
|
from .config import CONFIG
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
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):
|
|
post_content = f"{self.content.value}\n> Gepostet von <@{interaction.user.id}>"
|
|
|
|
# Vorschau mit Buttons
|
|
view = PostConfirm()
|
|
await interaction.response.send_message(
|
|
content=f"## Post-Vorschau\n\n{post_content}",
|
|
view=view,
|
|
# nur für ausführenden User
|
|
ephemeral=True,
|
|
)
|
|
|
|
# warten auf User-Entscheidung
|
|
if await view.wait() is True:
|
|
# Entscheidungszeit abgelaufen
|
|
await interaction.delete_original_response()
|
|
await interaction.user.send("Zeit abgelaufen, Post verworfen")
|
|
_logger.info(
|
|
f"User {interaction.user.name}({interaction.user.id}) timeout during /post"
|
|
)
|
|
|
|
elif view.action is Action.PUBLISH:
|
|
# Post veröffentlichen
|
|
_logger.info(
|
|
f"User {interaction.user.name}({interaction.user.id}) published a /post"
|
|
)
|
|
target = CONFIG.post.get_channel(interaction.client)
|
|
await target.send(post_content)
|
|
|
|
elif view.action is Action.ABORT:
|
|
# Post abbrechen
|
|
_logger.info(
|
|
f"User {interaction.user.name}({interaction.user.id}) aborted a /post"
|
|
)
|
|
|
|
|
|
@ev_command(name="post")
|
|
async def post(interaction: discord.Interaction) -> None:
|
|
"""
|
|
Einen Post im Lenaisten-Bereich verfassen (nur für ausgewählte Mitglieder verfügbar)
|
|
"""
|
|
|
|
if interaction.user.id in CONFIG.post.users:
|
|
# Verfassen-Dialog anzeigen
|
|
_logger.debug(
|
|
f"User {interaction.user.name}({interaction.user.id}) started a /post"
|
|
)
|
|
await interaction.response.send_modal(PostModal())
|
|
|
|
else:
|
|
# Zugriff verweigern
|
|
_logger.info(
|
|
f"User {interaction.user.name}({interaction.user.id}) tried to /post"
|
|
)
|
|
await interaction.response.send_message(
|
|
content="Du bist nicht berechtigt, den `/post`-Befehl zu benutzen!",
|
|
# nur für ausführenden User
|
|
ephemeral=True,
|
|
)
|
|
|
|
|
|
COMMANDS = [
|
|
post,
|
|
]
|