forked from Pycord-Development/pycord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal_dialogs.py
85 lines (64 loc) · 3.02 KB
/
modal_dialogs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import discord
from discord.ext import commands
from discord.ui import InputText, Modal
class Bot(commands.Bot):
def __init__(self):
super().__init__(command_prefix=">")
bot = Bot()
class MyModal(Modal):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.add_item(InputText(label="Short Input", placeholder="Placeholder Test"))
self.add_item(
InputText(
label="Longer Input",
value="Longer Value\nSuper Long Value",
style=discord.InputTextStyle.long,
)
)
async def callback(self, interaction: discord.Interaction):
embed = discord.Embed(title="Your Modal Results", color=discord.Color.random())
embed.add_field(name="First Input", value=self.children[0].value, inline=False)
embed.add_field(name="Second Input", value=self.children[1].value, inline=False)
await interaction.response.send_message(embeds=[embed])
@bot.slash_command(name="modaltest", guild_ids=[...])
async def modal_slash(ctx):
"""Shows an example of a modal dialog being invoked from a slash command."""
modal = MyModal(title="Slash Command Modal")
await ctx.interaction.response.send_modal(modal)
@bot.message_command(name="messagemodal", guild_ids=[...])
async def modal_message(ctx, message):
"""Shows an example of a modal dialog being invoked from a message command."""
modal = MyModal(title="Message Command Modal")
modal.title = f"Modal for Message ID: {message.id}"
await ctx.interaction.response.send_modal(modal)
@bot.user_command(name="usermodal", guild_ids=[...])
async def modal_user(ctx, member):
"""Shows an example of a modal dialog being invoked from a user command."""
modal = MyModal(title="User Command Modal")
modal.title = f"Modal for User: {member.display_name}"
await ctx.interaction.response.send_modal(modal)
@bot.command()
async def modaltest(ctx):
"""Shows an example of modals being invoked from an interaction component (e.g. a button or select menu)"""
class MyView(discord.ui.View):
@discord.ui.button(label="Modal Test", style=discord.ButtonStyle.primary)
async def button_callback(self, button, interaction):
modal = MyModal(title="Modal Triggered from Button")
await interaction.response.send_modal(modal)
@discord.ui.select(
placeholder="Pick Your Modal",
min_values=1,
max_values=1,
options=[
discord.SelectOption(label="First Modal", description="Shows the first modal"),
discord.SelectOption(label="Second Modal", description="Shows the second modal"),
],
)
async def select_callback(self, select, interaction):
modal = MyModal(title="Temporary Title")
modal.title = select.values[0]
await interaction.response.send_modal(modal)
view = MyView()
await ctx.send("Click Button, Receive Modal", view=view)
bot.run("your token")