如何使用DiscordSocketClient获取用户的角色

时间:2021-12-08 19:57:49

I have been locking for a long time on how to get a user's role so I can set permissions for commands. This is my code. I am using Discord.NET in the newer version.

我已经锁定了很长时间如何获得用户的角色,所以我可以设置命令的权限。这是我的代码。我在较新版本中使用Discord.NET。

using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AmberScript2.Modules
{
    public class Kick : ModuleBase<SocketCommandContext>
    {
        [Command("kick")]
        public async Task KickUser(string userName)
        {
            if (Context.Guild.GetRole(Context.Message.Author.Id).Name == "Administrator")
            {
                await Context.Channel.SendMessageAsync("Success!");
            }
            else
            {
                await Context.Channel.SendMessageAsync("Inadequate permisions.");
            }
        }
    }
}

The error i am getting is object reference not set to an instance of an object. I have been trying to find the source of it and i can't. Thanks.

我得到的错误是对象引用未设置为对象的实例。我一直试图找到它的来源,我不能。谢谢。

(And yes i have yet to get rid of excess usings. This code isn't done yet.)

(是的,我还没有摆脱多余的使用。这段代码还没有完成。)

3 个解决方案

#1


3  

If you want to try to get a role of the user, try using SocketGuildUser instead of a string. (Use var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Role");)

如果您想尝试获取用户的角色,请尝试使用SocketGuildUser而不是字符串。 (使用var role =(user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name ==“Role”);)

using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AmberScript2.Modules
{
    public class Kick : ModuleBase<SocketCommandContext>
    {
        [Command("kick")]
        public async Task KickUser(SocketGuildUser userName)
        {
             var user = Context.User as SocketGuildUser;
             var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Role");
             if (!userName.Roles.Contains(role))
             {
            // Do Stuff
            if (user.GuildPermissions.KickMembers)
            {
            await userName.KickAsync();
            }
        }
    }
}
}

That is most of my code for kicking.

这是我踢的大部分代码。

#2


0  

I have the same problem... To my knowledge Linq needs to be used with IGuildUser or SocketGuildUser. I have not yet been able to create functioning code to check if the person performing the command does have a role named "Admin"or some other name Edit: This might be helpful https://discord.foxbot.me/docs/api/Discord.WebSocket.SocketGuildUser.html

我有同样的问题......据我所知,Linq需要与IGuildUser或SocketGuildUser一起使用。我还没有能够创建正常运行的代码来检查执行该命令的人是否具有名为“Admin”的角色或其他名称编辑:这可能会有所帮助https://discord.foxbot.me/docs/api/ Discord.WebSocket.SocketGuildUser.html

#3


0  

With the line RequireUserPermission(GuildPermission.KickMembers) you check whether or not the user has the permission to kick members.

使用行RequireUserPermission(GuildPermission.KickMembers),您可以检查用户是否具有踢成员的权限。

Within GuildPermission there are many different permissions. Like GuildPermission.ManageRoles etc.

在GuildPermission中有许多不同的权限。像GuildPermission.ManageRoles等。

using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AmberScript2.Modules
{
    public class Kick : ModuleBase<SocketCommandContext>
    {
        [Command("kick"), RequireUserPermission(GuildPermission.KickMembers)]
        public async Task KickUser(SocketGuildUser userName)
        {
             var user = Context.User as SocketGuildUser;
             var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Role");
             if (!userName.Roles.Contains(role))
             {
            // Do Stuff
            if (user.GuildPermissions.KickMembers)
            {
            await userName.KickAsync();
            }
        }
    }
}

#1


3  

If you want to try to get a role of the user, try using SocketGuildUser instead of a string. (Use var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Role");)

如果您想尝试获取用户的角色,请尝试使用SocketGuildUser而不是字符串。 (使用var role =(user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name ==“Role”);)

using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AmberScript2.Modules
{
    public class Kick : ModuleBase<SocketCommandContext>
    {
        [Command("kick")]
        public async Task KickUser(SocketGuildUser userName)
        {
             var user = Context.User as SocketGuildUser;
             var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Role");
             if (!userName.Roles.Contains(role))
             {
            // Do Stuff
            if (user.GuildPermissions.KickMembers)
            {
            await userName.KickAsync();
            }
        }
    }
}
}

That is most of my code for kicking.

这是我踢的大部分代码。

#2


0  

I have the same problem... To my knowledge Linq needs to be used with IGuildUser or SocketGuildUser. I have not yet been able to create functioning code to check if the person performing the command does have a role named "Admin"or some other name Edit: This might be helpful https://discord.foxbot.me/docs/api/Discord.WebSocket.SocketGuildUser.html

我有同样的问题......据我所知,Linq需要与IGuildUser或SocketGuildUser一起使用。我还没有能够创建正常运行的代码来检查执行该命令的人是否具有名为“Admin”的角色或其他名称编辑:这可能会有所帮助https://discord.foxbot.me/docs/api/ Discord.WebSocket.SocketGuildUser.html

#3


0  

With the line RequireUserPermission(GuildPermission.KickMembers) you check whether or not the user has the permission to kick members.

使用行RequireUserPermission(GuildPermission.KickMembers),您可以检查用户是否具有踢成员的权限。

Within GuildPermission there are many different permissions. Like GuildPermission.ManageRoles etc.

在GuildPermission中有许多不同的权限。像GuildPermission.ManageRoles等。

using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AmberScript2.Modules
{
    public class Kick : ModuleBase<SocketCommandContext>
    {
        [Command("kick"), RequireUserPermission(GuildPermission.KickMembers)]
        public async Task KickUser(SocketGuildUser userName)
        {
             var user = Context.User as SocketGuildUser;
             var role = (user as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == "Role");
             if (!userName.Roles.Contains(role))
             {
            // Do Stuff
            if (user.GuildPermissions.KickMembers)
            {
            await userName.KickAsync();
            }
        }
    }
}