-
-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathmodal_dialogs.py
105 lines (86 loc) · 3.58 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# This example requires the `message_content` privileged intent for prefixed commands.
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(
command_prefix=commands.when_mentioned_or("!"), debug_guilds=[...], intents=intents
)
class MyModal(discord.ui.Modal):
def __init__(self, *args, **kwargs) -> None:
super().__init__(
discord.ui.InputText(
label="Short Input",
placeholder="Placeholder Test",
),
discord.ui.InputText(
label="Longer Input",
value="Longer Value\nSuper Long Value",
style=discord.InputTextStyle.long,
),
*args,
**kwargs,
)
async def callback(self, interaction: discord.Interaction):
embed = discord.Embed(
title="Your Modal Results",
fields=[
discord.EmbedField(
name="First Input", value=self.children[0].value, inline=False
),
discord.EmbedField(
name="Second Input", value=self.children[1].value, inline=False
),
],
color=discord.Color.random(),
)
await interaction.response.send_message(embeds=[embed])
@bot.slash_command(name="modaltest")
async def modal_slash(ctx: discord.ApplicationContext):
"""Shows an example of a modal dialog being invoked from a slash command."""
modal = MyModal(title="Slash Command Modal")
await ctx.send_modal(modal)
@bot.message_command(name="messagemodal")
async def modal_message(ctx: discord.ApplicationContext, message: discord.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.send_modal(modal)
@bot.user_command(name="usermodal")
async def modal_user(ctx: discord.ApplicationContext, member: discord.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.send_modal(modal)
@bot.command()
async def modaltest(ctx: commands.Context):
"""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: discord.ui.Button, interaction: discord.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: discord.ui.Select, interaction: discord.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("TOKEN")