Added PackedColor
This commit is contained in:
@@ -199,6 +199,9 @@
|
|||||||
<Compile Include="..\Discord.Net\Models\Message.cs">
|
<Compile Include="..\Discord.Net\Models\Message.cs">
|
||||||
<Link>Models\Message.cs</Link>
|
<Link>Models\Message.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="..\Discord.Net\Models\PackedColor.cs">
|
||||||
|
<Link>Models\PackedColor.cs</Link>
|
||||||
|
</Compile>
|
||||||
<Compile Include="..\Discord.Net\Models\PackedPermissions.cs">
|
<Compile Include="..\Discord.Net\Models\PackedPermissions.cs">
|
||||||
<Link>Models\PackedPermissions.cs</Link>
|
<Link>Models\PackedPermissions.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
@@ -270,7 +270,7 @@ namespace Discord
|
|||||||
|
|
||||||
return _rest.Delete(Endpoints.ServerRole(serverId, roleId));
|
return _rest.Delete(Endpoints.ServerRole(serverId, roleId));
|
||||||
}
|
}
|
||||||
public Task EditRole(string serverId, string roleId, string name = null, uint? permissions = null, bool? hoist = null, uint? color = null)
|
public Task EditRole(string serverId, string roleId, string name = null, uint? permissions = null, uint? color = null, bool? hoist = null)
|
||||||
{
|
{
|
||||||
if (serverId == null) throw new ArgumentNullException(nameof(serverId));
|
if (serverId == null) throw new ArgumentNullException(nameof(serverId));
|
||||||
if (roleId == null) throw new ArgumentNullException(nameof(roleId));
|
if (roleId == null) throw new ArgumentNullException(nameof(roleId));
|
||||||
|
|||||||
@@ -654,13 +654,13 @@ namespace Discord
|
|||||||
|
|
||||||
public Task EditRole(Role role, string newName)
|
public Task EditRole(Role role, string newName)
|
||||||
=> EditRole(role?.ServerId, role?.Id, newName);
|
=> EditRole(role?.ServerId, role?.Id, newName);
|
||||||
public Task EditRole(string serverId, string roleId, string name = null, PackedServerPermissions permissions = null, bool? hoist = null, uint? color = null)
|
public Task EditRole(string serverId, string roleId, string name = null, PackedServerPermissions permissions = null, PackedColor color = null, bool? hoist = null)
|
||||||
{
|
{
|
||||||
CheckReady();
|
CheckReady();
|
||||||
if (serverId == null) throw new NullReferenceException(nameof(serverId));
|
if (serverId == null) throw new NullReferenceException(nameof(serverId));
|
||||||
if (roleId == null) throw new NullReferenceException(nameof(roleId));
|
if (roleId == null) throw new NullReferenceException(nameof(roleId));
|
||||||
|
|
||||||
return _api.EditRole(serverId, roleId, name: name, permissions: permissions?.RawValue, hoist: hoist, color: color);
|
return _api.EditRole(serverId, roleId, name: name, permissions: permissions?.RawValue, color: color?.RawValue, hoist: hoist);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task DeleteRole(Role role)
|
public Task DeleteRole(Role role)
|
||||||
|
|||||||
46
src/Discord.Net/Models/PackedColor.cs
Normal file
46
src/Discord.Net/Models/PackedColor.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Discord
|
||||||
|
{
|
||||||
|
public class PackedColor
|
||||||
|
{
|
||||||
|
private bool _isLocked;
|
||||||
|
private uint _rawValue;
|
||||||
|
public uint RawValue
|
||||||
|
{
|
||||||
|
get { return _rawValue; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_isLocked)
|
||||||
|
throw new InvalidOperationException("Unable to edit cached permissions directly, use Copy() to make an editable copy.");
|
||||||
|
_rawValue = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PackedColor(uint rawValue) { _rawValue = rawValue; }
|
||||||
|
|
||||||
|
/// <summary> If True, a user may join channels. </summary>
|
||||||
|
public byte Red { get { return GetByte(3); } set { SetByte(3, value); } }
|
||||||
|
/// <summary> If True, a user may send messages. </summary>
|
||||||
|
public byte Green { get { return GetByte(2); } set { SetByte(2, value); } }
|
||||||
|
/// <summary> If True, a user may send text-to-speech messages. </summary>
|
||||||
|
public byte Blue { get { return GetByte(1); } set { SetByte(1, value); } }
|
||||||
|
|
||||||
|
internal void Lock() => _isLocked = true;
|
||||||
|
internal void SetRawValue(uint rawValue)
|
||||||
|
{
|
||||||
|
//Bypasses isLocked for API changes.
|
||||||
|
_rawValue = rawValue;
|
||||||
|
}
|
||||||
|
protected byte GetByte(int pos) => (byte)((_rawValue >> (8 * (pos - 1))) & 0xFF);
|
||||||
|
protected void SetByte(int pos, byte value)
|
||||||
|
{
|
||||||
|
if (_isLocked)
|
||||||
|
throw new InvalidOperationException("Unable to edit cached permissions directly, use Copy() to make an editable copy.");
|
||||||
|
|
||||||
|
int bit = 8 * (pos - 1);
|
||||||
|
uint mask = (uint)((1 << bit) - 1);
|
||||||
|
_rawValue = ((uint)value << bit) | (_rawValue & mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ namespace Discord
|
|||||||
/// <summary> If true, this role is displayed isolated from other users. </summary>
|
/// <summary> If true, this role is displayed isolated from other users. </summary>
|
||||||
public bool Hoist { get; private set; }
|
public bool Hoist { get; private set; }
|
||||||
/// <summary> Returns the color of this role. </summary>
|
/// <summary> Returns the color of this role. </summary>
|
||||||
public uint Color { get; private set; }
|
public PackedColor Color { get; private set; }
|
||||||
|
|
||||||
/// <summary> Returns the the permissions contained by this role. </summary>
|
/// <summary> Returns the the permissions contained by this role. </summary>
|
||||||
public PackedServerPermissions Permissions { get; }
|
public PackedServerPermissions Permissions { get; }
|
||||||
@@ -38,9 +38,11 @@ namespace Discord
|
|||||||
_client = client;
|
_client = client;
|
||||||
Id = id;
|
Id = id;
|
||||||
ServerId = serverId;
|
ServerId = serverId;
|
||||||
|
IsEveryone = isEveryone;
|
||||||
Permissions = new PackedServerPermissions(0);
|
Permissions = new PackedServerPermissions(0);
|
||||||
Permissions.Lock();
|
Permissions.Lock();
|
||||||
IsEveryone = isEveryone;
|
Color = new PackedColor(0);
|
||||||
|
Color.Lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Update(API.RoleInfo model)
|
internal void Update(API.RoleInfo model)
|
||||||
@@ -50,7 +52,7 @@ namespace Discord
|
|||||||
if (model.Hoist != null)
|
if (model.Hoist != null)
|
||||||
Hoist = model.Hoist.Value;
|
Hoist = model.Hoist.Value;
|
||||||
if (model.Color != null)
|
if (model.Color != null)
|
||||||
Color = model.Color.Value;
|
Color.SetRawValue(model.Color.Value);
|
||||||
if (model.Permissions != null)
|
if (model.Permissions != null)
|
||||||
Permissions.SetRawValue(model.Permissions.Value);
|
Permissions.SetRawValue(model.Permissions.Value);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user