From eeefbcf5089f88738a1561031bed783e2eb40035 Mon Sep 17 00:00:00 2001 From: hawk-tomy <67221751+hawk-tomy@users.noreply.github.com> Date: Sat, 23 Mar 2024 18:49:46 +0900 Subject: [PATCH] add modal example --- example/modal.py | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 example/modal.py diff --git a/example/modal.py b/example/modal.py new file mode 100644 index 0000000..464e9b5 --- /dev/null +++ b/example/modal.py @@ -0,0 +1,102 @@ +from __future__ import annotations + +from os import getenv + +from discord import Client, Embed, Intents, Interaction +from discord.app_commands import CommandTree +from discord.ext.flow import Button, Controller, Message, ModalConfig, ModelBase, Result, TextInput, send_modal + + +class EmbedModel(ModelBase): + def __init__(self, title: str) -> None: + self.embed = Embed(title=title, description='') + + def message(self) -> Message: + return Message( + embeds=[self.embed], + items=( + self.edit_title_button(), + self.edit_description_button(), + self.edit_title_and_description_button(), + self.finish_button(), + ), + edit_original=True, + disable_items=True, + ephemeral=True, + ) + + def edit_title_button(self) -> Button: + async def inner(interaction: Interaction[Client]) -> Result: + texts, interaction = await send_modal( + interaction, + ModalConfig(title='Edit Title'), + (TextInput(label='title', default=self.embed.title),), + ) + assert len(texts) >= 1 + self.embed.title = texts[0] + return Result.send_message(message=self.message(), interaction=interaction) + + return Button(label='edit title', callback=inner) + + def edit_description_button(self) -> Button: + async def inner(interaction: Interaction[Client]) -> Result: + texts, interaction = await send_modal( + interaction, + ModalConfig(title='Edit Description'), + (TextInput(label='description', default=self.embed.description),), + ) + assert len(texts) >= 1 + self.embed.description = texts[0] + return Result.send_message(message=self.message(), interaction=interaction) + + return Button(label='edit description', callback=inner) + + def edit_title_and_description_button(self) -> Button: + async def inner(interaction: Interaction[Client]) -> Result: + texts, interaction = await send_modal( + interaction, + ModalConfig(title='Edit Title and Description'), + ( + TextInput(label='title', default=self.embed.title), + TextInput(label='description', default=self.embed.description), + ), + ) + assert len(texts) >= 2 + self.embed.title = texts[0] + self.embed.description = texts[1] + return Result.send_message(message=self.message(), interaction=interaction) + + return Button(label='edit title and description', callback=inner) + + def finish_button(self) -> Button: + async def inner(_: Interaction[Client]) -> Result: + return Result.send_message(message=Message(embeds=[self.embed])) + + return Button(label='finish', callback=inner) + + +class MyClient(Client): + def __init__(self) -> None: + super().__init__(intents=Intents.default()) + self.tree = CommandTree(self) + + async def setup_hook(self) -> None: + await self.tree.sync() + + +client = MyClient() + + +@client.event +async def on_ready() -> None: + assert client.user is not None + print(f'Logged in as {client.user} (ID: {client.user.id})') + print('------') + + +@client.tree.command(name='embed') +async def embed(interaction: Interaction[Client], title: str) -> None: + await Controller(EmbedModel(title)).invoke(interaction) + + +client.run(getenv('TOKEN', ''))