英文:
discordpy 2.0 interaction is a required argument that is missing
问题
@client.command(brief="发送带有按钮的消息")
async def button(ctx, interaction: discord.Interaction):
view = discord.ui.View()
style = discord.ButtonStyle.gray
item = discord.ui.Button(style=style, label="查看文档!", url="https://discordpy.readthedocs.io/en/master")
view.add_item(item=item)
await interaction.response.send_message("这条消息带有按钮!", view=view)
await interaction.response.send_message(content="你好!", ephemeral=True)
discord.ext.commands.errors.MissingRequiredArgument: interaction 是一个必需的参数,但是缺失了。
英文:
@client.command(brief="Send a message with a button!")
async def button(ctx,interaction: discord.Interaction):
view = discord.ui.View()
style = discord.ButtonStyle.gray
item = discord.ui.Button(style=style, label="Read the docs!", url="https://discordpy.readthedocs.io/en/master")
view.add_item(item=item)
await interaction.response.send_message("This message has buttons!", view=view)
await interaction.response.send_message(content="Hi", ephemeral=True)
discord.ext.commands.errors.MissingRequiredArgument: interaction is a required argument that is missing.
Actually i just want to send a message by intraction, but seem itwasn't work :(
</details>
# 答案1
**得分**: 2
我认为你对基本概念有些混淆,因为你的代码混合了命令和交互的内容。<br>
如果你想创建按钮,就不能像 `@client.command` 那样以旧的命令方式编写。你需要使用 `应用命令` 或 `混合命令`(即交互和命令的混合方式)<br>
所以,你不应该同时有像 `(ctx: Context, interaction: Interaction)` 这样的内容。<br>
而且,传统上,你需要创建一个 `view` 类并在这个 `view` 类中创建一个按钮。然后,当你将消息或嵌入发送给用户时,你可以将这个视图附加到其中。<br>
如果你想在应用命令中多次回应用户,你不能通过 `interaction.response` 来做到(只能回应一次)。你可以使用 `interaction.followup()` 来进行后续的回应。[链接](https://discordpy.readthedocs.io/en/stable/interactions/api.html?highlight=followup#discord.Interaction.followup)<br>
关于视图示例,请参考 [此处](https://github.com/Rapptz/discord.py/tree/master/examples/views) 下的示例文件。<br>
关于斜杠命令(应用命令)示例,请参考 [此处](https://gist.github.com/AbstractUmbra/a9c188797ae194e592efe05fa129c57f)。
<details>
<summary>英文:</summary>
I think you are confused about the basic concepts since your code is mixed up command and interaction stuffs. <br>
If you want to create button, you cannot write in the older command way like `@client.command`. You need to either use `application command` or `hybrid command` (which is a mix of interaction and command)<br>
So, you should never have something like `(ctx: Context, interaction: Interaction)` together.<br>
Also, classically, you need to create a `view` class and create a button inside this `view` class. Then you can attach this view to your message or embed when you send this to the user. <br>
And, if you want to respond to user multiple times in a application command, you cannot do that through `interaction.response` (only once). You can use `interaction.followup()` for later responses. (https://discordpy.readthedocs.io/en/stable/interactions/api.html?highlight=followup#discord.Interaction.followup)<br>
For view examples, see file example under [this](https://github.com/Rapptz/discord.py/tree/master/examples/views)<br>
For slash command (application command) example, see [this](https://gist.github.com/AbstractUmbra/a9c188797ae194e592efe05fa129c57f)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论