Exemplo n.º 1
0
        private static void CheckIfUserHasRoles(DSharpPlus.Entities.DiscordMember member, ulong adminId, ulong userId, out bool isAdmin, out bool isUser)
        {
            var roles = member.Roles;

            isAdmin = false;
            isUser  = false;
            foreach (var role in roles)
            {
                if (role.Id == adminId)
                {
                    isAdmin = true;
                }
                if (role.Id == userId)
                {
                    isUser = true;
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Adds a channel permission overwrite for specified member.
 /// </summary>
 /// <param name="member"></param>
 /// <param name="allow"></param>
 /// <param name="deny"></param>
 /// <param name="reason">Reason for audit logs.</param>
 /// <returns></returns>
 public Task AddOverwriteAsync(DiscordMember member, Permissions allow = Permissions.None, Permissions deny = Permissions.None, string reason = null)
 => this.Discord.ApiClient.EditChannelPermissionsAsync(this.Id, member.Id, allow, deny, "member", reason);
Exemplo n.º 3
0
        /// <summary>
        /// Calculates permissions for a given member.
        /// </summary>
        /// <param name="mbr">Member to calculate permissions for.</param>
        /// <returns>Calculated permissions for a given member.</returns>
        public Permissions PermissionsFor(DiscordMember mbr)
        {
            // default permissions
            const Permissions def = Permissions.None;

            // future note: might be able to simplify @everyone role checks to just check any role ... but i'm not sure
            // xoxo, ~uwx
            //
            // you should use a single tilde
            // ~emzi

            // user > role > everyone
            // allow > deny > undefined
            // =>
            // user allow > user deny > role allow > role deny > everyone allow > everyone deny
            // thanks to meew0

            if (this.IsPrivate || this.Guild == null)
            {
                return(def);
            }

            if (this.Guild.OwnerId == mbr.Id)
            {
                return(~def);
            }

            Permissions perms;

            // assign @everyone permissions
            var everyoneRole = this.Guild.EveryoneRole;

            perms = everyoneRole.Permissions;

            // roles that member is in
            var mbRoles = mbr.Roles.Where(xr => xr.Id != everyoneRole.Id).ToArray();
            // channel overrides for roles that member is in
            var mbRoleOverrides = mbRoles
                                  .Select(xr => this._permission_overwrites.FirstOrDefault(xo => xo.Id == xr.Id))
                                  .Where(xo => xo != null)
                                  .ToList();

            // assign permissions from member's roles (in order)
            perms |= mbRoles.Aggregate(def, (c, role) => c | role.Permissions);

            // assign channel permission overwrites for @everyone pseudo-role
            var everyoneOverwrites = this._permission_overwrites.FirstOrDefault(xo => xo.Id == everyoneRole.Id);

            if (everyoneOverwrites != null)
            {
                perms &= ~everyoneOverwrites.Deny;
                perms |= everyoneOverwrites.Allow;
            }

            // assign channel permission overwrites for member's roles (explicit deny)
            perms &= ~mbRoleOverrides.Aggregate(def, (c, overs) => c | overs.Deny);
            // assign channel permission overwrites for member's roles (explicit allow)
            perms |= mbRoleOverrides.Aggregate(def, (c, overs) => c | overs.Allow);

            // channel overrides for just this member
            var mbOverrides = this._permission_overwrites.FirstOrDefault(xo => xo.Id == mbr.Id);

            if (mbOverrides == null)
            {
                return(perms);
            }

            // assign channel permission overwrites for just this member
            perms &= ~mbOverrides.Deny;
            perms |= mbOverrides.Allow;

            return(perms);
        }