Skip to content

Commit

Permalink
add modal example
Browse files Browse the repository at this point in the history
  • Loading branch information
hawk-tomy committed Mar 23, 2024
1 parent 7fe9e66 commit eeefbcf
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions example/modal.py
Original file line number Diff line number Diff line change
@@ -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', ''))

0 comments on commit eeefbcf

Please sign in to comment.