为什么我的 !ban 命令不起作用(找不到用户)DISCORD.NET / C#

Why my !ban command dosen't work (User not found) DISCORD.NET / C#

我正在尝试使用 Discord.NET 在 C# 中制作一个机器人。如果我尝试使用我的命令禁止用户,我的控制台应用程序会返回错误:未找到用户

我的其他命令正常工作

我的任务 HandleCommandAsync:

    public async Task HandleCommandAsync(SocketMessage socketMessage)
    {
        var message = socketMessage as SocketUserMessage;
        var context = new SocketCommandContext(_client, message);

        if (message.Author.IsBot)
            return;

        int caracterPos = 0;
        if(message.HasStringPrefix("!", ref caracterPos))
        {
            var result = await _commands.ExecuteAsync(context, caracterPos, _services);
            if (!result.IsSuccess)
                Console.WriteLine(result.ErrorReason);
            if (result.Error.Equals(CommandError.UnmetPrecondition))
                await message.Channel.SendMessageAsync(result.ErrorReason);
        }
    }

result.ErrorReason =>“找不到用户”

等待message.Channel.SendMessageAsync(result.ErrorReason); => 不要发送消息

我的命令在我的 commands.cs class :

    [Command("ban")]
    [RequireUserPermission(GuildPermission.BanMembers, ErrorMessage = "Tu n'as pas la permission de bannir des membres !")]
    public async Task BanMember(SocketGuildUser user = null, [Remainder] string reason = null)
    {
        if (user == null)
        {
            await ReplyAsync("Aucun Utilisateur spécifié");
            return;
        }
        if (reason == null)
            reason = "Raison non-spécifiée";

        var embedBuilder = new EmbedBuilder()
            .WithDescription($":white_check_mark: {user.Mention} a été banni\n**Raison :** {reason}")
            .WithColor(new Color(255,0,0));
        Embed embed = embedBuilder.Build();
        await ReplyAsync(embed: embed);
        await Context.Guild.AddBanAsync(user, 0, reason);
    }

我在 Discord 中的命令:!ban @user reason

感谢您的帮助!

LPR

您必须在您的 discord 应用程序中激活它 -> Bot :

        config = new DiscordSocketConfig
        {
            AlwaysDownloadUsers = true,
            MessageCacheSize = 100,
            GatewayIntents =
            GatewayIntents.Guilds |
            GatewayIntents.GuildMembers |
            GatewayIntents.GuildMessageReactions |
            GatewayIntents.GuildMessages |
            GatewayIntents.GuildVoiceStates
        };
        DiscordSocketClient client = new DiscordSocketClient(config);