Kicking a user in Discord.Net

牧云@^-^@ 提交于 2021-01-28 09:21:26

问题


I made this code to kick a user from the guild of a discord server

The bot console doesn't give me any errors, but this code doesn't work properly when I try to kick a user. What I mean by this is that it doesn't kick the user, and it doesn't output anything in the channel.

I also checked the bots permissions, and updated these so this can't be the problem.

The version of discord.net is 1.0.2

    [Command("kick"), Alias("Kick"), Summary("Kicks a user from the server")]
    public async Task Kick(SocketGuildUser userAccount, string reason)
    {
        var user = Context.User as SocketGuildUser;
        var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Admin");
        if (!userAccount.Roles.Contains(role))
        {
            if (user.GuildPermissions.KickMembers)
            {
                await userAccount.KickAsync(reason);
                await Context.Channel.SendMessageAsync($"The user `{userAccount}` has been kicked, for {reason}");
            }
            else
            {
                await Context.Channel.SendMessageAsync("No permissions for kicking a user.");
            }
        }
        else
        {
            await Context.Channel.SendMessageAsync("This User can't be kicked, because the user has a admin role.");
        }
    }

回答1:


[Command("kick")]
[RequireBotPermission(GuildPermission.KickMembers)]
public async Task KickAsync(IGuildUser user)
{

    var GuildUser = Context.Guild.GetUser(Context.User.Id);

    if (!GuildUser.GuildPermissions.KickMembers)
    {
        await Context.Message.DeleteAsync();
        await ReplyAsync(":warning: `No permissions to kick players`");
        return;
    }
    else
    {
        await user.KickAsync();
        await Context.Channel.SendMessageAsync($":eye: `{user.Username} has been kicked from the server`");

        var guild = Context.Guild;
        var channel = guild.GetTextChannel(609086251978457098); //582790350620327952

        EmbedBuilder builder = new EmbedBuilder();

        builder.WithTitle("Logged Information")
            .AddField("User", $"{user.Mention}")
            .AddField("Moderator", $"{Context.User.Username}")
            .AddField("Other Information", "Can be invited back")
            .AddField("Command", $"``.kick {user.Username}``")
            .WithDescription($"This player has been kicked from {Context.Guild.Name} by {Context.User.Username}")
            .WithFooter($"{Context.User.Username}", Context.User.GetAvatarUrl())

            .WithCurrentTimestamp()
            .WithColor(new Color(54, 57, 62));
    }
}



回答2:


I figured it out, instead of using the

async Task Kick(SocketGuildUser userAccount, string reason)

I needed to use

async Task Kick(IGuildUser userAccount, string reason)


来源:https://stackoverflow.com/questions/53764827/kicking-a-user-in-discord-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!