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

Discord.py机器人playing命令双参数校验功能实现求助

问题原因

你当前的代码存在两个核心问题:

  • 你将args1、args2定义为必选位置参数,用户执行命令时如果没有传入对应数量的参数,Discord.py会直接抛出参数缺失错误,根本不会进入函数内部执行你写的判断逻辑
  • 函数内部的判断逻辑错误,不需要校验参数是否存在于消息原文中,直接判断参数是否被赋值即可

修复后的代码

@bot.command()
@commands.has_role("administrador") # 注意如果administrador是角色名需要用字符串包裹,是变量的话保持原样即可
async def playing(ctx, args1=None, args2=None):
    if args1 is None:
        await ctx.send("There is not a first argument")
        return
    if args2 is None:
        await ctx.send("There is not a second argument")
        return
    await ctx.send("It worked perfectly!")

补充说明

如果你的第二个参数允许包含空格,可以将参数标记为可选的消耗型参数,写法如下,这样用户输入的第二个参数即使带空格也会被完整识别:

async def playing(ctx, args1=None, *, args2=None):

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 16:18:02