Repository navigation
Expand file tree
/
Copy pathcomponents_v2_example.py
More file actions
85 lines (71 loc) · 2.76 KB
/
Copy pathcomponents_v2_example.py
File metadata and controls
85 lines (71 loc) · 2.76 KB
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
@nightyScript(
name="Components V2 Example",
author="rico",
description="Native Components V2 counter with buttons",
usage="/counter"
)
def components_v2_example():
"""
COMPONENTS V2 EXAMPLE
COMMANDS:
/counter - Open a private counter with increment and reset buttons
"""
async def send_v2(interaction, view, private=None):
if private is None:
private = bool(getConfigData().get("private"))
if not interaction.response.is_done():
return await interaction.response.send_message(
view=view,
ephemeral=private
)
deferred = (
interaction.response.type
== discord_bot.InteractionResponseType.deferred_channel_message
)
if deferred and not interaction.extras.get("v2_placeholder_used"):
interaction.extras["v2_placeholder_used"] = True
return await interaction.edit_original_response(view=view)
return await interaction.followup.send(view=view, ephemeral=private)
class CounterView(discord_bot.ui.LayoutView):
def __init__(self):
super().__init__(timeout=300)
self.count = 0
self.render()
def render(self):
self.clear_items()
plus = discord_bot.ui.Button(
label="+1",
style=discord_bot.ButtonStyle.success
)
reset = discord_bot.ui.Button(
label="Reset",
style=discord_bot.ButtonStyle.secondary
)
plus.callback = self.on_plus
reset.callback = self.on_reset
container = discord_bot.ui.Container(accent_color=0x5865F2)
container.add_item(
discord_bot.ui.Section(
discord_bot.ui.TextDisplay(
f"## Counter\nCurrent value: **{self.count}**"
),
accessory=discord_bot.ui.Thumbnail(
str(bot.user.display_avatar.url)
)
)
)
container.add_item(discord_bot.ui.Separator())
container.add_item(discord_bot.ui.ActionRow(plus, reset))
self.add_item(container)
async def on_plus(self, interaction):
self.count += 1
self.render()
await interaction.response.edit_message(view=self)
async def on_reset(self, interaction):
self.count = 0
self.render()
await interaction.response.edit_message(view=self)
@slashCommand(name="counter", description="Counter with buttons")
async def counter(interaction: discord_bot.Interaction):
await send_v2(interaction, CounterView())
components_v2_example()