Added channel reordering
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
namespace Discord.API
|
namespace Discord.API
|
||||||
{
|
{
|
||||||
@@ -43,6 +44,21 @@ namespace Discord.API
|
|||||||
[JsonProperty("topic", NullValueHandling = NullValueHandling.Ignore)]
|
[JsonProperty("topic", NullValueHandling = NullValueHandling.Ignore)]
|
||||||
public string Topic;
|
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
|
//Invites
|
||||||
internal sealed class CreateInviteRequest
|
internal sealed class CreateInviteRequest
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using Discord.API;
|
using Discord.API;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -85,6 +86,17 @@ namespace Discord
|
|||||||
var request = new EditChannelRequest { Name = name, Topic = topic };
|
var request = new EditChannelRequest { Name = name, Topic = topic };
|
||||||
return _rest.Patch<EditChannelResponse>(Endpoints.Channel(channelId), request);
|
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)
|
public Task<GetMessagesResponse> GetMessages(string channelId, int count)
|
||||||
{
|
{
|
||||||
if (channelId == null) throw new ArgumentNullException(nameof(channelId));
|
if (channelId == null) throw new ArgumentNullException(nameof(channelId));
|
||||||
|
|||||||
@@ -108,16 +108,58 @@ namespace Discord
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Edits the provided channel, changing only non-null attributes. </summary>
|
/// <summary> Edits the provided channel, changing only non-null attributes. </summary>
|
||||||
public Task EditChannel(Channel channel, string name = null, string topic = null)
|
public Task EditChannel(string channelId, string name = null, string topic = null, int? position = null)
|
||||||
=> EditChannel(channel?.Id, name: name, topic: topic);
|
=> EditChannel(_channels[channelId], name: name, topic: topic, position: position);
|
||||||
/// <summary> Edits the provided channel, changing only non-null attributes. </summary>
|
/// <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();
|
CheckReady();
|
||||||
if (channelId == null) throw new ArgumentNullException(nameof(channelId));
|
if (channel == null) throw new ArgumentNullException(nameof(channel));
|
||||||
if (topic == null) throw new ArgumentNullException(nameof(topic));
|
|
||||||
|
|
||||||
return _api.EditChannel(channelId, name: name, topic: topic);
|
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();
|
||||||
|
|
||||||
|
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>
|
/// <summary> Destroys the provided channel. </summary>
|
||||||
@@ -243,7 +285,7 @@ namespace Discord
|
|||||||
else if (x is Role)
|
else if (x is Role)
|
||||||
return (x as Role).Id;
|
return (x as Role).Id;
|
||||||
else
|
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);
|
return _api.EditMember(serverId, userId, mute, deaf, newRoles);
|
||||||
|
|||||||
Reference in New Issue
Block a user