代码之家  ›  专栏  ›  技术社区  ›  ComedicChimera

如何使用DiscordSocketClient获取用户角色

  •  1
  • ComedicChimera  · 技术社区  · 7 年前

    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.");
                }
            }
        }
    }
    

    3 回复  |  直到 7 年前
        1
  •  5
  •   dVenom HD    7 年前

    如果您想尝试获取用户角色,请尝试使用SocketGuildUser而不是字符串。(使用var role=(用户为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();
                }
            }
        }
    }
    }
    

        2
  •  3
  •   D4ryB3rry    6 年前

    在GuildPermission中有许多不同的权限。

    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();
                }
            }
        }
    }
    
        3
  •  0
  •   Campiotti    7 年前

    据我所知,Linq需要与IGuildUser或SocketGuildUser一起使用。 编辑:这可能会有帮助 https://discord.foxbot.me/docs/api/Discord.WebSocket.SocketGuildUser.html