Browse Source

Added channel reordering

tags/docs-0.9
RogueException 9 years ago
parent
commit
67a17438ac
3 changed files with 77 additions and 7 deletions
  1. +16
    -0
      src/Discord.Net/API/Requests.cs
  2. +12
    -0
      src/Discord.Net/DiscordAPIClient.cs
  3. +49
    -7
      src/Discord.Net/DiscordClient.API.cs

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

@@ -4,6 +4,7 @@

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections;

namespace Discord.API
{
@@ -43,6 +44,21 @@ namespace Discord.API
[JsonProperty("topic", NullValueHandling = NullValueHandling.Ignore)]
public string Topic;
}
internal sealed class ReorderChannelsRequest : IEnumerable<ReorderChannelsRequest.Channel>
{
public sealed class Channel
{
[JsonProperty("id")]
public string Id;
[JsonProperty("position")]
public uint Position;
}
private IEnumerable<Channel> _channels;
public ReorderChannelsRequest(IEnumerable<Channel> channels) { _channels = channels; }

public IEnumerator<Channel> GetEnumerator() =>_channels.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _channels.GetEnumerator();
}

//Invites
internal sealed class CreateInviteRequest


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

@@ -1,6 +1,7 @@
using Discord.API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

@@ -85,6 +86,17 @@ namespace Discord
var request = new EditChannelRequest { Name = name, Topic = topic };
return _rest.Patch<EditChannelResponse>(Endpoints.Channel(channelId), request);
}
public Task ReorderChannels(string serverId, int startPos, IEnumerable<string> channelIds)
{
if (serverId == null) throw new ArgumentNullException(nameof(serverId));
if (channelIds == null) throw new ArgumentNullException(nameof(channelIds));
if (startPos < 0) throw new ArgumentOutOfRangeException(nameof(startPos), "startPos must be a positive integer.");

uint pos = (uint)startPos;
var channels = channelIds.Select(x => new ReorderChannelsRequest.Channel { Id = x, Position = pos++ });
var request = new ReorderChannelsRequest(channels);
return _rest.Patch(Endpoints.ServerChannels(serverId), request);
}
public Task<GetMessagesResponse> GetMessages(string channelId, int count)
{
if (channelId == null) throw new ArgumentNullException(nameof(channelId));


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

@@ -108,16 +108,58 @@ namespace Discord
}

/// <summary> Edits the provided channel, changing only non-null attributes. </summary>
public Task EditChannel(Channel channel, string name = null, string topic = null)
=> EditChannel(channel?.Id, name: name, topic: topic);
public Task EditChannel(string channelId, string name = null, string topic = null, int? position = null)
=> EditChannel(_channels[channelId], name: name, topic: topic, position: position);
/// <summary> Edits the provided channel, changing only non-null attributes. </summary>
public Task EditChannel(string channelId, string name = null, string topic = null)
public async Task EditChannel(Channel channel, string name = null, string topic = null, int? position = null)
{
CheckReady();
if (channelId == null) throw new ArgumentNullException(nameof(channelId));
if (topic == null) throw new ArgumentNullException(nameof(topic));
if (channel == null) throw new ArgumentNullException(nameof(channel));

await _api.EditChannel(channel.Id, name: name, topic: topic);

if (position != null)
{
int oldPos = channel.Position;
int newPos = position.Value;
int minPos;
Channel[] channels = channel.Server.Channels.OrderBy(x => x.Position).ToArray();

return _api.EditChannel(channelId, name: name, topic: topic);
if (oldPos < newPos) //Moving Down
{
minPos = oldPos;
for (int i = oldPos; i < newPos; i++)
channels[i] = channels[i + 1];
channels[newPos] = channel;
}
else //(oldPos > newPos) Moving Up
{
minPos = newPos;
for (int i = oldPos; i > newPos; i--)
channels[i] = channels[i - 1];
channels[newPos] = channel;
}
await _api.ReorderChannels(channel.ServerId, minPos, channels.Skip(minPos).Select(x => x.Id));
}
}

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

return _api.ReorderChannels(serverId, startPos, channels.Select(x =>
{
if (x is string)
return x as string;
else if (x is Channel)
return (x as Role).Id;
else
throw new ArgumentException("Channels must be a collection of string or Channel.", nameof(channels));
}));
}

/// <summary> Destroys the provided channel. </summary>
@@ -243,7 +285,7 @@ namespace Discord
else if (x is Role)
return (x as Role).Id;
else
throw new ArgumentException("Roles must be a collection of strings or roles.", nameof(roles));
throw new ArgumentException("Roles must be a collection of string or Role.", nameof(roles));
});

return _api.EditMember(serverId, userId, mute, deaf, newRoles);


Loading…
Cancel
Save