英文:
Disnake Bot Adding buttons to a message
问题
请告诉我如何向将发送到另一个频道的消息添加按钮。
示例:用户输入命令!nick [name]来更改名称。之后,将向管理频道发送带有a request to change the user's nickname的按钮消息。管理员点击“接受”或“拒绝”按钮,从而更改用户的昵称。
我知道如何将消息发送到另一个频道,但不知道如何向该消息添加按钮。
英文:
Please tell me how to add buttons to a message that will be sent to another channel.
Example: a user writes a command !nick [name] to change the name. After that, a message with buttons is sent to the administration channel with a request to change the user's nickname. The administrator clicks on the "Accept" or "Reject" button and thereby changes the user's nickname.
I know how to send a message to another channel, but I don't know how to add buttons to this message
答案1
得分: 1
I prefer the disnake.py package over discord.py because discord.py hadn't been fully upgraded when Discord rolled out Application Interactions. (For those that may know more, Danny had left discord.py and wasn't going to be upgrading it, so I learned disnake.py instead. Danny has since come back and done a tremendous job with discord.py, but I never switched back. They're very similar with only minor nuances. An understanding of discord.py should be proficient to understand the disnake.py coding)
This would be the last line, where the bot sends the buttons for the user to interact with. You'll see components
, you can also have content
for words to be displayed, embed
if you want to attach an embed to send, or even embeds
if you have a list of embeds you want to send (up to 10). Here I have just components
because I want you to see just how to add the buttons.
Each button has the minimum arguments to define the button. label
, style
, and custom_id
.
await inter.response.send_message(components=[
disnake.ui.Button(label="ACCEPT", style=disnake.ButtonStyle.green, custom_id="accept"),
disnake.ui.Button(label="REJECT", style=disnake.ButtonStyle.red, custom_id="reject")])
In addition to this code that displays the buttons, you will also need something that listens for the button clicks.
@yourbot.listen()
async def on_button_click(inter: disnake.MessageInteraction):
if inter.component.custom_id not in ["accept", "reject"]:
return
else:
if inter.component.custom_id == 'accept':
# Here is all your fancy code to do cool stuff
elif inter.component.custom_id == 'reject':
# Do coding stuff
英文:
EDIT: I prefer the disnake.py package over discord.py because discord.py hadn't been fully upgraded when Discord rolled out Application Interactions. (For those that may know more, Danny had left discord.py and wasn't going to be upgrading it, so I learned disnake.py instead. Danny has since come back and done a tremendous job with discord.py, but I never switched back. They're very similar with only minor nuances. An understanding of discord.py should be proficient to understand the disnake.py coding)
This would be the last line, where the bot sends the buttons for the user to interact with. You'll see components
, you can also have content
for words to be displayed, embed
if you want to attach an embed to send, or even embeds
if you have a list of embeds you want to send (up to 10). Here I have just components
because I want you to see just how to add the buttons.
Each button has the minimum arguments to define the button. label
, style
, and custom_id
.
await inter.response.send_message(components=[
disnake.ui.Button(label="ACCEPT", style=disnake.ButtonStyle.green, custom_id="accept"),
disnake.ui.Button(label="REJECT", style=disnake.ButtonStyle.red, custom_id="reject")])
In addition to this code that displays the buttons, you will also need something that listens for the button clicks.
@yourbot.listen()
async def on_button_click(inter: disnake.MessageInteraction):
if inter.component.custom_id not in ["accept", "reject"]:
return
else:
if inter.component.custom_id == 'accept':
*Here is all your fancy code to do cool stuff
elif inter.component.custom_id == 'reject':
*Do coding stuff
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论