在Discord.Net中踢用户

时间:2018-12-13 15:12:32

标签: c# discord.net

我编写了这段代码,将用户踢出了不和谐服务器的行会

机器人控制台没有给我任何错误,但是当我尝试踢用户时,此代码无法正常工作。 我的意思是,它不会踢用户,并且不会在频道中输出任何内容。

我还检查了漫游器权限,并更新了这些权限,所以这不是问题。

感谢您的帮助。

编辑:discord.net的版本是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.");
        }
    }

2 个答案:

答案 0 :(得分:1)

我知道了,而不是使用

async Task Kick(SocketGuildUser userAccount, string reason)

我需要使用

async Task Kick(IGuildUser userAccount, string reason)

答案 1 :(得分:0)

[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));
    }
}