Browse Source

Added PackedColor

tags/docs-0.9
RogueException 9 years ago
parent
commit
bf49a7ff95
5 changed files with 57 additions and 6 deletions
  1. +3
    -0
      src/Discord.Net.Net45/Discord.Net.csproj
  2. +1
    -1
      src/Discord.Net/DiscordAPIClient.cs
  3. +2
    -2
      src/Discord.Net/DiscordClient.API.cs
  4. +46
    -0
      src/Discord.Net/Models/PackedColor.cs
  5. +5
    -3
      src/Discord.Net/Models/Role.cs

+ 3
- 0
src/Discord.Net.Net45/Discord.Net.csproj View File

@@ -199,6 +199,9 @@
<Compile Include="..\Discord.Net\Models\Message.cs">
<Link>Models\Message.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Models\PackedColor.cs">
<Link>Models\PackedColor.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Models\PackedPermissions.cs">
<Link>Models\PackedPermissions.cs</Link>
</Compile>


+ 1
- 1
src/Discord.Net/DiscordAPIClient.cs View File

@@ -270,7 +270,7 @@ namespace Discord

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 (roleId == null) throw new ArgumentNullException(nameof(roleId));


+ 2
- 2
src/Discord.Net/DiscordClient.API.cs View File

@@ -654,13 +654,13 @@ 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, 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();
if (serverId == null) throw new NullReferenceException(nameof(serverId));
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)


+ 46
- 0
src/Discord.Net/Models/PackedColor.cs View 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);
}
}
}

+ 5
- 3
src/Discord.Net/Models/Role.cs View File

@@ -15,7 +15,7 @@ namespace Discord
/// <summary> If true, this role is displayed isolated from other users. </summary>
public bool Hoist { get; private set; }
/// <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>
public PackedServerPermissions Permissions { get; }
@@ -38,9 +38,11 @@ namespace Discord
_client = client;
Id = id;
ServerId = serverId;
IsEveryone = isEveryone;
Permissions = new PackedServerPermissions(0);
Permissions.Lock();
IsEveryone = isEveryone;
Color = new PackedColor(0);
Color.Lock();
}

internal void Update(API.RoleInfo model)
@@ -50,7 +52,7 @@ namespace Discord
if (model.Hoist != null)
Hoist = model.Hoist.Value;
if (model.Color != null)
Color = model.Color.Value;
Color.SetRawValue(model.Color.Value);
if (model.Permissions != null)
Permissions.SetRawValue(model.Permissions.Value);



Loading…
Cancel
Save