Browse Source

Added role reordering

tags/docs-0.9
RogueException 9 years ago
parent
commit
356bcc2a65
5 changed files with 81 additions and 4 deletions
  1. +2
    -0
      src/Discord.Net/API/Common.cs
  2. +15
    -0
      src/Discord.Net/API/Requests.cs
  3. +11
    -0
      src/Discord.Net/DiscordAPIClient.cs
  4. +49
    -4
      src/Discord.Net/DiscordClient.API.cs
  5. +4
    -0
      src/Discord.Net/Models/Role.cs

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

@@ -263,6 +263,8 @@ namespace Discord.API
public uint? Permissions;
[JsonProperty("name")]
public string Name;
[JsonProperty("position")]
public int? Position;
[JsonProperty("hoist")]
public bool? Hoist;
[JsonProperty("color")]


+ 15
- 0
src/Discord.Net/API/Requests.cs View File

@@ -144,6 +144,21 @@ namespace Discord.API
[JsonProperty("color", NullValueHandling = NullValueHandling.Ignore)]
public uint? Color;
}
internal sealed class ReorderRolesRequest : IEnumerable<ReorderRolesRequest.Role>
{
public sealed class Role
{
[JsonProperty("id")]
public string Id;
[JsonProperty("position")]
public uint Position;
}
private IEnumerable<Role> _roles;
public ReorderRolesRequest(IEnumerable<Role> roles) { _roles = roles; }

public IEnumerator<Role> GetEnumerator() => _roles.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _roles.GetEnumerator();
}

//Servers
internal sealed class CreateServerRequest


+ 11
- 0
src/Discord.Net/DiscordAPIClient.cs View File

@@ -278,6 +278,17 @@ namespace Discord
var request = new EditRoleRequest { Name = name, Permissions = permissions, Hoist = hoist, Color = color };
return _rest.Patch(Endpoints.ServerRole(serverId, roleId), request);
}
public Task ReorderRoles(string serverId, IEnumerable<string> roleIds, int startPos = 0)
{
if (serverId == null) throw new ArgumentNullException(nameof(serverId));
if (roleIds == null) throw new ArgumentNullException(nameof(roleIds));
if (startPos < 0) throw new ArgumentOutOfRangeException(nameof(startPos), "startPos must be a positive integer.");

uint pos = (uint)startPos;
var roles = roleIds.Select(x => new ReorderRolesRequest.Role { Id = x, Position = pos++ });
var request = new ReorderRolesRequest(roles);
return _rest.Patch(Endpoints.ServerRoles(serverId), request);
}

//Servers
public Task<CreateServerResponse> CreateServer(string name, string region)


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

@@ -652,19 +652,43 @@ namespace Discord
return _api.CreateRole(serverId);
}

public Task EditRole(string roleId, string name = null, PackedServerPermissions permissions = null, PackedColor color = null, bool? hoist = null)
=> EditRole(_roles[roleId], name: name, permissions: permissions, color: color, hoist: hoist);
public Task EditRole(Role role, string name = null, PackedServerPermissions permissions = null, PackedColor color = null, bool? hoist = null)
public Task EditRole(string roleId, string name = null, PackedServerPermissions permissions = null, PackedColor color = null, bool? hoist = null, int? position = null)
=> EditRole(_roles[roleId], name: name, permissions: permissions, color: color, hoist: hoist, position: position);
public async Task EditRole(Role role, string name = null, PackedServerPermissions permissions = null, PackedColor color = null, bool? hoist = null, int? position = null)
{
CheckReady();
if (role == null) throw new NullReferenceException(nameof(role));

//TODO: Stop defaulting to cache variables once the server stops 500ing at us
return _api.EditRole(role.ServerId, role.Id,
await _api.EditRole(role.ServerId, role.Id,
name: name ?? role.Name,
permissions: permissions?.RawValue ?? role.Permissions.RawValue,
color: color?.RawValue ?? role.Color.RawValue,
hoist: hoist ?? role.Hoist);

if (position != null)
{
int oldPos = role.Position;
int newPos = position.Value;
int minPos;
Role[] roles = role.Server.Roles.OrderBy(x => x.Position).ToArray();

if (oldPos < newPos) //Moving Down
{
minPos = oldPos;
for (int i = oldPos; i < newPos; i++)
roles[i] = roles[i + 1];
roles[newPos] = role;
}
else //(oldPos > newPos) Moving Up
{
minPos = newPos;
for (int i = oldPos; i > newPos; i--)
roles[i] = roles[i - 1];
roles[newPos] = role;
}
await _api.ReorderRoles(role.ServerId, roles.Skip(minPos).Select(x => x.Id), minPos);
}
}

public Task DeleteRole(Role role)
@@ -678,6 +702,27 @@ namespace Discord
return _api.DeleteRole(serverId, roleId);
}

public Task ReorderRoles(Server server, IEnumerable<object> roles, int startPos = 0)
=> ReorderChannels(server.Id, roles, startPos);
public Task ReorderRoles(string serverId, IEnumerable<object> roles, int startPos = 0)
{
if (serverId == null) throw new ArgumentNullException(nameof(serverId));
if (roles == null) throw new ArgumentNullException(nameof(roles));
if (startPos < 0) throw new ArgumentOutOfRangeException(nameof(startPos), "startPos must be a positive integer.");

var roleIds = roles.Select(x =>
{
if (x is string)
return x as string;
else if (x is Role)
return (x as Role).Id;
else
throw new ArgumentException("Channels must be a collection of string or Role.", nameof(roles));
});

return _api.ReorderRoles(serverId, roleIds, startPos);
}

//Servers
/// <summary> Creates a new server with the provided name and region (see Regions). </summary>
public async Task<Server> CreateServer(string name, string region)


+ 4
- 0
src/Discord.Net/Models/Role.cs View File

@@ -14,6 +14,8 @@ namespace Discord
public string Name { get; private set; }
/// <summary> If true, this role is displayed isolated from other users. </summary>
public bool Hoist { get; private set; }
/// <summary> Returns the position of this channel in the role list for this server. </summary>
public int Position { get; private set; }
/// <summary> Returns the color of this role. </summary>
public PackedColor Color { get; private set; }

@@ -51,6 +53,8 @@ namespace Discord
Name = model.Name;
if (model.Hoist != null)
Hoist = model.Hoist.Value;
if (model.Position != null)
Position = model.Position.Value;
if (model.Color != null)
Color.SetRawValue(model.Color.Value);
if (model.Permissions != null)


Loading…
Cancel
Save