discordpy 2.0 interaction 是一个必需的参数,目前缺失。

huangapple go评论36阅读模式
英文:

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

我认为你对基本概念有些混淆,因为你的代码混合了命令和交互的内容。&lt;br&gt;
如果你想创建按钮,就不能像 `@client.command` 那样以旧的命令方式编写。你需要使用 `应用命令` 或 `混合命令`(即交互和命令的混合方式)&lt;br&gt;
所以,你不应该同时有像 `(ctx: Context, interaction: Interaction)` 这样的内容。&lt;br&gt;
而且,传统上,你需要创建一个 `view` 类并在这个 `view` 类中创建一个按钮。然后,当你将消息或嵌入发送给用户时,你可以将这个视图附加到其中。&lt;br&gt;
如果你想在应用命令中多次回应用户,你不能通过 `interaction.response` 来做到(只能回应一次)。你可以使用 `interaction.followup()` 来进行后续的回应。[链接](https://discordpy.readthedocs.io/en/stable/interactions/api.html?highlight=followup#discord.Interaction.followup)&lt;br&gt;
关于视图示例,请参考 [此处](https://github.com/Rapptz/discord.py/tree/master/examples/views) 下的示例文件。&lt;br&gt;
关于斜杠命令(应用命令)示例,请参考 [此处](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. &lt;br&gt;
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)&lt;br&gt;
So, you should never have something like `(ctx: Context, interaction: Interaction)` together.&lt;br&gt;
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. &lt;br&gt;
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)&lt;br&gt; For view examples, see file example under [this](https://github.com/Rapptz/discord.py/tree/master/examples/views)&lt;br&gt; For slash command (application command) example, see [this](https://gist.github.com/AbstractUmbra/a9c188797ae194e592efe05fa129c57f) </details>

huangapple
  • 本文由 发表于 2023年2月18日 01:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定