From 20da5bd078329d34ba0e8bc2b7c8e76e92169165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sat, 18 Nov 2023 21:52:07 +0100 Subject: [PATCH] Test dummy "post" app_command --- .devcontainer/devcontainer.json | 2 +- lenaverse_bot/core/bot.py | 23 +++++++++------- lenaverse_bot/core/commands.py | 48 ++++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 8ff93f0..98dcad5 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,7 +1,7 @@ // For format details, see https://aka.ms/devcontainer.json. For config options, see the // README at: https://github.com/devcontainers/templates/tree/main/src/python { - "name": "Python 3", + "name": "LEVbot", "build": { "dockerfile": "Dockerfile", "context": "..", diff --git a/lenaverse_bot/core/bot.py b/lenaverse_bot/core/bot.py index c51fc4f..a373773 100644 --- a/lenaverse_bot/core/bot.py +++ b/lenaverse_bot/core/bot.py @@ -1,27 +1,30 @@ import logging import discord -from discord.ext.commands import Bot -from .commands import foo, ping +from .commands import post _logger = logging.getLogger(__name__) -class LenaverseBot(Bot): +class LenaverseBot(discord.Client): def __init__(self) -> None: intents = discord.Intents.default() intents.message_content = True - super().__init__( - command_prefix="!", - intents=intents, - ) + super().__init__(intents=intents) - self.add_command(ping) - self.add_command(foo) + self.tree = discord.app_commands.CommandTree(self) + self.tree.add_command(post) + + async def setup_hook(self): + await self.tree.sync() + _logger.info("Commands synced") async def on_ready(self) -> None: _logger.info(self.guilds) - assert self.user is not None + + if self.user is None: + return + _logger.info(f"{self.user.name} has connected to Discord!") diff --git a/lenaverse_bot/core/commands.py b/lenaverse_bot/core/commands.py index 66e5c4c..f5b588a 100644 --- a/lenaverse_bot/core/commands.py +++ b/lenaverse_bot/core/commands.py @@ -1,11 +1,45 @@ -from discord.ext import commands +import discord +from discord import ui -@commands.hybrid_command() -async def ping(ctx: commands.Context): - await ctx.send("pong") +# 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() -@commands.hybrid_command() -async def foo(ctx: commands.Context, arg: str): - await ctx.send(arg) +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(f"Confirmed '{self.content.value}'") + + +@discord.app_commands.command() +async def post(interaction: discord.Interaction): + await interaction.response.send_modal(PostModal())