Пример #1
0
        private void UpdatePermissions(User user, ServerPermissions permissions)
        {
            uint newPermissions = 0;

            if (user.Id == _ownerId)
            {
                newPermissions = ServerPermissions.All.RawValue;
            }
            else
            {
                foreach (var serverRole in user.Roles)
                {
                    newPermissions |= serverRole.Permissions.RawValue;
                }
            }

            if (newPermissions.HasBit((byte)PermissionsBits.ManageRolesOrPermissions))
            {
                newPermissions = ServerPermissions.All.RawValue;
            }

            if (newPermissions != permissions.RawValue)
            {
                permissions.SetRawValueInternal(newPermissions);
                foreach (var channel in _channels)
                {
                    channel.Value.UpdatePermissions(user);
                }
            }
        }
Пример #2
0
        private bool UpdatePermissions(User user, ref ServerPermissions permissions)
        {
            uint newPermissions = 0;

            if (user.Id == _ownerId)
            {
                newPermissions = ServerPermissions.All.RawValue;
            }
            else
            {
                foreach (var serverRole in user.Roles)
                {
                    newPermissions |= serverRole.Permissions.RawValue;
                }
            }

            if (newPermissions.HasBit((byte)PermissionBits.ManageRolesOrPermissions))
            {
                newPermissions = ServerPermissions.All.RawValue;
            }

            if (newPermissions != permissions.RawValue)
            {
                permissions = new ServerPermissions(newPermissions);
                return(true);
            }
            return(false);
        }
Пример #3
0
        internal void Update(APIRole model)
        {
            if (model.Name != null)
            {
                Name = model.Name;
            }
            if (model.Hoist != null)
            {
                IsHoisted = model.Hoist.Value;
            }
            if (model.Managed != null)
            {
                IsManaged = model.Managed.Value;
            }
            if (model.Position != null && !IsEveryone)
            {
                Position = model.Position.Value;
            }
            if (model.Color != null)
            {
                Color = new Color(model.Color.Value);
            }
            if (model.Permissions != null)
            {
                Permissions = new ServerPermissions(model.Permissions.Value);
            }

            foreach (var member in Members)
            {
                Server.UpdatePermissions(member);
            }
        }
Пример #4
0
        public ServerPermissions(ServerPermissions basePerms, bool? createInstantInvite = null, bool? administrator = null,
            bool? banMembers = null, bool? kickMembers = null, bool? manageChannel = null, bool? manageServer = null,
            bool? readMessages = null, bool? sendMessages = null, bool? sendTTSMessages = null, bool? manageMessages = null,
            bool? embedLinks = null, bool? attachFiles = null, bool? readMessageHistory = null, bool? mentionEveryone = null,
            bool? connect = null, bool? speak = null, bool? muteMembers = null, bool? deafenMembers = null,
            bool? moveMembers = null, bool? useVoiceActivation = null, bool? changeNickname = null, bool? manageNicknames = null, bool? manageRoles = null)
        {
            uint value = basePerms.RawValue;

            PermissionsHelper.SetValue(ref value, createInstantInvite, PermissionBits.CreateInstantInvite);
            PermissionsHelper.SetValue(ref value, administrator, PermissionBits.Administrator);
            PermissionsHelper.SetValue(ref value, banMembers, PermissionBits.BanMembers);
            PermissionsHelper.SetValue(ref value, kickMembers, PermissionBits.KickMembers);
            PermissionsHelper.SetValue(ref value, manageChannel, PermissionBits.ManageChannel);
            PermissionsHelper.SetValue(ref value, manageServer, PermissionBits.ManageServer);
            PermissionsHelper.SetValue(ref value, readMessages, PermissionBits.ReadMessages);
            PermissionsHelper.SetValue(ref value, sendMessages, PermissionBits.SendMessages);
            PermissionsHelper.SetValue(ref value, sendTTSMessages, PermissionBits.SendTTSMessages);
            PermissionsHelper.SetValue(ref value, manageMessages, PermissionBits.ManageMessages);
            PermissionsHelper.SetValue(ref value, embedLinks, PermissionBits.EmbedLinks);
            PermissionsHelper.SetValue(ref value, attachFiles, PermissionBits.AttachFiles);
            PermissionsHelper.SetValue(ref value, readMessageHistory, PermissionBits.ReadMessageHistory);
            PermissionsHelper.SetValue(ref value, mentionEveryone, PermissionBits.MentionEveryone);
            PermissionsHelper.SetValue(ref value, connect, PermissionBits.Connect);
            PermissionsHelper.SetValue(ref value, speak, PermissionBits.Speak);
            PermissionsHelper.SetValue(ref value, muteMembers, PermissionBits.MuteMembers);
            PermissionsHelper.SetValue(ref value, deafenMembers, PermissionBits.DeafenMembers);
            PermissionsHelper.SetValue(ref value, moveMembers, PermissionBits.MoveMembers);
            PermissionsHelper.SetValue(ref value, useVoiceActivation, PermissionBits.UseVoiceActivation);
            PermissionsHelper.SetValue(ref value, changeNickname, PermissionBits.ChangeNickname);
            PermissionsHelper.SetValue(ref value, manageNicknames, PermissionBits.ManageNicknames);
            PermissionsHelper.SetValue(ref value, manageRoles, PermissionBits.ManageRolesOrPermissions);

            RawValue = value;
        }
Пример #5
0
        /// <summary> Creates a new role. </summary>
        public async Task <Role> CreateRole(string name, ServerPermissions permissions = null, Color color = null, bool isHoisted = false)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var createRequest  = new CreateRoleRequest(Id);
            var createResponse = await Client.ClientAPI.Send(createRequest).ConfigureAwait(false);

            var role = AddRole(createResponse.Id);

            role.Update(createResponse);

            var editRequest = new UpdateRoleRequest(role.Server.Id, role.Id)
            {
                Name        = name,
                Permissions = (permissions ?? role.Permissions).RawValue,
                Color       = (color ?? Color.Default).RawValue,
                IsHoisted   = isHoisted
            };
            var editResponse = await Client.ClientAPI.Send(editRequest).ConfigureAwait(false);

            role.Update(editResponse);

            return(role);
        }
Пример #6
0
        public ServerPermissions(ServerPermissions basePerms, bool?createInstantInvite = null, bool?manageRoles = null,
                                 bool?kickMembers  = null, bool?banMembers         = null, bool?manageChannel      = null, bool?manageServer    = null,
                                 bool?readMessages = null, bool?sendMessages       = null, bool?sendTTSMessages    = null, bool?manageMessages  = null,
                                 bool?embedLinks   = null, bool?attachFiles        = null, bool?readMessageHistory = null, bool?mentionEveryone = null,
                                 bool?connect      = null, bool?speak              = null, bool?muteMembers = null, bool?deafenMembers = null,
                                 bool?moveMembers  = null, bool?useVoiceActivation = null)
        {
            uint value = basePerms.RawValue;

            PermissionsHelper.SetValue(ref value, createInstantInvite, PermissionBits.CreateInstantInvite);
            PermissionsHelper.SetValue(ref value, banMembers, PermissionBits.BanMembers);
            PermissionsHelper.SetValue(ref value, kickMembers, PermissionBits.KickMembers);
            PermissionsHelper.SetValue(ref value, manageRoles, PermissionBits.ManageRolesOrPermissions);
            PermissionsHelper.SetValue(ref value, manageChannel, PermissionBits.ManageChannel);
            PermissionsHelper.SetValue(ref value, manageServer, PermissionBits.ManageServer);
            PermissionsHelper.SetValue(ref value, readMessages, PermissionBits.ReadMessages);
            PermissionsHelper.SetValue(ref value, sendMessages, PermissionBits.SendMessages);
            PermissionsHelper.SetValue(ref value, sendTTSMessages, PermissionBits.SendTTSMessages);
            PermissionsHelper.SetValue(ref value, manageMessages, PermissionBits.ManageMessages);
            PermissionsHelper.SetValue(ref value, embedLinks, PermissionBits.EmbedLinks);
            PermissionsHelper.SetValue(ref value, attachFiles, PermissionBits.AttachFiles);
            PermissionsHelper.SetValue(ref value, readMessageHistory, PermissionBits.ReadMessageHistory);
            PermissionsHelper.SetValue(ref value, mentionEveryone, PermissionBits.MentionEveryone);
            PermissionsHelper.SetValue(ref value, connect, PermissionBits.Connect);
            PermissionsHelper.SetValue(ref value, speak, PermissionBits.Speak);
            PermissionsHelper.SetValue(ref value, muteMembers, PermissionBits.MuteMembers);
            PermissionsHelper.SetValue(ref value, deafenMembers, PermissionBits.DeafenMembers);
            PermissionsHelper.SetValue(ref value, moveMembers, PermissionBits.MoveMembers);
            PermissionsHelper.SetValue(ref value, useVoiceActivation, PermissionBits.UseVoiceActivation);

            RawValue = value;
        }
Пример #7
0
		internal Role(ulong id, Server server)
		{
            Id = id;
            Server = server;

			Permissions = new ServerPermissions(0);
			Color = new Color(0);
		}
Пример #8
0
        internal Role(ulong id, Server server)
        {
            Id     = id;
            Server = server;

            Permissions = new ServerPermissions(0);
            Color       = new Color(0);
        }
Пример #9
0
        //TODO: Add local members cache

        internal Role(DiscordClient client, string id, string serverId)
            : base(client, id)
        {
            _server     = new Reference <Server>(serverId, x => _client.Servers[x], x => x.AddRole(this), x => x.RemoveRole(this));
            Permissions = new ServerPermissions(0);
            Permissions.Lock();
            Color = new Color(0);
            Color.Lock();
        }
Пример #10
0
		//TODO: Add local members cache

		internal Role(DiscordClient client, string id, string serverId)
			: base(client, id)
		{
			_server = new Reference<Server>(serverId, x => _client.Servers[x], x => x.AddRole(this), x => x.RemoveRole(this));
			Permissions = new ServerPermissions(0);
			Permissions.Lock();
			Color = new Color(0);
			Color.Lock();
		}
Пример #11
0
        public static async Task<bool> SafeEdit(this Role role,
            string name = null, ServerPermissions? perm = null, Color color = null, bool? isHoisted = false,
            int? position = null)
        {
            if (color != null && role.IsEveryone)
                return false;

            await role.Edit(name, perm, color, isHoisted, position);

            return true;
        }
Пример #12
0
        public async Task Edit(string name = null, ServerPermissions permissions = null, Color color = null, bool?isHoisted = null, int?position = null)
        {
            var updateRequest = new UpdateRoleRequest(Server.Id, Id)
            {
                Name        = name ?? Name,
                Permissions = (permissions ?? Permissions).RawValue,
                Color       = (color ?? Color).RawValue,
                IsHoisted   = isHoisted ?? IsHoisted
            };

            var updateResponse = await Client.ClientAPI.Send(updateRequest).ConfigureAwait(false);

            if (position != null)
            {
                int    oldPos = Position;
                int    newPos = position.Value;
                int    minPos;
                Role[] roles = Server.Roles.OrderBy(x => x.Position).ToArray();

                if (oldPos < newPos) //Moving Down
                {
                    minPos = oldPos;
                    for (int i = oldPos; i < newPos; i++)
                    {
                        roles[i] = roles[i + 1];
                    }
                    roles[newPos] = this;
                }
                else //(oldPos > newPos) Moving Up
                {
                    minPos = newPos;
                    for (int i = oldPos; i > newPos; i--)
                    {
                        roles[i] = roles[i - 1];
                    }
                    roles[newPos] = this;
                }

                var reorderRequest = new ReorderRolesRequest(Server.Id)
                {
                    RoleIds  = roles.Skip(minPos).Select(x => x.Id).ToArray(),
                    StartPos = minPos
                };
                await Client.ClientAPI.Send(reorderRequest).ConfigureAwait(false);
            }
        }
Пример #13
0
        public async Task Edit(string name = null, ServerPermissions? permissions = null, Color color = null, bool? isHoisted = null, int? position = null, bool? isMentionable = null)
        {
            var updateRequest = new UpdateRoleRequest(Server.Id, Id)
            {
                Name = name ?? Name,
                Permissions = (permissions ?? Permissions).RawValue,
                Color = (color ?? Color).RawValue,
                IsHoisted = isHoisted ?? IsHoisted,
                IsMentionable = isMentionable ?? IsMentionable
            };

            var updateResponse = await Client.ClientAPI.Send(updateRequest).ConfigureAwait(false);

            if (position != null)
            {
                int oldPos = Position;
                int newPos = position.Value;
                int minPos;
                Role[] roles = Server.Roles.OrderBy(x => x.Position).ToArray();

                if (oldPos < newPos) //Moving Down
                {
                    minPos = oldPos;
                    for (int i = oldPos; i < newPos; i++)
                        roles[i] = roles[i + 1];
                    roles[newPos] = this;
                }
                else //(oldPos > newPos) Moving Up
                {
                    minPos = newPos;
                    for (int i = oldPos; i > newPos; i--)
                        roles[i] = roles[i - 1];
                    roles[newPos] = this;
                }

                var reorderRequest = new ReorderRolesRequest(Server.Id)
                {
                    RoleIds = roles.Skip(minPos).Select(x => x.Id).ToArray(),
                    StartPos = minPos
                };
                await Client.ClientAPI.Send(reorderRequest).ConfigureAwait(false);
            }
        }
Пример #14
0
        public async Task EditRole(Role role, string name = null, ServerPermissions permissions = null, Color color = null, bool?hoist = null, int?position = null)
        {
            if (role == null)
            {
                throw new ArgumentNullException(nameof(role));
            }
            CheckReady();

            //TODO: check this null workaround later, should be fixed on Discord's end soon
            var response = await _api.EditRole(role.Server.Id, role.Id,
                                               name : name ?? role.Name,
                                               permissions : (permissions ?? role.Permissions).RawValue,
                                               color : (color ?? role.Color).RawValue,
                                               hoist : hoist ?? role.IsHoisted).ConfigureAwait(false);

            if (position != null)
            {
                int    oldPos = role.Position;
                int    newPos = position.Value;
                int    minPos;
                Role[] roles = role.Server.Roles.OrderBy(x => x.Position).ToArray();

                if (oldPos < newPos)                 //Moving Down
                {
                    minPos = oldPos;
                    for (int i = oldPos; i < newPos; i++)
                    {
                        roles[i] = roles[i + 1];
                    }
                    roles[newPos] = role;
                }
                else                 //(oldPos > newPos) Moving Up
                {
                    minPos = newPos;
                    for (int i = oldPos; i > newPos; i--)
                    {
                        roles[i] = roles[i - 1];
                    }
                    roles[newPos] = role;
                }
                await _api.ReorderRoles(role.Server.Id, roles.Skip(minPos).Select(x => x.Id), minPos).ConfigureAwait(false);
            }
        }
Пример #15
0
        internal User(DiscordClient client, string id, string serverId)
            : base(client, id)
        {
            _globalUser = new Reference <GlobalUser>(id,
                                                     x => _client.GlobalUsers.GetOrAdd(x),
                                                     x => x.AddUser(this),
                                                     x => x.RemoveUser(this));
            _server = new Reference <Server>(serverId,
                                             x => _client.Servers[x],
                                             x =>
            {
                x.AddMember(this);
                if (Id == _client.CurrentUserId)
                {
                    x.CurrentUser = this;
                }
            },
                                             x =>
            {
                x.RemoveMember(this);
                if (Id == _client.CurrentUserId)
                {
                    x.CurrentUser = null;
                }
            });
            _voiceChannel = new Reference <Channel>(x => _client.Channels[x]);
            _roles        = new Dictionary <string, Role>();

            Status    = UserStatus.Offline;
            _channels = new ConcurrentDictionary <string, Channel>();
            if (serverId != null)
            {
                _permissions       = new ConcurrentDictionary <string, ChannelPermissions>();
                _serverPermissions = new ServerPermissions();
            }

            if (serverId == null)
            {
                UpdateRoles(null);
            }
        }
Пример #16
0
 internal void Update(APIRole model, bool updatePermissions)
 {
     if (model.Name != null)
     {
         Name = model.Name;
     }
     if (model.Hoist != null)
     {
         IsHoisted = model.Hoist.Value;
     }
     if (model.Managed != null)
     {
         IsManaged = model.Managed.Value;
     }
     if (model.Mentionable != null)
     {
         IsMentionable = model.Mentionable.Value;
     }
     if (model.Position != null && !IsEveryone)
     {
         Position = model.Position.Value;
     }
     if (model.Color != null)
     {
         Color = new Color(model.Color.Value);
     }
     if (model.Permissions != null)
     {
         Permissions = new ServerPermissions(model.Permissions.Value);
         if (updatePermissions) //Dont update these during READY
         {
             foreach (var member in Members)
             {
                 Server.UpdatePermissions(member);
             }
         }
     }
 }
Пример #17
0
		internal void Update(APIRole model, bool updatePermissions)
		{
            if (model.Name != null)
				Name = model.Name;
			if (model.Hoist != null)
				IsHoisted = model.Hoist.Value;
			if (model.Managed != null)
				IsManaged = model.Managed.Value;
			if (model.Mentionable != null)
				IsMentionable = model.Mentionable.Value;
			if (model.Position != null && !IsEveryone)
				Position = model.Position.Value;
			if (model.Color != null)
				Color = new Color(model.Color.Value);
            if (model.Permissions != null)
            {
                Permissions = new ServerPermissions(model.Permissions.Value);
                if (updatePermissions) //Dont update these during READY
                {                    
                    foreach (var member in Members)
                        Server.UpdatePermissions(member);
                }
            }
		}
Пример #18
0
 public Member(User user, ServerPermissions permissions)
 {
     User        = user;
     Permissions = permissions;
 }
Пример #19
0
		internal User(DiscordClient client, string id, string serverId)
			: base(client, id)
		{
			_globalUser = new Reference<GlobalUser>(id, 
				x => _client.GlobalUsers.GetOrAdd(x), 
				x => x.AddUser(this), 
				x => x.RemoveUser(this));
			_server = new Reference<Server>(serverId, 
				x => _client.Servers[x], 
				x =>
				{
					x.AddMember(this);
					if (Id == _client.CurrentUserId)
						x.CurrentUser = this;
                }, 
				x =>
				{
					x.RemoveMember(this);
					if (Id == _client.CurrentUserId)
						x.CurrentUser = null;
				});
			_voiceChannel = new Reference<Channel>(x => _client.Channels[x]);
			_roles = new Dictionary<string, Role>();

			Status = UserStatus.Offline;
			_channels = new ConcurrentDictionary<string, Channel>();
			if (serverId != null)
			{
				_permissions = new ConcurrentDictionary<string, ChannelPermissions>();
				_serverPermissions = new ServerPermissions();
			}

			if (serverId == null)
				UpdateRoles(null);
		}
Пример #20
0
 public Member(User user, ServerPermissions permissions)
 {
     User = user;
     Permissions = permissions;
 }
Пример #21
0
        async Task<Role> CreateMutedRole(Server server)
        {
            try
            {
                var role = await _client.CreateRole(server, "Muted");
                var serverPermissions = new ServerPermissions
                {
                    ReadMessages = true,
                    SendMessages = false,
                    SendTTSMessages = false
                };

                await _client.EditRole(role, "Muted", serverPermissions);

                return role;
            } 
            catch (Exception ex)
            {
                _botServices.Logging.LogError(string.Format("Failed to create muted role on the {0} server.", server.Name), ex);
                throw;
            }
        } 
Пример #22
0
 public Member(User user)
 {
     User        = user;
     Permissions = new ServerPermissions();
     Permissions.Lock();
 }
Пример #23
0
        /// <summary> Creates a new role. </summary>
        public async Task<Role> CreateRole(string name, ServerPermissions? permissions = null, Color color = null, bool isHoisted = false)
        {
            if (name == null) throw new ArgumentNullException(nameof(name));

            var createRequest = new CreateRoleRequest(Id);
            var createResponse = await Client.ClientAPI.Send(createRequest).ConfigureAwait(false);
            var role = AddRole(createResponse.Id);
            role.Update(createResponse);

            var editRequest = new UpdateRoleRequest(role.Server.Id, role.Id)
            {
                Name = name,
                Permissions = (permissions ?? role.Permissions).RawValue,
                Color = (color ?? Color.Default).RawValue,
                IsHoisted = isHoisted
            };
            var editResponse = await Client.ClientAPI.Send(editRequest).ConfigureAwait(false);
            role.Update(editResponse);

            return role;
        }
Пример #24
0
 public Member(User user)
 {
     User = user;
     Permissions = new ServerPermissions();
     Permissions.Lock();
 }
Пример #25
0
        private bool UpdatePermissions(User user, ref ServerPermissions permissions)
		{
			uint newPermissions = 0;

			if (user.Id == _ownerId)
				newPermissions = ServerPermissions.All.RawValue;
			else
			{
				foreach (var serverRole in user.Roles)
					newPermissions |= serverRole.Permissions.RawValue;
			}

			if (newPermissions.HasBit((byte)PermissionBits.ManageRolesOrPermissions))
				newPermissions = ServerPermissions.All.RawValue;

			if (newPermissions != permissions.RawValue)
			{
				permissions = new ServerPermissions(newPermissions);
                return true;
			}
            return false;
		}
Пример #26
0
        private void UpdatePermissions(User user, ServerPermissions permissions)
		{
			uint newPermissions = 0;

			if (user.Id == _ownerId)
				newPermissions = ServerPermissions.All.RawValue;
			else
			{
				foreach (var serverRole in user.Roles)
					newPermissions |= serverRole.Permissions.RawValue;
			}

			if (newPermissions.HasBit((byte)PermissionsBits.ManageRolesOrPermissions))
				newPermissions = ServerPermissions.All.RawValue;

			if (newPermissions != permissions.RawValue)
			{
				permissions.SetRawValueInternal(newPermissions);
				foreach (var channel in _channels)
					channel.Value.UpdatePermissions(user);
			}
		}