discord.net 禁止值为 null

discord.net ban having a value of null

public async Task ExecuteAsync(SocketUserMessage input, string[] parameters)
{
    if (parameters.Length == 0)
    {
        parameters[0] = "Unspecified";
        return;
    }
    var allBans = await Context.Guild.GetBansAsync();
    bool isBanned = allBans.Select(b => b.User).Where(u => u.Username == Context.User.Username).Any();

    if (!isBanned)
    {
        var targetHighest = (Context.User as SocketGuildUser).Hierarchy;
        var senderHighest = (Context.User as SocketGuildUser).Hierarchy;

        if (targetHighest < senderHighest)
        {
            await Context.Guild.AddBanAsync(Context.User);
            await Context.Channel.SendMessageAsync($"**{Context.User.Username}** has been banned by Moderator {Context.User}. Reason: **{parameters}**");

            var dmChannel = await Context.User.GetOrCreateDMChannelAsync();
            await dmChannel.SendMessageAsync($"You have been banned from **{Context.Guild.Name}** by Moderator **{Context.User}** for **{parameters}**");
        }
    }
}

所以我正在尝试发出禁止用户的命令,但 allBans 一直得到 null 值。不知道是我写错了还是真有错误..

感谢您的帮助。

Guild.GetBansAsync() returns 在此服务器上被禁止的用户列表,而不是调用者想要禁止的用户列表。

相反,您应该从来电者的消息中获取提及列表。
您可以 Context.Message.MentionedUser 获取提到的用户列表或
Context.Message.MentionedUserIds 获取提及用户的 ID 列表。

最后,你可以做到 await Guild.AddBanAsync(id/user)
(关于禁止的文档 here,您可以传入一些可选参数。)