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

Discord机器人斜杠命令触发TypeError问题求助

解决Discord斜杠命令触发TypeError的问题

问题场景

开发Discord机器人时,创建了同时支持前缀调用和斜杠调用的info命令,前缀调用正常工作,但斜杠调用时触发TypeError。

相关代码

@slash.slash(name='info', description='information',
             options=[{"name": "category", "description": "category info", "type": 3, "required": True}])
@bot.command()
async def info(ctx, categoty: str = None):
    print(categoty)
    match categoty:
        case 'commands':
            await ctx.send(f"```{categoty}:
            \n    {setting['prefix']}hello
            \n    {setting['prefix']}top
            \n    {setting['prefix']}avatar
            \n    {setting['prefix']}balance
            \n    {setting['prefix']}reward
            \n    {setting['prefix']}take```")
        case 'help':
            await ctx.send("```information```")
        case None:
            await ctx.send(f"```info:
            \n    {setting['prefix']}info commands
            \n    {setting['prefix']}info help```")
        case _:
            await ctx.send("```commands not found```")

报错信息

An exception has occurred while executing command `info`:
Traceback (most recent call last):
  File "/Users/bogdan/PycharmProjects/discord-bot/venv/lib/python3.10/site-packages/discord_slash/client.py", line 1353, in invoke_command
    await func.invoke(ctx, **args)
  File "/Users/bogdan/PycharmProjects/discord-bot/venv/lib/python3.10/site-packages/discord_slash/model.py", line 210, in invoke
    return await self.func(*args, **kwargs)
  File "/Users/bogdan/PycharmProjects/discord-bot/venv/lib/python3.10/site-packages/discord/ext/commands/core.py", line 374, in __call__
    return await self.callback(*args, **kwargs)
TypeError: info() got an unexpected keyword argument 'category'

问题原因

斜杠命令选项中定义的参数名为category,但函数的参数名写成了**categoty**(拼写错误,少了一个字母r)。斜杠命令调用时会将category作为关键字参数传递给函数,但函数没有这个参数,因此抛出TypeError。

修复方案

  1. 将函数参数名从categoty修改为category,与斜杠命令选项中的名称保持一致
  2. 统一修改函数内所有使用该变量的地方(包括print、match语句中的引用)

修复后的代码

@slash.slash(name='info', description='information',
             options=[{"name": "category", "description": "category info", "type": 3, "required": True}])
@bot.command()
async def info(ctx, category: str = None):
    print(category)
    match category:
        case 'commands':
            await ctx.send(f"```{category}:
            \n    {setting['prefix']}hello
            \n    {setting['prefix']}top
            \n    {setting['prefix']}avatar
            \n    {setting['prefix']}balance
            \n    {setting['prefix']}reward
            \n    {setting['prefix']}take```")
        case 'help':
            await ctx.send("```information```")
        case None:
            await ctx.send(f"```info:
            \n    {setting['prefix']}info commands
            \n    {setting['prefix']}info help```")
        case _:
            await ctx.send("```commands not found```")

额外说明:斜杠命令中设置了required: True,用户调用斜杠命令时必须传入category参数,函数中的默认值None仅会在前缀调用且不传入参数时生效,这部分逻辑无需调整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 12:15:41