discord.ext.commands.errors.MissingRequiredArgument: 缺少必需参数 interaction

-1 投票
1 回答
20 浏览
提问于 2025-04-13 14:52
import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix='$', intents=intents)


@bot.command()
async def test(interaction: discord.Interaction):
    await interaction.response.send_message(content="Hello!")

bot.run('token')

我尝试通过交互发送消息,但没有成功。
我遇到了这个错误:discord.ext.commands.errors.MissingRequiredArgument: interaction 是一个必需的参数,但缺失了。

我不知道该怎么解决这个问题。

1 个回答

0

你遇到的错误是因为 test 命令是用 @bot.command() 这个装饰器定义的,意思是它是一个普通命令。但是你却想把它当成交互命令(斜杠命令)来用,所以加了 interaction 这个参数。

要解决这个问题,你有两个选择:

  1. 如果你想把 test 命令当成普通命令,也就是通过消息触发的命令,那就把命令定义中的 interaction 参数去掉:
@bot.command()
async def test(ctx):
    await ctx.send(content="Hello!")

这样的话,当用户发送以 $test 开头的消息时(假设 $ 是你的命令前缀),这个命令就会被触发。

  1. 如果你想把 test 命令当成斜杠命令(交互命令),那你需要用 @bot.tree.command() 这个装饰器,而不是 @bot.command()
@bot.tree.command()
async def test(interaction: discord.Interaction):
    await interaction.response.send_message(content="Hello!")

bot.run('token')

这样的话,test 命令就会被注册为斜杠命令,用户可以通过在聊天中输入 /test 来触发它。

撰写回答