当前位置: 首页>>代码示例>>C#>>正文


C# User.HasRole方法代码示例

本文整理汇总了C#中Discord.User.HasRole方法的典型用法代码示例。如果您正苦于以下问题:C# User.HasRole方法的具体用法?C# User.HasRole怎么用?C# User.HasRole使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Discord.User的用法示例。


在下文中一共展示了User.HasRole方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CanRun

        public bool CanRun(Command command, User user, Channel channel, out string error)
        {
            error = null;

            if (channel.IsPrivate)
                return DefaultPermissionLevel <= DefaultPermChecker.GetPermissionLevel(user, channel);

            DynPermFullData data = DynPerms.GetPerms(channel.Server.Id);

            // apply default perms.
            bool retval = DefaultPermissionLevel <= DefaultPermChecker.GetPermissionLevel(user, channel);

            // if we do not have dynamic perms in place for the user's server, return the default perms.
            if (data == null || (!data.Perms.RolePerms.Any() && !data.Perms.UserPerms.Any()))
                return retval;

            /* 
              Firsly do role checks.
              Lower entries override higher entries. 
              To do that we have to iterate over the dict instead of using roles the user has as keys.
            */

            foreach (var pair in data.Perms.RolePerms)
            {
                if (user.HasRole(pair.Key))
                    retval = EvaluatePerms(pair.Value, command, retval, channel, ref error);
            }

            // users override roles, do them next.
            DynamicPermissionBlock permBlock;
            if (data.Perms.UserPerms.TryGetValue(user.Id, out permBlock))
                retval = EvaluatePerms(permBlock, command, retval, channel, ref error);

            return retval;
        }
开发者ID:SSStormy,项目名称:Stormbot,代码行数:35,代码来源:DynamicPermissionChecker.cs

示例2: CanRun

        public bool CanRun(Command command, User user, Channel channel, out string error) {
            error = null;

            if (channel.IsPrivate)
                return true;

            try {
                //is it a permission command?
                // if it is, check if the user has the correct role
                // if yes return true, if no return false
                if (command.Category == "Permissions")
                    if (user.Server.IsOwner || user.HasRole(PermissionHelper.ValidateRole(user.Server, PermissionsHandler.GetServerPermissionsRoleName(user.Server))))
                        return true;
                    else
                        throw new Exception($"You don't have the necessary role (**{PermissionsHandler._permissionsDict[user.Server].PermissionsControllerRole}**) to change permissions.");

                var permissionType = PermissionsHandler.GetPermissionBanType(command, user, channel);

                string msg;

                switch (permissionType) {
                    case PermissionsHandler.PermissionBanType.None:
                        return true;
                    case PermissionsHandler.PermissionBanType.ServerBanCommand:
                        msg = $"**{command.Text}** command has been banned from use on this **server**.";
                        break;
                    case PermissionsHandler.PermissionBanType.ServerBanModule:
                        msg = $"**{command.Category}** module has been banned from use on this **server**.";
                        break;
                    case PermissionsHandler.PermissionBanType.ChannelBanCommand:
                        msg = $"**{command.Text}** command has been banned from use on this **channel**.";
                        break;
                    case PermissionsHandler.PermissionBanType.ChannelBanModule:
                        msg = $"**{command.Category}** module has been banned from use on this **channel**.";
                        break;
                    case PermissionsHandler.PermissionBanType.RoleBanCommand:
                        msg = $"You do not have a **role** which permits you the usage of **{command.Text}** command.";
                        break;
                    case PermissionsHandler.PermissionBanType.RoleBanModule:
                        msg = $"You do not have a **role** which permits you the usage of **{command.Category}** module.";
                        break;
                    case PermissionsHandler.PermissionBanType.UserBanCommand:
                        msg = $"{user.Mention}, You have been banned from using **{command.Text}** command.";
                        break;
                    case PermissionsHandler.PermissionBanType.UserBanModule:
                        msg = $"{user.Mention}, You have been banned from using **{command.Category}** module.";
                        break;
                    default:
                        return true;
                }
                if (PermissionsHandler._permissionsDict[user.Server].Verbose) //if verbose - print errors
                    Task.Run(() => channel.SendMessage(msg));
                return false;
            } catch (Exception ex) {
                if (PermissionsHandler._permissionsDict[user.Server].Verbose) //if verbose - print errors
                    Task.Run(() => channel.SendMessage(ex.Message));
                return false;
            }
        }
开发者ID:TheFerty7,项目名称:NadekoBot,代码行数:59,代码来源:PermissionChecker.cs

示例3: CanRun

        public bool CanRun(Command command, User user, Channel channel, out string error)
        {
            error = String.Empty;

            if (!NadekoBot.Ready)
                return false;

            if (channel.IsPrivate || channel.Server == null)
                return command.Category == "Help";

            if (user == null)
                return false;

            if (ConfigHandler.IsUserBlacklisted(user.Id) ||
                (!channel.IsPrivate &&
                 (ConfigHandler.IsServerBlacklisted(channel.Server.Id) || ConfigHandler.IsChannelBlacklisted(channel.Id))))
            {
                return false;
            }
            try
            {
                if (timeBlackList.ContainsKey(user.Id))
                    return false;
            }
            catch { return false; }

            if (!channel.IsPrivate && !channel.Server.CurrentUser.GetPermissions(channel).SendMessages)
            {
                return false;
            }

            timeBlackList.TryAdd(user.Id, true);

            ServerPermissions perms;
            PermissionsHandler.PermissionsDict.TryGetValue(user.Server.Id, out perms);

            AddUserCooldown(user.Server.Id, user.Id, command.Text.ToLower());
            if (commandCooldowns.Keys.Contains(user.Server.Id + ":" + command.Text.ToLower()))
            {
                if (perms?.Verbose == true)
                    error = $"{user.Mention} You have a cooldown on that command.";
                return false;
            }

            try
            {
                //is it a permission command?
                // if it is, check if the user has the correct role
                // if yes return true, if no return false
                if (command.Category == "Permissions")
                {
                    Discord.Role role = null;
                    try
                    {
                        role = PermissionHelper.ValidateRole(user.Server,
                            PermissionsHandler.GetServerPermissionsRoleName(user.Server));
                    }
                    catch { }
                    if (user.Server.Owner.Id == user.Id || (role != null && user.HasRole(role)))
                        return true;
                    throw new Exception($"You don't have the necessary role (**{(perms?.PermissionsControllerRole ?? "Nadeko")}**) to change permissions.");
                }

                var permissionType = PermissionsHandler.GetPermissionBanType(command, user, channel);

                string msg;

                if (permissionType == PermissionsHandler.PermissionBanType.ServerBanModule &&
                    command.Category.ToLower() == "nsfw")
                    msg = $"**{command.Category}** module has been banned from use on this **server**.\nNSFW module is disabled by default. Server owner can type `;sm nsfw enable` to enable it.";
                else
                    switch (permissionType)
                    {
                        case PermissionsHandler.PermissionBanType.None:
                            return true;
                        case PermissionsHandler.PermissionBanType.ServerBanCommand:
                            msg = $"**{command.Text}** command has been banned from use on this **server**.";
                            break;
                        case PermissionsHandler.PermissionBanType.ServerBanModule:
                            msg = $"**{command.Category}** module has been banned from use on this **server**.";
                            break;
                        case PermissionsHandler.PermissionBanType.ChannelBanCommand:
                            msg = $"**{command.Text}** command has been banned from use on this **channel**.";
                            break;
                        case PermissionsHandler.PermissionBanType.ChannelBanModule:
                            msg = $"**{command.Category}** module has been banned from use on this **channel**.";
                            break;
                        case PermissionsHandler.PermissionBanType.RoleBanCommand:
                            msg = $"You do not have a **role** which permits you the usage of **{command.Text}** command.";
                            break;
                        case PermissionsHandler.PermissionBanType.RoleBanModule:
                            msg = $"You do not have a **role** which permits you the usage of **{command.Category}** module.";
                            break;
                        case PermissionsHandler.PermissionBanType.UserBanCommand:
                            msg = $"{user.Mention}, You have been banned from using **{command.Text}** command.";
                            break;
                        case PermissionsHandler.PermissionBanType.UserBanModule:
                            msg = $"{user.Mention}, You have been banned from using **{command.Category}** module.";
                            break;
                        default:
//.........这里部分代码省略.........
开发者ID:Ryonez,项目名称:Lucy,代码行数:101,代码来源:PermissionChecker.cs

示例4: CheckForRolesAsync

        private async static Task CheckForRolesAsync(User u, IEnumerable<Role> Roles)
        {
            var roleList = new List<Role>();

            foreach (Role r in Roles)
            {
                if (r.Name[0] == rolePrefix && u.HasRole(r))
                    roleList.Add(r);
            }

            if (roleList.Count != 0)
                await u.RemoveRoles(roleList.ToArray());
        }
开发者ID:XanderBras,项目名称:ColourBot,代码行数:13,代码来源:ColourCommand.cs


注:本文中的Discord.User.HasRole方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。