Separated Channel and Server permissions
This commit is contained in:
@@ -444,29 +444,29 @@ namespace Discord
|
||||
}
|
||||
|
||||
//Permissions
|
||||
public Task SetChannelUserPermissions(Channel channel, Member member, PackedPermissions allow, PackedPermissions deny)
|
||||
public Task SetChannelUserPermissions(Channel channel, Member member, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
=> SetChannelPermissions(channel?.Id, member?.UserId, "member", allow, deny);
|
||||
public Task SetChannelUserPermissions(string channelId, Member member, PackedPermissions allow, PackedPermissions deny)
|
||||
public Task SetChannelUserPermissions(string channelId, Member member, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
=> SetChannelPermissions(channelId, member?.UserId, "member", allow, deny);
|
||||
public Task SetChannelUserPermissions(Channel channel, User user, PackedPermissions allow, PackedPermissions deny)
|
||||
public Task SetChannelUserPermissions(Channel channel, User user, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
=> SetChannelPermissions(channel?.Id, user?.Id, "member", allow, deny);
|
||||
public Task SetChannelUserPermissions(string channelId, User user, PackedPermissions allow, PackedPermissions deny)
|
||||
public Task SetChannelUserPermissions(string channelId, User user, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
=> SetChannelPermissions(channelId, user?.Id, "member", allow, deny);
|
||||
public Task SetChannelUserPermissions(Channel channel, string userId, PackedPermissions allow, PackedPermissions deny)
|
||||
public Task SetChannelUserPermissions(Channel channel, string userId, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
=> SetChannelPermissions(channel?.Id, userId, "member", allow, deny);
|
||||
public Task SetChannelUserPermissions(string channelId, string userId, PackedPermissions allow, PackedPermissions deny)
|
||||
public Task SetChannelUserPermissions(string channelId, string userId, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
=> SetChannelPermissions(channelId, userId, "member", allow, deny);
|
||||
|
||||
public Task SetChannelRolePermissions(Channel channel, Role role, PackedPermissions allow, PackedPermissions deny)
|
||||
public Task SetChannelRolePermissions(Channel channel, Role role, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
=> SetChannelPermissions(channel?.Id, role?.Id, "role", allow, deny);
|
||||
public Task SetChannelRolePermissions(string channelId, Role role, PackedPermissions allow, PackedPermissions deny)
|
||||
public Task SetChannelRolePermissions(string channelId, Role role, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
=> SetChannelPermissions(channelId, role?.Id, "role", allow, deny);
|
||||
public Task SetChannelRolePermissions(Channel channel, string userId, PackedPermissions allow, PackedPermissions deny)
|
||||
public Task SetChannelRolePermissions(Channel channel, string userId, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
=> SetChannelPermissions(channel?.Id, userId, "role", allow, deny);
|
||||
public Task SetChannelRolePermissions(string channelId, string userId, PackedPermissions allow, PackedPermissions deny)
|
||||
public Task SetChannelRolePermissions(string channelId, string userId, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
=> SetChannelPermissions(channelId, userId, "role", allow, deny);
|
||||
|
||||
private Task SetChannelPermissions(string channelId, string userOrRoleId, string idType, PackedPermissions allow, PackedPermissions deny)
|
||||
private Task SetChannelPermissions(string channelId, string userOrRoleId, string idType, PackedChannelPermissions allow, PackedChannelPermissions deny)
|
||||
{
|
||||
CheckReady();
|
||||
if (channelId == null) throw new NullReferenceException(nameof(channelId));
|
||||
@@ -538,7 +538,7 @@ namespace Discord
|
||||
|
||||
public Task EditRole(Role role, string newName)
|
||||
=> EditRole(role?.ServerId, role?.Id, newName);
|
||||
public Task EditRole(string serverId, string roleId, string name = null, PackedPermissions permissions = null)
|
||||
public Task EditRole(string serverId, string roleId, string name = null, PackedServerPermissions permissions = null)
|
||||
{
|
||||
CheckReady();
|
||||
if (serverId == null) throw new NullReferenceException(nameof(serverId));
|
||||
|
||||
@@ -11,8 +11,8 @@ namespace Discord
|
||||
{
|
||||
public string Type { get; internal set; }
|
||||
public string Id { get; internal set; }
|
||||
public PackedPermissions Deny { get; internal set; }
|
||||
public PackedPermissions Allow { get; internal set; }
|
||||
public PackedChannelPermissions Deny { get; internal set; }
|
||||
public PackedChannelPermissions Allow { get; internal set; }
|
||||
}
|
||||
|
||||
private readonly DiscordClient _client;
|
||||
@@ -100,8 +100,8 @@ namespace Discord
|
||||
{
|
||||
Type = x.Type,
|
||||
Id = x.Id,
|
||||
Deny = new PackedPermissions(true, x.Deny),
|
||||
Allow = new PackedPermissions(true, x.Allow)
|
||||
Deny = new PackedChannelPermissions(true, x.Deny),
|
||||
Allow = new PackedChannelPermissions(true, x.Allow)
|
||||
}).ToArray();
|
||||
}
|
||||
else
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Discord
|
||||
public class Member
|
||||
{
|
||||
private readonly DiscordClient _client;
|
||||
private ConcurrentDictionary<string, PackedPermissions> _permissions;
|
||||
private ConcurrentDictionary<string, PackedChannelPermissions> _permissions;
|
||||
|
||||
/// <summary> Returns the name of this user on this server. </summary>
|
||||
public string Name { get; internal set; }
|
||||
@@ -67,7 +67,7 @@ namespace Discord
|
||||
UserId = userId;
|
||||
ServerId = serverId;
|
||||
Status = UserStatus.Offline;
|
||||
_permissions = new ConcurrentDictionary<string, PackedPermissions>();
|
||||
_permissions = new ConcurrentDictionary<string, PackedChannelPermissions>();
|
||||
}
|
||||
|
||||
public override string ToString() => UserId;
|
||||
@@ -137,12 +137,12 @@ namespace Discord
|
||||
|
||||
internal void AddChannel(string channelId)
|
||||
{
|
||||
_permissions.TryAdd(channelId, new PackedPermissions());
|
||||
_permissions.TryAdd(channelId, new PackedChannelPermissions());
|
||||
UpdatePermissions(channelId);
|
||||
}
|
||||
internal bool RemoveChannel(string channelId)
|
||||
{
|
||||
PackedPermissions ignored;
|
||||
PackedChannelPermissions ignored;
|
||||
return _permissions.TryRemove(channelId, out ignored);
|
||||
}
|
||||
internal void UpdatePermissions()
|
||||
@@ -161,7 +161,7 @@ namespace Discord
|
||||
var serverOverwrites = channel.PermissionOverwrites;
|
||||
var channelOverwrites = channel.PermissionOverwrites;
|
||||
|
||||
PackedPermissions permissions;
|
||||
PackedChannelPermissions permissions;
|
||||
if (!_permissions.TryGetValue(channelId, out permissions)) return;
|
||||
uint newPermissions = 0x0;
|
||||
|
||||
@@ -182,9 +182,10 @@ namespace Discord
|
||||
channel._areMembersStale = true;
|
||||
}
|
||||
}
|
||||
public PackedPermissions GetPermissions(string channelId)
|
||||
//TODO: Add GetServerPermissions
|
||||
public PackedChannelPermissions GetPermissions(string channelId)
|
||||
{
|
||||
PackedPermissions perms;
|
||||
PackedChannelPermissions perms;
|
||||
if (_permissions.TryGetValue(channelId, out perms))
|
||||
return perms;
|
||||
return null;
|
||||
|
||||
@@ -2,31 +2,54 @@
|
||||
|
||||
namespace Discord
|
||||
{
|
||||
public sealed class PackedPermissions
|
||||
public sealed class PackedServerPermissions : PackedPermissions
|
||||
{
|
||||
private bool _isLocked;
|
||||
private uint _rawValue;
|
||||
public uint RawValue { get { return _rawValue; } internal set { _rawValue = value; } } //Internal set bypasses isLocked for API changes.
|
||||
public PackedServerPermissions() : base(false, 0) { }
|
||||
internal PackedServerPermissions(bool isLocked, uint rawValue) : base(isLocked, rawValue) { }
|
||||
|
||||
public PackedPermissions() { _isLocked = false; }
|
||||
internal PackedPermissions(bool isLocked) { _isLocked = isLocked; }
|
||||
internal PackedPermissions(bool isLocked, uint rawValue) { _isLocked = isLocked; _rawValue = rawValue; }
|
||||
|
||||
/// <summary> If True, a user may create invites. </summary>
|
||||
public bool General_CreateInstantInvite { get { return GetBit(1); } set { SetBit(1, value); } }
|
||||
/// <summary> If True, a user may ban users from the server. </summary>
|
||||
public bool General_BanMembers { get { return GetBit(2); } set { SetBit(2, value); } }
|
||||
/// <summary> If True, a user may kick users from the server. </summary>
|
||||
public bool General_KickMembers { get { return GetBit(3); } set { SetBit(3, value); } }
|
||||
/// <summary> (Server Roles only) If True, a user may adjust roles. This also implictly grants all other permissions. </summary>
|
||||
/// <summary> If True, a user may adjust roles. This also implictly grants all other permissions. </summary>
|
||||
public bool General_ManageRoles { get { return GetBit(4); } set { SetBit(4, value); } }
|
||||
/// <summary> (Channels only) If True, a user may adjust permissions. This also implictly grants all other permissions. </summary>
|
||||
public bool General_ManagePermissions { get { return GetBit(4); } set { SetBit(4, value); } }
|
||||
/// <summary> If True, a user may create, delete and modify channels. </summary>
|
||||
public bool General_ManageChannels { get { return GetBit(5); } set { SetBit(5, value); } }
|
||||
/// <summary> If True, a user may adjust server properties. </summary>
|
||||
public bool General_ManageServer { get { return GetBit(6); } set { SetBit(6, value); } }
|
||||
|
||||
public PackedServerPermissions Copy() => new PackedServerPermissions(false, _rawValue);
|
||||
}
|
||||
|
||||
public sealed class PackedChannelPermissions : PackedPermissions
|
||||
{
|
||||
public PackedChannelPermissions() : base(false, 0) { }
|
||||
internal PackedChannelPermissions(bool isLocked, uint rawValue) : base(isLocked, rawValue) { }
|
||||
|
||||
/// <summary> If True, a user may adjust permissions. This also implictly grants all other permissions. </summary>
|
||||
public bool General_ManagePermissions { get { return GetBit(4); } set { SetBit(4, value); } }
|
||||
/// <summary> If True, a user may create, delete and modify this channel. </summary>
|
||||
public bool General_ManageChannel { get { return GetBit(5); } set { SetBit(5, value); } }
|
||||
|
||||
public PackedChannelPermissions Copy() => new PackedChannelPermissions(false, _rawValue);
|
||||
}
|
||||
|
||||
public abstract class PackedPermissions
|
||||
{
|
||||
private bool _isLocked;
|
||||
protected uint _rawValue;
|
||||
public uint RawValue { get { return _rawValue; } internal set { _rawValue = value; } } //Internal set bypasses isLocked for API changes.
|
||||
|
||||
protected PackedPermissions(bool isLocked, uint rawValue) { _isLocked = isLocked; _rawValue = rawValue; }
|
||||
|
||||
/// <summary> If True, a user may create invites. </summary>
|
||||
public bool General_CreateInstantInvite { get { return GetBit(1); } set { SetBit(1, value); } }
|
||||
//Bit 2 = BanMembers/???
|
||||
//Bit 3 = KickMembers/???
|
||||
//Bit 4 = ManageRoles/ManagePermissions
|
||||
//Bit 5 = ManageChannels/ManageChannel
|
||||
//Bit 6 = ManageServer/???
|
||||
|
||||
//4 Unused
|
||||
|
||||
/// <summary> If True, a user may join channels. </summary>
|
||||
@@ -63,8 +86,8 @@ namespace Discord
|
||||
|
||||
//6 Unused
|
||||
|
||||
private bool GetBit(int pos) => ((_rawValue >> (pos - 1)) & 1U) == 1;
|
||||
private void SetBit(int pos, bool value)
|
||||
protected bool GetBit(int pos) => ((_rawValue >> (pos - 1)) & 1U) == 1;
|
||||
protected void SetBit(int pos, bool value)
|
||||
{
|
||||
if (_isLocked)
|
||||
throw new InvalidOperationException("Unable to edit cached permissions directly, use Copy() to make an editable copy.");
|
||||
@@ -73,7 +96,5 @@ namespace Discord
|
||||
else
|
||||
_rawValue &= ~(1U << (pos - 1));
|
||||
}
|
||||
|
||||
public PackedPermissions Copy() => new PackedPermissions(false, _rawValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace Discord
|
||||
public string Name { get; internal set; }
|
||||
|
||||
/// <summary> Returns the the permissions contained by this role. </summary>
|
||||
public PackedPermissions Permissions { get; }
|
||||
public PackedServerPermissions Permissions { get; }
|
||||
|
||||
/// <summary> Returns the id of the server this role is a member of. </summary>
|
||||
public string ServerId { get; }
|
||||
@@ -34,7 +34,7 @@ namespace Discord
|
||||
_client = client;
|
||||
Id = id;
|
||||
ServerId = serverId;
|
||||
Permissions = new PackedPermissions(true);
|
||||
Permissions = new PackedServerPermissions(true, 0);
|
||||
IsEveryone = isEveryone;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user