You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用discord.py交互发送消息报错:缺少必填参数interaction

问题修复方案

你错误混淆了discord.py中前缀命令和交互(斜杠)命令的实现方式,导致参数不匹配报错。以下是两种针对性修复方案:

方案一:修复前缀命令

@bot.command()是前缀命令(通过$test触发),它的第一个参数必须是commands.Context,而非discord.Interaction。修改代码如下:

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(ctx: commands.Context):
    await ctx.send("Hello!")

bot.run('你的token')

运行后输入$test即可触发命令发送消息。

方案二:改为交互(斜杠)命令

如果确实需要使用交互功能(通过/test触发),需要使用@bot.tree.command()装饰器,并且同步命令到服务器:

import discord
from discord.ext import commands

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

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

@bot.event
async def on_ready():
    print(f'已登录为 {bot.user}')
    # 同步命令到指定服务器(测试推荐用服务器同步,全局同步需1小时左右生效)
    await bot.tree.sync(guild=discord.Object(id='你的服务器ID')) # 替换为你的服务器ID,可在Discord开发者模式下查看
    print("交互命令已同步")

@bot.tree.command(name="test", description="测试交互命令")
async def test(interaction: discord.Interaction):
    await interaction.response.send_message(content="Hello!")

bot.run('你的token')

错误原因说明

原代码中@bot.command()是前缀命令的装饰器,当你输入$test时,discord.py会自动传入commands.Context对象,但你的函数要求接收discord.Interaction类型参数,两者不匹配,因此抛出MissingRequiredArgument错误。

内容的提问来源于stack exchange,提问作者dere nunuyt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 14:44:56