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

discord.py举报命令报错MissingRequiredArgument,求参数使用指导

问题解决与参数说明

错误原因

你的report参数被定义为必填项,当用户仅输入命令前缀r!report而未填写举报内容时,discord.py会直接抛出缺失必填参数的错误,不会进入你写的if report == None分支。

修复步骤

1. 修改参数为可选类型

将命令的参数定义改为带默认值的可选参数,这样用户未输入内容时report会自动取None,触发你的错误提示:

async def report(ctx, *, report: str = None):

这里的*是关键,它表示捕获命令后所有的文本内容作为单个参数(包括空格),确保用户的长举报内容能被完整接收。

2. 修复变量名冲突

你在成功分支里把Embed对象赋值给了report变量,导致后续add_field里的value引用的是Embed对象而非用户输入的举报内容。把Embed的变量名换成其他名称,比如report_embed。

修复后完整代码

@client.command()
async def report(ctx, *, report: str = None):
    # 基础信息获取
    user = ctx.author
    server = ctx.guild.name
    channel = client.get_channel(1009201586586783785)
    
    # 处理举报内容
    if report is None:
        await ctx.send("Enter your report! example : r!report {your report here}")
    else:
        await ctx.send("We have sent your report to **DisReport Hub** to be reviewed!")
        # 创建Embed消息,避免变量名冲突
        report_embed = discord.Embed(title=f"{user.name} sent a report!", description="-----", color=discord.Colour.blurple())
        report_embed.add_field(name=f"Report from - {server}: ", value=report)
        await channel.send(embed=report_embed)

参数使用说明

  • ctx:命令上下文,包含了消息发送者、服务器、频道等核心信息,是discord.py命令的必填参数。
  • *:位置参数捕获符,用于将命令后的所有文本合并为一个参数,适合需要接收长文本的场景(比如举报内容)。
  • report: str = None:将举报内容参数设为可选字符串类型,默认值为None,既避免了缺失参数错误,又能处理用户未输入内容的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 21:30:54