Beispiel #1
0
        public static bool HaveAccess(this SocketGuildChannel channel, SocketGuildUser user)
        {
            if (channel.GetUser(user.Id) != null)
            {
                return(true);
            }

            if (channel.PermissionOverwrites.Count == 0)
            {
                return(true); // Default permissions. Access all
            }
            var overwrite = channel.GetPermissionOverwrite(user);

            if (overwrite != null)
            {
                // Specific user overwrite
                if (overwrite.Value.ViewChannel == PermValue.Allow)
                {
                    return(true);
                }
                else if (overwrite.Value.ViewChannel == PermValue.Deny)
                {
                    return(false);
                }
            }

            var everyonePerm   = channel.GetPermissionOverwrite(user.Guild.EveryoneRole);
            var isEveryonePerm = everyonePerm != null && (everyonePerm.Value.ViewChannel == PermValue.Allow || everyonePerm.Value.ViewChannel == PermValue.Inherit);

            foreach (var role in user.Roles.Where(o => !o.IsEveryone))
            {
                var roleOverwrite = channel.GetPermissionOverwrite(role);
                if (roleOverwrite == null)
                {
                    continue;
                }

                if (roleOverwrite.Value.ViewChannel == PermValue.Deny && isEveryonePerm)
                {
                    return(false);
                }

                if (roleOverwrite.Value.ViewChannel == PermValue.Allow)
                {
                    return(true);
                }
            }

            return(isEveryonePerm);
        }
Beispiel #2
0
        private async Task ModifyUserInChannelAsync(IUser user, Action <OverwritePermissions> perms, string reason = null, SocketGuildChannel channel = null)
        {
            if (channel == null)
            {
                channel = Context.Channel as SocketGuildChannel;
            }

            var overwrite = channel.GetPermissionOverwrite(user) ?? OverwritePermissions.InheritAll;

            perms(overwrite);

            try
            {
                RequestOptions options = RequestOptions.Default;

                if (reason != null)
                {
                    options.AuditLogReason = reason;
                }

                await(channel as SocketTextChannel).AddPermissionOverwriteAsync(user, overwrite, options);

                await ReplyAsync("Successfully modified user.");
            }
            catch (HttpException e) when(e.DiscordCode.GetValueOrDefault() == 50013)
            {
                var embed = (await _misc.GenerateErrorMessage(e));

                embed.Description = $"{user.Mention} is above me in the hierarchy, so I cannot complete the requested action.";

                await ReplyAsync(embed : embed.Build());
            }
        }
 public virtual OverwritePermissions?GetPermissionOverwrite(IRole role)
 {
     return(_socketGuildChannel.GetPermissionOverwrite(role));
 }