コード例 #1
0
ファイル: User.cs プロジェクト: TheGrandCoding/discord-bot
        bool canChangeNickname(SocketGuildUser oper, SocketGuildUser target)
        {
            if (oper == null)
            {
                return(false);
            }
            var bUser = oper.Guild.CurrentUser;

            if (bUser.Hierarchy <= target.Hierarchy)
            {
                return(false);
            }
            if (oper.GuildPermissions.Administrator)
            {
                return(true);
            }
            if ((oper.Hierarchy > target.Hierarchy))
            {
                return(oper.GuildPermissions.Administrator || oper.GuildPermissions.ManageNicknames);
            }
            else if (oper.Id == target.Id)
            {
                if (oper.GuildPermissions.ChangeNickname)
                {
                    return(true);
                }
                var botU = Program.GetUser(oper);
                if (PermChecker.UserHasPerm(botU, Perms.Bot.User.ChangeServerNickname))
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #2
0
        async Task printActualFor(BotUser u)
        {
            var paginator = new StaticPaginatorBuilder();
            var cPage     = new PageBuilder();

            cPage.Title = $"Permissions of {u.Name}";
            cPage.Text  = "These are the effective permissions of the user, taking into account wildcards, denials and allowals.";
            foreach (var perm in Service.AllNodes.Values)
            {
                if (!PermChecker.UserHasPerm(u, perm, out var inherit))
                {
                    continue;
                }
                string sep = inherit ? "**" : "";
                var    s   = $"`{perm.Node}` {sep}{perm.Description}{sep}\r\n";
                if ((cPage.Description.Length + s.Length) > 1024)
                {
                    paginator.AddPage(cPage);
                    cPage       = new PageBuilder();
                    cPage.Title = $"Permissions of {u.Name}";
                    cPage.Text  = "These are the effective permissions of the user, taking into account wildcards, denials and allowals.";
                }
                cPage.Description += s;
            }
            if (cPage.Description == "")
            {
                cPage.Description = "None.";
            }
            paginator.AddPage(cPage);
            await PagedReplyAsync(paginator);
        }
コード例 #3
0
        protected override void OnOpen()
        {
            string strToken = null;
            var    cookie   = Context.CookieCollection[AuthToken.SessionToken];

            strToken ??= cookie?.Value;
            strToken ??= Context.QueryString.Get(AuthToken.SessionToken);
            strToken ??= Context.Headers.Get($"X-{AuthToken.SessionToken.ToUpper()}");
            if (!Handler.findToken(strToken, out var usr, out _))
            {
                Program.LogMsg($"{IP} attempted unknown WS log.", LogSeverity.Debug, "WSLog");
                Context.WebSocket.Close(CloseStatusCode.Normal, "Unauthorized");
                return;
            }
            if (!PermChecker.UserHasPerm(usr, Perms.Bot.Developer.SeeLatestLog))
            {
                Program.LogMsg($"{IP}, {usr.Name} attempted forbidden log connection", LogSeverity.Warning, "WsLog");
                Context.WebSocket.Close(CloseStatusCode.Normal, "Forbidden");
                return;
            }
            User = usr;
            Program.LogMsg($"{User.Name} has begun watching the logs via WS", LogSeverity.Info, IP);
            lock (Program._lockObj)
            {
                foreach (var log in Program.lastLogs)
                {
                    Send(log);
                }
            }
            Program.Log += Handle;
        }
コード例 #4
0
 public bool HasPerm(NodeInfo node)
 {
     if (BotUser == null)
     {
         return(false);
     }
     return(PermChecker.UserHasPerm(BotUser, node));
 }
コード例 #5
0
        async Task doCheckFor(BotUser u, NodeInfo node)
        {
            var given = PermChecker.UserHasPerm(Context.BotUser, node, out var b);

            if (given)
            {
                await ReplyAsync($":ballot_box_with_check: {(u.Id == Context.User.Id ? "You have" : $"{u.Name} has")} that node {(b ? "via a wildcard" : "directly")}");
            }
コード例 #6
0
        public bool HasPerm(string perm)
        {
            pService ??= Program.Services.GetRequiredService <PermissionsService>();
            var node = pService.FindNode(perm);

            if (node == null)
            {
                Program.LogMsg($"Attempted checking invalid perm: {Path}, '{perm}'");
                return(false);
            }
            return(PermChecker.HasPerm(this, node));
        }
コード例 #7
0
        static async System.Threading.Tasks.Task <BotResult> runReactions(CallbackEventArgs e)
        {
            if (!(ulong.TryParse(e.State, out var guildId)))
            {
                return(new BotResult($"Failed to parse '{e.State}' as ulong."));
            }
            if (!(instance.Messages.TryGetValue(guildId, out var setup)))
            {
                return(new BotResult($"Guild {guildId} has no reaction roles set up."));
            }
            if (!(ulong.TryParse(e.ComponentId, out var roleId)))
            {
                return(new BotResult($"Failed to parse custom id as ulong: {e.ComponentId}"));
            }
            if (!(e.Message.Channel is ITextChannel txt))
            {
                return(new BotResult($"Current channel, {e.Message.Channel} is not a text channel"));
            }
            var role = txt.Guild.GetRole(roleId);

            if (role == null)
            {
                return(new BotResult($"Role {roleId} does not exist."));
            }
            var perm = $"roles.{role.Guild.Id}.{role.Id}";
            var user = await txt.Guild.GetUserAsync(e.User.Id);

            var bUser = Program.GetUser(user);

            if (PermChecker.UserHasPerm(bUser, perm) == false)
            {
                return(new BotResult($"You lack the permission to toggle that role.\r\n" /*+
                                                                                          * $"Use `{Program.Prefix}perms viewo {bUser.Id}` to see what permissions they do have."*/));
            }
            if (user.RoleIds.Any(x => x == roleId))
            {
                await user.RemoveRoleAsync(role);

                await e.Interaction.FollowupAsync($"You no longer have the {role.Name} role",
                                                  ephemeral : true);
            }
            else
            {
                await user.AddRoleAsync(role);

                await e.Interaction.FollowupAsync($"You now have the {role.Name} role",
                                                  ephemeral : true);
            }
            return(new BotResult());
        }
コード例 #8
0
        public async override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
        {
            var node = Program.Services.GetRequiredService <PermissionsService>().FindNode(Node);

            if (!(context is BotCommandContext bC))
            {
                return(PreconditionResult.FromError("Command context invalid, contact bot developer."));
            }
            if (PermChecker.HasPerm(bC, node))
            {
                return(PreconditionResult.FromSuccess());
            }
            return(PreconditionResult.FromError($"You required extra permissions to access this endpoint (`{node.Node}`: {node.Description})"));
        }
コード例 #9
0
        public override PreconditionResult Check(APIContext context)
        {
            if (context.User == null)
            {
                return(PreconditionResult.FromError("You must be logged in"));
            }
            List <NodeInfo> missing = new List <NodeInfo>();

            foreach (var perm in Nodes)
            {
                var node = (NodeInfo)perm;
                var val  = PermChecker.HasPerm(context, node);
                if (!val)
                {
                    missing.Add(node);
                }
            }
            if (missing.Count > 0)
            {
                return(PreconditionResult.FromError("Missing permissions: '" + string.Join("', '", missing.Select(x => x.Description)) + "'"));
            }
            return(PreconditionResult.FromSuccess());
        }