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

Pycord/discord.py斜杠命令不显示且报AttributeError错误求助

Discord机器人斜杠命令同步失败,报错AttributeError: 'NoneType' object has no attribute 'value'

问题情况

  • 首次遇到该问题,其他同源码机器人运行正常
  • 启动机器人后出现报错,机器人能正常启动但服务器内无法显示任何斜杠命令
  • 邀请链接已包含bot和applications.commands权限,机器人Intents已全部开启

报错信息

Traceback (most recent call last):
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\client.py", line 377, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\bot.py", line 1164, in on_connect
    await self.sync_commands()
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\bot.py", line 719, in sync_commands
    registered_commands = await self.register_commands(
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\bot.py", line 588, in register_commands
    data = [cmd["command"].to_dict() for cmd in filtered_deleted]
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\bot.py", line 588, in <listcomp>
    data = [cmd["command"].to_dict() for cmd in filtered_deleted]
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\commands\core.py", line 844, in to_dict
    "options": [o.to_dict() for o in self.options],
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\commands\core.py", line 844, in <listcomp>
    "options": [o.to_dict() for o in self.options],
  File "C:\Users\Overdrive\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\commands\options.py", line 318, in to_dict
    "type": self.input_type.value,
AttributeError: 'NoneType' object has no attribute 'value'

原代码

import discord
from discord.ext import commands
import os

intents=discord.Intents.all()
client=commands.Bot(intents=intents)

@client.event
async def on_ready():
    print(f'\n\nSuccessfully logged into Discord as "{client.user}"\nAwaiting user input...')
    await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name="all by myself..."))

@client.slash_command()
async def lol(interaction: discord.Interaction, ctx: discord.ApplicationContext):
    await interaction.response.send_message("LOL")

client.run(os.environ.get("TOKEN"))

解决方案

问题出在斜杠命令的参数定义上:ctx: discord.ApplicationContext是错误写法,Discord.py的斜杠命令不需要传入ApplicationContext参数,所有上下文信息已包含在interaction对象中。

修改后的斜杠命令函数:

@client.slash_command()
async def lol(interaction: discord.Interaction):
    await interaction.response.send_message("LOL")

原因解释

当斜杠命令中定义了无法被Discord.py识别为合法输入类型的参数时,会导致参数的input_type为None,同步命令时调用to_dict()就会触发AttributeError。ApplicationContext不属于斜杠命令的合法参数类型,因此引发了错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 16:40:32