forked from smallshawn95/Python-Discord-Bot-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
151 lines (140 loc) · 4.43 KB
/
button.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import discord
from discord.ext import commands
intents = discord.Intents.all()
bot = commands.Bot(command_prefix = "$", intents = intents)
@bot.event
async def on_ready():
slash = await bot.tree.sync()
print(f"目前登入身份 --> {bot.user}")
print(f"載入 {len(slash)} 個斜線指令")
# 創建按鈕交互函式
async def button_callback(interaction: discord.Interaction):
await interaction.response.edit_message(content = "Hello, world!")
# 回呼函式(Callback)
@bot.tree.command(name = "button_interaction_callback", description = "Button 回呼函式交互")
async def button_interaction_callback(interaction: discord.Interaction):
view = discord.ui.View()
button = discord.ui.Button(
label = "Click",
style = discord.ButtonStyle.blurple
)
button.callback = button_callback
view.add_item(button)
await interaction.response.send_message(view = view)
# 創建自定 View
class ButtonView(discord.ui.View):
def __init__(self, timeout: float | None = 180):
super().__init__(timeout = timeout)
@discord.ui.button(
label = "Click",
style = discord.ButtonStyle.blurple
)
async def button_decorator(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.edit_message(content = "Hello, world!")
# 被裝飾函式(Decorator)
@bot.tree.command(name = "button_interaction_decorator", description = "Button 被裝飾函式交互")
async def button_interaction_decorator(interaction: discord.Interaction):
view = ButtonView()
await interaction.response.send_message(view = view)
# 監聽交互動作
@bot.event
async def on_interaction(self, interaction: discord.Interaction):
if "custom_id" in interaction.data:
if interaction.data["custom_id"] == "hello_world":
await interaction.response.edit_message(content = "Hello, world!")
# 持續監聽事件(Event Listener)
@bot.tree.command(name = "button_interaction_on", description = "Button 持續監聽事件交互")
async def button_interaction_on(interaction: discord.Interaction):
view = discord.ui.View()
view.add_item(
discord.ui.Button(
label = "Hello, world!",
style = discord.ButtonStyle.blurple,
custom_id = "hello_world"
)
)
await interaction.response.send_message(view = view)
@bot.tree.command(name = "button_style", description = "所有 Button 風格")
async def button_style(interaction: discord.Interaction):
view = discord.ui.View()
view.add_item(
discord.ui.Button(
label = "Primary",
style = discord.ButtonStyle.primary,
row = 0
)
)
view.add_item(
discord.ui.Button(
label = "Blurple",
style = discord.ButtonStyle.blurple,
row = 0
)
)
view.add_item(
discord.ui.Button(
label = "Secondary",
style = discord.ButtonStyle.secondary,
row = 1
)
)
view.add_item(
discord.ui.Button(
label = "Grey",
style = discord.ButtonStyle.grey,
row = 1
)
)
view.add_item(
discord.ui.Button(
label = "Gray",
style = discord.ButtonStyle.gray,
row = 1
)
)
view.add_item(
discord.ui.Button(
label = "Success",
style = discord.ButtonStyle.success,
row = 2
)
)
view.add_item(
discord.ui.Button(
label = "Green",
style = discord.ButtonStyle.green,
row = 2
)
)
view.add_item(
discord.ui.Button(
label = "Danger",
style = discord.ButtonStyle.danger,
row = 3
)
)
view.add_item(
discord.ui.Button(
label = "Red",
style = discord.ButtonStyle.red,
row = 3
)
)
view.add_item(
discord.ui.Button(
label = "link",
style = discord.ButtonStyle.link,
url = "https://hackmd.io/@smallshawn95/python_discord_bot_button",
row = 4
)
)
view.add_item(
discord.ui.Button(
label = "Url",
style = discord.ButtonStyle.url,
url = "https://hackmd.io/@smallshawn95/python_discord_bot_button",
row = 4
)
)
await interaction.response.send_message(view = view)
bot.run("BOT TOKEN")