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

Pycord自定义/help斜杠命令仍触发CommandNotFound错误求助

问题解决:Discord斜杠命令/help无法触发且报错CommandNotFound

可能原因及修复步骤

1. 斜杠命令未完成同步

Discord的斜杠命令需要同步到服务器后才能被识别,新添加的命令必须执行同步操作:

  • 若使用discord.py v2.x,可添加同步命令代码:
@bot.command()
@commands.is_owner()
async def sync(ctx):
    synced = await bot.tree.sync()
    await ctx.send(f"已全局同步 {len(synced)} 个命令")

运行该命令后,等待数分钟让Discord完成同步,再尝试使用/help。

2. 机器人权限不足

确保机器人拥有必要权限:

  • 进入Discord开发者门户,在「OAuth2」→「URL生成」中,勾选applications.commands和bot权限,重新邀请机器人至服务器。
  • 在服务器内检查机器人角色,确保其拥有发送消息、嵌入链接等权限。

3. 命令冲突或代码错误

  • 检查是否存在同名前缀命令(如@bot.command(name="help")),若有需移除或重命名其中一个,避免冲突。
  • 确认代码无语法错误,需导入正确模块:
import discord
from discord.ext import commands
from discord import ApplicationContext
  • 确保机器人实例初始化正确:
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)

4. 本地测试同步优化

本地测试时全局同步可能有延迟,可同步到指定服务器加快生效:

@bot.command()
@commands.is_owner()
async def sync_guild(ctx):
    synced = await bot.tree.sync(guild=discord.Object(id=你的服务器ID))
    await ctx.send(f"已向当前服务器同步 {len(synced)} 个命令")

验证修复

完成上述操作后重启机器人,在服务器输入/help,即可触发自定义命令,发送包含用户提及和视频链接的嵌入消息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 21:46:12