From 75a5f4a739c5bf8eeaae93e04d966c8cab3d7757 Mon Sep 17 00:00:00 2001 From: RogueException Date: Fri, 6 May 2016 01:11:15 -0300 Subject: [PATCH] Added Administrator, ChangeNick and ManageNick permissions --- src/Discord.Net/Enums/PermissionBits.cs | 8 +++- src/Discord.Net/Models/Channel.cs | 30 ++++++------- src/Discord.Net/Models/Permissions.cs | 80 ++++++++++++++++++++------------- 3 files changed, 68 insertions(+), 50 deletions(-) diff --git a/src/Discord.Net/Enums/PermissionBits.cs b/src/Discord.Net/Enums/PermissionBits.cs index 0766dadc4..dece56cef 100644 --- a/src/Discord.Net/Enums/PermissionBits.cs +++ b/src/Discord.Net/Enums/PermissionBits.cs @@ -6,10 +6,14 @@ CreateInstantInvite = 0, KickMembers = 1, BanMembers = 2, - ManageRolesOrPermissions = 3, + Administrator = 3, ManageChannel = 4, ManageServer = 5, + ChangeNickname = 26, + ManageNicknames = 27, + ManageRolesOrPermissions = 28, + //Text ReadMessages = 10, SendMessages = 11, @@ -26,6 +30,6 @@ MuteMembers = 22, DeafenMembers = 23, MoveMembers = 24, - UseVoiceActivation = 25 + UseVoiceActivation = 25, } } diff --git a/src/Discord.Net/Models/Channel.cs b/src/Discord.Net/Models/Channel.cs index bd2b4a9cc..14c88f432 100644 --- a/src/Discord.Net/Models/Channel.cs +++ b/src/Discord.Net/Models/Channel.cs @@ -74,7 +74,7 @@ namespace Discord public IEnumerable Messages => _messages?.Values ?? Enumerable.Empty(); /// Gets a collection of all custom permissions used for this channel. public IEnumerable PermissionOverwrites => _permissionOverwrites.Select(x => x.Value); - + /// Gets a collection of all users with read access to this channel. public IEnumerable Users { @@ -131,7 +131,7 @@ namespace Discord if (client.Config.MessageCacheSize > 0) _messages = new ConcurrentDictionary(2, (int)(client.Config.MessageCacheSize * 1.05)); } - + internal void Update(APIChannel model) { if (!IsPrivate && model.Name != null) @@ -199,7 +199,7 @@ namespace Discord await Server.ReorderChannels(channels.Skip(minPos), after).ConfigureAwait(false); } } - + public async Task Delete() { try { await Client.ClientAPI.Send(new DeleteChannelRequest(Id)).ConfigureAwait(false); } @@ -278,12 +278,12 @@ namespace Discord return new Message(id, this, userId != null ? GetUserFast(userId.Value) : null); } - public async Task DownloadMessages(int limit = 100, ulong? relativeMessageId = null, + public async Task DownloadMessages(int limit = 100, ulong? relativeMessageId = null, Relative relativeDir = Relative.Before, bool useCache = true) { if (limit < 0) throw new ArgumentOutOfRangeException(nameof(limit)); if (limit == 0 || Type != ChannelType.Text) return new Message[0]; - + try { var request = new GetMessagesRequest(Id) @@ -331,12 +331,12 @@ namespace Discord if (text == null) throw new ArgumentNullException(nameof(text)); if (text == "") throw new ArgumentException("Value cannot be blank", nameof(text)); if (text.Length > DiscordConfig.MaxMessageSize) - throw new ArgumentOutOfRangeException(nameof(text), $"Message must be {DiscordConfig.MaxMessageSize} characters or less."); + throw new ArgumentOutOfRangeException(nameof(text), $"Message must be {DiscordConfig.MaxMessageSize} characters or less."); return Task.FromResult(Client.MessageQueue.QueueSend(this, text, isTTS)); } public async Task SendFile(string filePath) - { + { using (var stream = File.OpenRead(filePath)) return await SendFile(System.IO.Path.GetFileName(filePath), stream).ConfigureAwait(false); } @@ -381,7 +381,7 @@ namespace Discord if (_users.TryGetValue(user.Id, out member)) { var perms = member.Permissions; - if (UpdatePermissions(member.User, ref perms)) + if (UpdatePermissions(member.User, ref perms)) _users[user.Id] = new Member(member.User, perms); } } @@ -398,8 +398,8 @@ namespace Discord //Start with this user's server permissions newPermissions = server.GetPermissions(user).RawValue; - if (IsPrivate || user == Server.Owner) - newPermissions = mask; //Owners always have all permissions + if (IsPrivate || user == Server.Owner || newPermissions.HasBit((byte)PermissionBits.Administrator) + newPermissions = mask; //Owners and Administrators always have all permissions else { var channelOverwrites = PermissionOverwrites; @@ -414,9 +414,7 @@ namespace Discord foreach (var allowUser in channelOverwrites.Where(x => x.TargetType == PermissionTarget.User && x.TargetId == user.Id && x.Permissions.AllowValue != 0)) newPermissions |= allowUser.Permissions.AllowValue; - if (newPermissions.HasBit((byte)PermissionBits.ManageRolesOrPermissions)) - newPermissions = mask; //ManageRolesOrPermissions gives all permisions - else if (Type == ChannelType.Text && !newPermissions.HasBit((byte)PermissionBits.ReadMessages)) + if (Type == ChannelType.Text && !newPermissions.HasBit((byte)PermissionBits.ReadMessages)) newPermissions = 0; //No read permission on a text channel removes all other permissions else if (Type == ChannelType.Voice && !newPermissions.HasBit((byte)PermissionBits.Connect)) newPermissions = 0; //No connect permissions on a voice channel removes all other permissions @@ -451,7 +449,7 @@ namespace Discord return perms; } } - + public ChannelPermissionOverrides GetPermissionsRule(User user) { if (user == null) throw new ArgumentNullException(nameof(user)); @@ -470,7 +468,7 @@ namespace Discord .Select(x => x.Permissions) .FirstOrDefault(); } - + public Task AddPermissionsRule(User user, ChannelPermissions allow, ChannelPermissions deny) { if (user == null) throw new ArgumentNullException(nameof(user)); @@ -506,7 +504,7 @@ namespace Discord }; return Client.ClientAPI.Send(request); } - + public Task RemovePermissionsRule(User user) { if (user == null) throw new ArgumentNullException(nameof(user)); diff --git a/src/Discord.Net/Models/Permissions.cs b/src/Discord.Net/Models/Permissions.cs index 609524dfe..112b12aa9 100644 --- a/src/Discord.Net/Models/Permissions.cs +++ b/src/Discord.Net/Models/Permissions.cs @@ -6,7 +6,7 @@ namespace Discord public struct ServerPermissions { public static ServerPermissions None { get; } = new ServerPermissions(); - public static ServerPermissions All { get; } = new ServerPermissions(Convert.ToUInt32("00000011111100111111110000111111", 2)); + public static ServerPermissions All { get; } = new ServerPermissions(Convert.ToUInt32("00011111111100111111110000111111", 2)); public uint RawValue { get; } @@ -16,8 +16,8 @@ namespace Discord public bool BanMembers => PermissionsHelper.GetValue(RawValue, PermissionBits.BanMembers); /// If True, a user may kick users from the server. public bool KickMembers => PermissionsHelper.GetValue(RawValue, PermissionBits.KickMembers); - /// If True, a user may adjust roles. This also implictly grants all other permissions. - public bool ManageRoles => PermissionsHelper.GetValue(RawValue, PermissionBits.ManageRolesOrPermissions); + /// If True, a user has all permissions and cannot have them revoked. + public bool Administrator => PermissionsHelper.GetValue(RawValue, PermissionBits.Administrator); /// If True, a user may create, delete and modify channels. public bool ManageChannels => PermissionsHelper.GetValue(RawValue, PermissionBits.ManageChannel); /// If True, a user may adjust server properties. @@ -53,30 +53,37 @@ namespace Discord /// If True, a user may use voice activation rather than push-to-talk. public bool UseVoiceActivation => PermissionsHelper.GetValue(RawValue, PermissionBits.UseVoiceActivation); - public ServerPermissions(bool? createInstantInvite = null, bool? manageRoles = null, + /// If True, a user may change their own nickname. + public bool ChangeNickname => PermissionsHelper.GetValue(RawValue, PermissionBits.ManageRolesOrPermissions); + /// If True, a user may change the nickname of other users. + public bool ManageNicknames => PermissionsHelper.GetValue(RawValue, PermissionBits.ManageRolesOrPermissions); + /// If True, a user may adjust roles. + public bool ManageRoles => PermissionsHelper.GetValue(RawValue, PermissionBits.ManageRolesOrPermissions); + + public ServerPermissions(bool? createInstantInvite = null, bool? administrator = 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) - : this(new ServerPermissions(), createInstantInvite, manageRoles, kickMembers, banMembers, manageChannel, manageServer, readMessages, + bool? moveMembers = null, bool? useVoiceActivation = null, bool? changeNickname = null, bool? manageNicknames = null, bool? manageRoles = null) + : this(new ServerPermissions(), createInstantInvite, administrator, kickMembers, banMembers, manageChannel, manageServer, readMessages, sendMessages, sendTTSMessages, manageMessages, embedLinks, attachFiles, mentionEveryone, connect, speak, muteMembers, deafenMembers, - moveMembers, useVoiceActivation) + moveMembers, useVoiceActivation, manageRoles) { } - public ServerPermissions(ServerPermissions basePerms, bool? createInstantInvite = null, bool? manageRoles = null, + public ServerPermissions(ServerPermissions basePerms, bool? createInstantInvite = null, bool? administrator = 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) + 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, manageRoles, PermissionBits.ManageRolesOrPermissions); PermissionsHelper.SetValue(ref value, manageChannel, PermissionBits.ManageChannel); PermissionsHelper.SetValue(ref value, manageServer, PermissionBits.ManageServer); PermissionsHelper.SetValue(ref value, readMessages, PermissionBits.ReadMessages); @@ -93,6 +100,9 @@ namespace Discord 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; } @@ -102,9 +112,9 @@ namespace Discord public struct ChannelPermissions { public static ChannelPermissions None { get; } = new ChannelPermissions(); - public static ChannelPermissions TextOnly { get; } = new ChannelPermissions(Convert.ToUInt32("00000000000000111111110000011001", 2)); + public static ChannelPermissions TextOnly { get; } = new ChannelPermissions(Convert.ToUInt32("00010000000000111111110000011001", 2)); public static ChannelPermissions PrivateOnly { get; } = new ChannelPermissions(Convert.ToUInt32("00000000000000011100110000000000", 2)); - public static ChannelPermissions VoiceOnly { get; } = new ChannelPermissions(Convert.ToUInt32("00000011111100000000000000011001", 2)); + public static ChannelPermissions VoiceOnly { get; } = new ChannelPermissions(Convert.ToUInt32("00010011111100000000000000011001", 2)); public static ChannelPermissions All(Channel channel) => All(channel.Type, channel.IsPrivate); public static ChannelPermissions All(ChannelType channelType, bool isPrivate) { @@ -118,8 +128,6 @@ namespace Discord /// If True, a user may create invites. public bool CreateInstantInvite => PermissionsHelper.GetValue(RawValue, PermissionBits.CreateInstantInvite); - /// If True, a user may adjust permissions. This also implictly grants all other permissions. - public bool ManagePermissions => PermissionsHelper.GetValue(RawValue, PermissionBits.ManageRolesOrPermissions); /// If True, a user may create, delete and modify this channel. public bool ManageChannel => PermissionsHelper.GetValue(RawValue, PermissionBits.ManageChannel); @@ -153,25 +161,27 @@ namespace Discord /// If True, a user may use voice activation rather than push-to-talk. public bool UseVoiceActivation => PermissionsHelper.GetValue(RawValue, PermissionBits.UseVoiceActivation); - public ChannelPermissions(bool? createInstantInvite = null, bool? managePermissions = null, + /// If True, a user may adjust permissions. + public bool ManagePermissions => PermissionsHelper.GetValue(RawValue, PermissionBits.ManageRolesOrPermissions); + + public ChannelPermissions(bool? createInstantInvite = null, bool? manageChannel = 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) - : this(new ChannelPermissions(), createInstantInvite, managePermissions, manageChannel, readMessages, sendMessages, sendTTSMessages, - manageMessages, embedLinks, attachFiles, mentionEveryone, connect, speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation) + bool? moveMembers = null, bool? useVoiceActivation = null, bool? managePermissions = null) + : this(new ChannelPermissions(), createInstantInvite, manageChannel, readMessages, sendMessages, sendTTSMessages, + manageMessages, embedLinks, attachFiles, mentionEveryone, connect, speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, managePermissions) { } - public ChannelPermissions(ChannelPermissions basePerms, bool? createInstantInvite = null, bool? managePermissions = null, + public ChannelPermissions(ChannelPermissions basePerms, bool? createInstantInvite = null, bool? manageChannel = 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? moveMembers = null, bool? useVoiceActivation = null, bool? managePermissions = null) { uint value = basePerms.RawValue; PermissionsHelper.SetValue(ref value, createInstantInvite, PermissionBits.CreateInstantInvite); - PermissionsHelper.SetValue(ref value, managePermissions, PermissionBits.ManageRolesOrPermissions); PermissionsHelper.SetValue(ref value, manageChannel, PermissionBits.ManageChannel); PermissionsHelper.SetValue(ref value, readMessages, PermissionBits.ReadMessages); PermissionsHelper.SetValue(ref value, sendMessages, PermissionBits.SendMessages); @@ -187,6 +197,7 @@ namespace Discord 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, managePermissions, PermissionBits.ManageRolesOrPermissions); RawValue = value; } @@ -202,8 +213,6 @@ namespace Discord /// If True, a user may create invites. public PermValue CreateInstantInvite => PermissionsHelper.GetValue(AllowValue, DenyValue, PermissionBits.CreateInstantInvite); - /// If True, a user may adjust permissions. This also implictly grants all other permissions. - public PermValue ManagePermissions => PermissionsHelper.GetValue(AllowValue, DenyValue, PermissionBits.ManageRolesOrPermissions); /// If True, a user may create, delete and modify this channel. public PermValue ManageChannel => PermissionsHelper.GetValue(AllowValue, DenyValue, PermissionBits.ManageChannel); /// If True, a user may join channels. @@ -223,8 +232,8 @@ namespace Discord /// If True, a user may mention @everyone. public PermValue MentionEveryone => PermissionsHelper.GetValue(AllowValue, DenyValue, PermissionBits.MentionEveryone); - /// If True, a user may connect to a voice channel. - public PermValue Connect => PermissionsHelper.GetValue(AllowValue, DenyValue, PermissionBits.Connect); + /// If True, a user may connect to a voice channel. + public PermValue Connect => PermissionsHelper.GetValue(AllowValue, DenyValue, PermissionBits.Connect); /// If True, a user may speak in a voice channel. public PermValue Speak => PermissionsHelper.GetValue(AllowValue, DenyValue, PermissionBits.Speak); /// If True, a user may mute users. @@ -236,25 +245,29 @@ namespace Discord /// If True, a user may use voice activation rather than push-to-talk. public PermValue UseVoiceActivation => PermissionsHelper.GetValue(AllowValue, DenyValue, PermissionBits.UseVoiceActivation); - public ChannelPermissionOverrides(PermValue? createInstantInvite = null, PermValue? managePermissions = null, + /// If True, a user may adjust permissions. + public PermValue ManagePermissions => PermissionsHelper.GetValue(AllowValue, DenyValue, PermissionBits.ManageRolesOrPermissions); + + public ChannelPermissionOverrides(PermValue? createInstantInvite = null, PermValue? manageChannel = null, PermValue? readMessages = null, PermValue? sendMessages = null, PermValue? sendTTSMessages = null, PermValue? manageMessages = null, PermValue? embedLinks = null, PermValue? attachFiles = null, PermValue? readMessageHistory = null, PermValue? mentionEveryone = null, PermValue? connect = null, PermValue? speak = null, PermValue? muteMembers = null, PermValue? deafenMembers = null, - PermValue? moveMembers = null, PermValue? useVoiceActivation = null) - : this(new ChannelPermissionOverrides(), createInstantInvite, managePermissions, manageChannel, readMessages, sendMessages, sendTTSMessages, - manageMessages, embedLinks, attachFiles, mentionEveryone, connect, speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation) + PermValue? moveMembers = null, PermValue? useVoiceActivation = null, PermValue? changeNickname = null, PermValue? manageNicknames = null, + PermValue? managePermissions = null) + : this(new ChannelPermissionOverrides(), createInstantInvite, manageChannel, readMessages, sendMessages, sendTTSMessages, + manageMessages, embedLinks, attachFiles, mentionEveryone, connect, speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, managePermissions) { } - public ChannelPermissionOverrides(ChannelPermissionOverrides basePerms, PermValue? createInstantInvite = null, PermValue? managePermissions = null, + public ChannelPermissionOverrides(ChannelPermissionOverrides basePerms, PermValue? createInstantInvite = null, PermValue? manageChannel = null, PermValue? readMessages = null, PermValue? sendMessages = null, PermValue? sendTTSMessages = null, PermValue? manageMessages = null, PermValue? embedLinks = null, PermValue? attachFiles = null, PermValue? readMessageHistory = null, PermValue? mentionEveryone = null, PermValue? connect = null, PermValue? speak = null, PermValue? muteMembers = null, PermValue? deafenMembers = null, - PermValue? moveMembers = null, PermValue? useVoiceActivation = null) + PermValue? moveMembers = null, PermValue? useVoiceActivation = null, PermValue? changeNickname = null, PermValue? manageNicknames = null, + PermValue? managePermissions = null) { uint allow = basePerms.AllowValue, deny = basePerms.DenyValue; PermissionsHelper.SetValue(ref allow, ref deny, createInstantInvite, PermissionBits.CreateInstantInvite); - PermissionsHelper.SetValue(ref allow, ref deny, managePermissions, PermissionBits.ManageRolesOrPermissions); PermissionsHelper.SetValue(ref allow, ref deny, manageChannel, PermissionBits.ManageChannel); PermissionsHelper.SetValue(ref allow, ref deny, readMessages, PermissionBits.ReadMessages); PermissionsHelper.SetValue(ref allow, ref deny, sendMessages, PermissionBits.SendMessages); @@ -270,6 +283,9 @@ namespace Discord PermissionsHelper.SetValue(ref allow, ref deny, deafenMembers, PermissionBits.DeafenMembers); PermissionsHelper.SetValue(ref allow, ref deny, moveMembers, PermissionBits.MoveMembers); PermissionsHelper.SetValue(ref allow, ref deny, useVoiceActivation, PermissionBits.UseVoiceActivation); + PermissionsHelper.SetValue(ref allow, ref deny, changeNickname, PermissionBits.ChangeNickname); + PermissionsHelper.SetValue(ref allow, ref deny, manageNicknames, PermissionBits.ManageNicknames); + PermissionsHelper.SetValue(ref allow, ref deny, managePermissions, PermissionBits.ManageRolesOrPermissions); AllowValue = allow; DenyValue = deny;