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

Discord.NET创建的Slash命令无法显示问题求助

解决Discord Slash命令不显示的问题

针对你遇到的Slash命令无法显示的情况,按以下步骤排查:

1. 修正服务器ID的使用方式

注册服务器命令时,RegisterCommandsToGuildAsync的参数需要是ulong类型的服务器ID,不能直接传字符串"my server id"。修改代码为:

// 替换成你实际的服务器数字ID,注意不要加引号
ulong guildId = 123456789012345678;
if (IsDebug())
    await sCommands.RegisterCommandsToGuildAsync(guildId);
else
    await sCommands.RegisterCommandsGloballyAsync(true);

字符串形式的ID会导致注册请求失败,命令无法同步到服务器。

2. 确认命令注册逻辑的执行时机

  • 确保注册代码在机器人客户端就绪后执行,比如放在Ready事件的回调中:
    client.Ready += async () =>
    {
        // 这里执行命令注册逻辑
        ulong guildId = 123456789012345678;
        if (IsDebug())
            await sCommands.RegisterCommandsToGuildAsync(guildId);
        else
            await sCommands.RegisterCommandsGloballyAsync(true);
    };
    
  • 添加日志输出,验证代码是否进入了预期的分支(Debug/非Debug),确认注册逻辑确实被执行。

3. 检查机器人授权权限

  • 确保机器人的授权链接同时包含bot和applications.commands范围,并且在目标服务器中拥有管理员权限或至少Use Slash Commands权限。
  • 重新生成授权链接,勾选所需权限后重新邀请机器人到服务器(无需删除现有机器人,重新授权会覆盖权限配置)。

4. 处理命令同步延迟与缓存

  • 全局命令注册后最多需要1小时完成同步,若刚注册全局命令,先等待一段时间再检查。
  • 服务器命令若长时间未显示,尝试先删除旧命令再重新注册:
    ulong guildId = 123456789012345678;
    await sCommands.DeleteGuildCommandsAsync(guildId);
    await sCommands.RegisterCommandsToGuildAsync(guildId);
    

5. 验证命令模块的加载

  • 确保带有[SlashCommand]属性的类继承自ModuleBase<SocketCommandContext>(针对Discord.Net库)。
  • 初始化CommandService时需配置正确的参数,并确保模块被扫描加载:
    var commandConfig = new CommandServiceConfig
    {
        // 启用Slash命令支持
        SlashCommandRenamedFields = SlashCommandRenamedFields.None
    };
    var sCommands = new CommandService(commandConfig);
    // 加载当前程序集中的所有命令模块
    await sCommands.AddModulesAsync(Assembly.GetEntryAssembly(), null);
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 00:00:57