Discord.NET Slash 命令未显示

问题描述 投票:0回答:1

所以我制作了一个不和谐的机器人,由于某种原因我看不到我创建的斜杠命令。

斜线命令

[SlashCommand("ping", "Receive a ping message")]
public async Task HandlePingCommand()
{
    await RespondAsync("Stop pinging me!");
}

我几乎可以肯定这与机器人权限有关。 我已经设置了 GatewayIntents AllUnprivileged 和 MessageContent。

Discord 开发者门户权限

添加信息

if (IsDebug())
    await sCommands.RegisterCommandsToGuildAsync("my server id"));
else
    await sCommands.RegisterCommandsGloballyAsync(true);

我还添加了 applications.commands 范围

如果需要更多信息和/或代码,我当然会添加它。

c# discord permissions bots discord.net
1个回答
0
投票

我也遇到了同样的问题,这就是我的解决方法:

  1. 启用日志记录并查看它是否记录任何错误等(我的问题之一是我将
    class
    传递到了 API 不支持的命令方法中:
    No type TypeConverter is defined for this Discord.WebSocket.SocketSlashCommand
_client.Log += message => // this one
{
    Console.WriteLine(message);
    return Task.CompletedTask;
};
// other code
_client.Ready += OnClientReady;
await _client.LoginAsync(TokenType.Bot, discordToken);
await _client.StartAsync();
  1. 尝试设置
    GatewayIntents.All
var config = new DiscordSocketConfig
{
    GatewayIntents = GatewayIntents.All
};
  1. 确保它与我的代码相似
private async Task OnClientReady()
{
    _client.Ready -= OnClientReady;
    _interactionService = new InteractionService(_client);
    await _interactionService.AddModulesAsync(Assembly.GetExecutingAssembly(), _serviceProvider);
    await _interactionService.RegisterCommandsGloballyAsync();
    _client.InteractionCreated += async interaction =>
    {
        var scope = _serviceProvider.CreateScope();
        var ctx = new SocketInteractionContext(_client, interaction);
        try
        {
            await _interactionService.ExecuteCommandAsync(ctx, scope.ServiceProvider);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error executing command: {ex.Message}");
        }
    };
}
  1. 尝试在浏览器中打开Discord并使用你的命令/yourcommand,之后,我的命令同时出现在浏览器和Discord应用程序中
© www.soinside.com 2019 - 2024. All rights reserved.