Skip to content

Commit

Permalink
Proposed Solution for #674 Permissions Changes (#743)
Browse files Browse the repository at this point in the history
* Initial commit of changes. Changed permissions from bitwise index to use bitwise flags instead. Modified relevant methods involved

* Revised enum value naming

* Added FlagsAttribute to ChannelPermission, GuildPermission

* Added comments per Joe4evr suggestion

* Added underlines to hex value digits for readability per Joe4evr suggestion

* updated names to better reflect actual permission names as per SubZero0 suggestion

* fix for 236775c

* Replaced Math.Pow with left shift operator

* Cleaned up the formatting of ChannelPermission and GuildPermission enums to make it easier to read
  • Loading branch information
Chris-Johnston authored and foxbot committed Oct 26, 2017
1 parent 759db34 commit f996338
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 145 deletions.
62 changes: 29 additions & 33 deletions src/Discord.Net.Core/Entities/Permissions/ChannelPermission.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
namespace Discord
using System;

namespace Discord
{
public enum ChannelPermission : byte
[FlagsAttribute]
public enum ChannelPermission : ulong
{
//General
CreateInstantInvite = 0,
//KickMembers = 1,
//BanMembers = 2,
//Administrator = 3,
ManageChannel = 4,
//ManageGuild = 5,
// General
CreateInstantInvite = 0x00_00_00_01,
ManageChannels = 0x00_00_00_10,

//Text
AddReactions = 6,
ReadMessages = 10,
SendMessages = 11,
SendTTSMessages = 12,
ManageMessages = 13,
EmbedLinks = 14,
AttachFiles = 15,
ReadMessageHistory = 16,
MentionEveryone = 17,
UseExternalEmojis = 18,
// Text
AddReactions = 0x00_00_00_40,
ReadMessages = 0x00_00_04_00,
SendMessages = 0x00_00_08_00,
SendTTSMessages = 0x00_00_10_00,
ManageMessages = 0x00_00_20_00,
EmbedLinks = 0x00_00_40_00,
AttachFiles = 0x00_00_80_00,
ReadMessageHistory = 0x00_01_00_00,
MentionEveryone = 0x00_02_00_00,
UseExternalEmojis = 0x00_04_00_00,

//Voice
Connect = 20,
Speak = 21,
MuteMembers = 22,
DeafenMembers = 23,
MoveMembers = 24,
UseVAD = 25,
// Voice
Connect = 0x00_10_00_00,
Speak = 0x00_20_00_00,
MuteMembers = 0x00_40_00_00,
DeafenMembers = 0x00_80_00_00,
MoveMembers = 0x01_00_00_00,
UseVAD = 0x02_00_00_00,

//General2
//ChangeNickname = 26,
//ManageNicknames = 27,
ManagePermissions = 28,
ManageWebhooks = 29,
//ManageEmojis = 30
// More General
ManageRoles = 0x10_00_00_00,
ManageWebhooks = 0x20_00_00_00,
}
}
28 changes: 14 additions & 14 deletions src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static ChannelPermissions All(IChannel channel)
/// <summary> If True, a user may create invites. </summary>
public bool CreateInstantInvite => Permissions.GetValue(RawValue, ChannelPermission.CreateInstantInvite);
/// <summary> If True, a user may create, delete and modify this channel. </summary>
public bool ManageChannel => Permissions.GetValue(RawValue, ChannelPermission.ManageChannel);
public bool ManageChannel => Permissions.GetValue(RawValue, ChannelPermission.ManageChannels);

/// <summary> If true, a user may add reactions. </summary>
public bool AddReactions => Permissions.GetValue(RawValue, ChannelPermission.AddReactions);
Expand Down Expand Up @@ -72,8 +72,8 @@ public static ChannelPermissions All(IChannel channel)
/// <summary> If True, a user may use voice-activity-detection rather than push-to-talk. </summary>
public bool UseVAD => Permissions.GetValue(RawValue, ChannelPermission.UseVAD);

/// <summary> If True, a user may adjust permissions. This also implictly grants all other permissions. </summary>
public bool ManagePermissions => Permissions.GetValue(RawValue, ChannelPermission.ManagePermissions);
/// <summary> If True, a user may adjust role permissions. This also implictly grants all other permissions. </summary>
public bool ManageRoles => Permissions.GetValue(RawValue, ChannelPermission.ManageRoles);
/// <summary> If True, a user may edit the webhooks for this channel. </summary>
public bool ManageWebhooks => Permissions.GetValue(RawValue, ChannelPermission.ManageWebhooks);

Expand All @@ -85,12 +85,12 @@ private ChannelPermissions(ulong initialValue, bool? createInstantInvite = 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? useExternalEmojis = null, bool? connect = null, bool? speak = null, bool? muteMembers = null, bool? deafenMembers = null,
bool? moveMembers = null, bool? useVoiceActivation = null, bool? managePermissions = null, bool? manageWebhooks = null)
bool? moveMembers = null, bool? useVoiceActivation = null, bool? manageRoles = null, bool? manageWebhooks = null)
{
ulong value = initialValue;

Permissions.SetValue(ref value, createInstantInvite, ChannelPermission.CreateInstantInvite);
Permissions.SetValue(ref value, manageChannel, ChannelPermission.ManageChannel);
Permissions.SetValue(ref value, manageChannel, ChannelPermission.ManageChannels);
Permissions.SetValue(ref value, addReactions, ChannelPermission.AddReactions);
Permissions.SetValue(ref value, readMessages, ChannelPermission.ReadMessages);
Permissions.SetValue(ref value, sendMessages, ChannelPermission.SendMessages);
Expand All @@ -107,7 +107,7 @@ private ChannelPermissions(ulong initialValue, bool? createInstantInvite = null,
Permissions.SetValue(ref value, deafenMembers, ChannelPermission.DeafenMembers);
Permissions.SetValue(ref value, moveMembers, ChannelPermission.MoveMembers);
Permissions.SetValue(ref value, useVoiceActivation, ChannelPermission.UseVAD);
Permissions.SetValue(ref value, managePermissions, ChannelPermission.ManagePermissions);
Permissions.SetValue(ref value, manageRoles, ChannelPermission.ManageRoles);
Permissions.SetValue(ref value, manageWebhooks, ChannelPermission.ManageWebhooks);

RawValue = value;
Expand All @@ -119,10 +119,10 @@ public ChannelPermissions(bool createInstantInvite = false, bool manageChannel =
bool readMessages = false, bool sendMessages = false, bool sendTTSMessages = false, bool manageMessages = false,
bool embedLinks = false, bool attachFiles = false, bool readMessageHistory = false, bool mentionEveryone = false,
bool useExternalEmojis = false, bool connect = false, bool speak = false, bool muteMembers = false, bool deafenMembers = false,
bool moveMembers = false, bool useVoiceActivation = false, bool managePermissions = false, bool manageWebhooks = false)
bool moveMembers = false, bool useVoiceActivation = false, bool manageRoles = false, bool manageWebhooks = false)
: this(0, createInstantInvite, manageChannel, addReactions, readMessages, sendMessages, sendTTSMessages, manageMessages,
embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect,
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, managePermissions, manageWebhooks)
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, manageRoles, manageWebhooks)
{ }

/// <summary> Creates a new ChannelPermissions from this one, changing the provided non-null permissions. </summary>
Expand All @@ -131,21 +131,21 @@ public ChannelPermissions Modify(bool? createInstantInvite = null, bool? manageC
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 useExternalEmojis = false, bool? connect = null, bool? speak = null, bool? muteMembers = null, bool? deafenMembers = null,
bool? moveMembers = null, bool? useVoiceActivation = null, bool? managePermissions = null, bool? manageWebhooks = null)
bool? moveMembers = null, bool? useVoiceActivation = null, bool? manageRoles = null, bool? manageWebhooks = null)
=> new ChannelPermissions(RawValue, createInstantInvite, manageChannel, addReactions, readMessages, sendMessages, sendTTSMessages, manageMessages,
embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect,
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, managePermissions, manageWebhooks);
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, manageRoles, manageWebhooks);

public bool Has(ChannelPermission permission) => Permissions.GetValue(RawValue, permission);

public List<ChannelPermission> ToList()
{
var perms = new List<ChannelPermission>();
ulong x = 1;
for (byte i = 0; i < Permissions.MaxBits; i++, x <<= 1)
for (byte i = 0; i < Permissions.MaxBits; i++)
{
if ((RawValue & x) != 0)
perms.Add((ChannelPermission)i);
ulong flag = ((ulong)1 << i);
if ((RawValue & flag) != 0)
perms.Add((ChannelPermission)flag);
}
return perms;
}
Expand Down
70 changes: 37 additions & 33 deletions src/Discord.Net.Core/Entities/Permissions/GuildPermission.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
namespace Discord
using System;

namespace Discord
{
public enum GuildPermission : byte
[FlagsAttribute]
public enum GuildPermission : ulong
{
//General
CreateInstantInvite = 0,
KickMembers = 1,
BanMembers = 2,
Administrator = 3,
ManageChannels = 4,
ManageGuild = 5,
// General
CreateInstantInvite = 0x00_00_00_01,
KickMembers = 0x00_00_00_02,
BanMembers = 0x00_00_00_04,
Administrator = 0x00_00_00_08,
ManageChannels = 0x00_00_00_10,
ManageGuild = 0x00_00_00_20,

//Text
AddReactions = 6,
ReadMessages = 10,
SendMessages = 11,
SendTTSMessages = 12,
ManageMessages = 13,
EmbedLinks = 14,
AttachFiles = 15,
ReadMessageHistory = 16,
MentionEveryone = 17,
UseExternalEmojis = 18,
// Text
AddReactions = 0x00_00_00_40,
ViewAuditLog = 0x00_00_00_80,
ReadMessages = 0x00_00_04_00,
SendMessages = 0x00_00_08_00,
SendTTSMessages = 0x00_00_10_00,
ManageMessages = 0x00_00_20_00,
EmbedLinks = 0x00_00_40_00,
AttachFiles = 0x00_00_80_00,
ReadMessageHistory = 0x00_01_00_00,
MentionEveryone = 0x00_02_00_00,
UseExternalEmojis = 0x00_04_00_00,

//Voice
Connect = 20,
Speak = 21,
MuteMembers = 22,
DeafenMembers = 23,
MoveMembers = 24,
UseVAD = 25,
// Voice
Connect = 0x00_10_00_00,
Speak = 0x00_20_00_00,
MuteMembers = 0x00_40_00_00,
DeafenMembers = 0x00_80_00_00,
MoveMembers = 0x01_00_00_00,
UseVAD = 0x02_00_00_00,

//General2
ChangeNickname = 26,
ManageNicknames = 27,
ManageRoles = 28,
ManageWebhooks = 29,
ManageEmojis = 30
// General 2
ChangeNickname = 0x04_00_00_00,
ManageNicknames = 0x08_00_00_00,
ManageRoles = 0x10_00_00_00,
ManageWebhooks = 0x20_00_00_00,
ManageEmojis = 0x40_00_00_00
}
}
35 changes: 21 additions & 14 deletions src/Discord.Net.Core/Entities/Permissions/GuildPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct GuildPermissions
/// <summary> Gets a GuildPermissions that grants all guild permissions for webhook users. </summary>
public static readonly GuildPermissions Webhook = new GuildPermissions(0b00000_0000000_0001101100000_000000);
/// <summary> Gets a GuildPermissions that grants all guild permissions. </summary>
public static readonly GuildPermissions All = new GuildPermissions(0b11111_1111110_0111111110001_111111);
public static readonly GuildPermissions All = new GuildPermissions(0b11111_1111110_0111111110011_111111);

/// <summary> Gets a packed value representing all the permissions in this GuildPermissions. </summary>
public ulong RawValue { get; }
Expand All @@ -31,6 +31,9 @@ public struct GuildPermissions

/// <summary> If true, a user may add reactions. </summary>
public bool AddReactions => Permissions.GetValue(RawValue, GuildPermission.AddReactions);
/// <summary> If true, a user may view the audit log. </summary>
public bool ViewAuditLog => Permissions.GetValue(RawValue, GuildPermission.ViewAuditLog);

/// <summary> If True, a user may join channels. </summary>
public bool ReadMessages => Permissions.GetValue(RawValue, GuildPermission.ReadMessages);
/// <summary> If True, a user may send messages. </summary>
Expand Down Expand Up @@ -78,11 +81,11 @@ public struct GuildPermissions
public GuildPermissions(ulong rawValue) { RawValue = rawValue; }

private GuildPermissions(ulong initialValue, bool? createInstantInvite = null, bool? kickMembers = null,
bool? banMembers = null, bool? administrator = null, bool? manageChannel = null, bool? manageGuild = null,
bool? addReactions = null,
bool? banMembers = null, bool? administrator = null, bool? manageChannels = null, bool? manageGuild = null,
bool? addReactions = null, bool? viewAuditLog = 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? userExternalEmojis = null, bool? connect = null, bool? speak = null, bool? muteMembers = null, bool? deafenMembers = null,
bool? useExternalEmojis = 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, bool? manageWebhooks = null, bool? manageEmojis = null)
{
Expand All @@ -92,9 +95,10 @@ private GuildPermissions(ulong initialValue, bool? createInstantInvite = null, b
Permissions.SetValue(ref value, banMembers, GuildPermission.BanMembers);
Permissions.SetValue(ref value, kickMembers, GuildPermission.KickMembers);
Permissions.SetValue(ref value, administrator, GuildPermission.Administrator);
Permissions.SetValue(ref value, manageChannel, GuildPermission.ManageChannels);
Permissions.SetValue(ref value, manageChannels, GuildPermission.ManageChannels);
Permissions.SetValue(ref value, manageGuild, GuildPermission.ManageGuild);
Permissions.SetValue(ref value, addReactions, GuildPermission.AddReactions);
Permissions.SetValue(ref value, viewAuditLog, GuildPermission.ViewAuditLog);
Permissions.SetValue(ref value, readMessages, GuildPermission.ReadMessages);
Permissions.SetValue(ref value, sendMessages, GuildPermission.SendMessages);
Permissions.SetValue(ref value, sendTTSMessages, GuildPermission.SendTTSMessages);
Expand All @@ -103,7 +107,7 @@ private GuildPermissions(ulong initialValue, bool? createInstantInvite = null, b
Permissions.SetValue(ref value, attachFiles, GuildPermission.AttachFiles);
Permissions.SetValue(ref value, readMessageHistory, GuildPermission.ReadMessageHistory);
Permissions.SetValue(ref value, mentionEveryone, GuildPermission.MentionEveryone);
Permissions.SetValue(ref value, userExternalEmojis, GuildPermission.UseExternalEmojis);
Permissions.SetValue(ref value, useExternalEmojis, GuildPermission.UseExternalEmojis);
Permissions.SetValue(ref value, connect, GuildPermission.Connect);
Permissions.SetValue(ref value, speak, GuildPermission.Speak);
Permissions.SetValue(ref value, muteMembers, GuildPermission.MuteMembers);
Expand All @@ -122,26 +126,26 @@ private GuildPermissions(ulong initialValue, bool? createInstantInvite = null, b
/// <summary> Creates a new GuildPermissions with the provided permissions. </summary>
public GuildPermissions(bool createInstantInvite = false, bool kickMembers = false,
bool banMembers = false, bool administrator = false, bool manageChannels = false, bool manageGuild = false,
bool addReactions = false,
bool addReactions = false, bool viewAuditLog = false,
bool readMessages = false, bool sendMessages = false, bool sendTTSMessages = false, bool manageMessages = false,
bool embedLinks = false, bool attachFiles = false, bool readMessageHistory = false, bool mentionEveryone = false,
bool useExternalEmojis = false, bool connect = false, bool speak = false, bool muteMembers = false, bool deafenMembers = false,
bool moveMembers = false, bool useVoiceActivation = false, bool? changeNickname = false, bool? manageNicknames = false,
bool manageRoles = false, bool manageWebhooks = false, bool manageEmojis = false)
: this(0, createInstantInvite, manageRoles, kickMembers, banMembers, manageChannels, manageGuild, addReactions,
: this(0, createInstantInvite, manageRoles, kickMembers, banMembers, manageChannels, manageGuild, addReactions, viewAuditLog,
readMessages, sendMessages, sendTTSMessages, manageMessages, embedLinks, attachFiles, mentionEveryone, useExternalEmojis, connect,
manageWebhooks, manageEmojis) { }

/// <summary> Creates a new GuildPermissions from this one, changing the provided non-null permissions. </summary>
public GuildPermissions Modify(bool? createInstantInvite = null, bool? kickMembers = null,
bool? banMembers = null, bool? administrator = null, bool? manageChannels = null, bool? manageGuild = null,
bool? addReactions = null,
bool? addReactions = null, bool? viewAuditLog = 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? useExternalEmojis = 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, bool? manageWebhooks = null, bool? manageEmojis = null)
=> new GuildPermissions(RawValue, createInstantInvite, manageRoles, kickMembers, banMembers, manageChannels, manageGuild, addReactions,
=> new GuildPermissions(RawValue, createInstantInvite, manageRoles, kickMembers, banMembers, manageChannels, manageGuild, addReactions, viewAuditLog,
readMessages, sendMessages, sendTTSMessages, manageMessages, embedLinks, attachFiles, mentionEveryone, useExternalEmojis, connect,
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, changeNickname, manageNicknames, manageRoles,
manageWebhooks, manageEmojis);
Expand All @@ -151,11 +155,14 @@ public GuildPermissions Modify(bool? createInstantInvite = null, bool? kickMemb
public List<GuildPermission> ToList()
{
var perms = new List<GuildPermission>();
ulong x = 1;
for (byte i = 0; i < Permissions.MaxBits; i++, x <<= 1)

// bitwise operations on raw value
// each of the GuildPermissions increments by 2^i from 0 to MaxBits
for (byte i = 0; i < Permissions.MaxBits; i++)
{
if ((RawValue & x) != 0)
perms.Add((GuildPermission)i);
ulong flag = ((ulong)1 << i);
if ((RawValue & flag) != 0)
perms.Add((GuildPermission)flag);
}
return perms;
}
Expand Down
Loading

0 comments on commit f996338

Please sign in to comment.