| @@ -16,8 +16,8 @@ | |||||
| public static string ChannelInvites(string channelId) => $"channels/{channelId}/invites"; | public static string ChannelInvites(string channelId) => $"channels/{channelId}/invites"; | ||||
| public static string ChannelMessages(string channelId) => $"channels/{channelId}/messages"; | public static string ChannelMessages(string channelId) => $"channels/{channelId}/messages"; | ||||
| public static string ChannelMessages(string channelId, int limit) => $"channels/{channelId}/messages?limit={limit}"; | public static string ChannelMessages(string channelId, int limit) => $"channels/{channelId}/messages?limit={limit}"; | ||||
| public static string ChannelMessages(string channelId, int limit, string beforeId) => $"channels/{channelId}/messages?limit={limit}&before={beforeId}"; | |||||
| public static string ChannelMessage(string channelId, string msgId) => $"channels/{channelId}/messages/{msgId}"; | |||||
| public static string ChannelMessages(string channelId, int limit, string relativeId, string relativeDir) => $"channels/{channelId}/messages?limit={limit}&{relativeDir}={relativeId}"; | |||||
| public static string ChannelMessage(string channelId, string msgId) => $"channels/{channelId}/messages/{msgId}"; | |||||
| public static string ChannelMessageAck(string channelId, string msgId) => $"channels/{channelId}/messages/{msgId}/ack"; | public static string ChannelMessageAck(string channelId, string msgId) => $"channels/{channelId}/messages/{msgId}/ack"; | ||||
| public static string ChannelPermission(string channelId, string userOrRoleId) => $"channels/{channelId}/permissions/{userOrRoleId}"; | public static string ChannelPermission(string channelId, string userOrRoleId) => $"channels/{channelId}/permissions/{userOrRoleId}"; | ||||
| public static string ChannelTyping(string channelId) => $"channels/{channelId}/typing"; | public static string ChannelTyping(string channelId) => $"channels/{channelId}/typing"; | ||||
| @@ -1,5 +1,4 @@ | |||||
| using Discord.API; | using Discord.API; | ||||
| using Discord.Net; | |||||
| using Discord.Net.Rest; | using Discord.Net.Rest; | ||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| @@ -9,6 +8,8 @@ using System.Threading.Tasks; | |||||
| namespace Discord | namespace Discord | ||||
| { | { | ||||
| public enum RelativeDirection { Before, After } | |||||
| /// <summary> A lightweight wrapper around the Discord API. </summary> | /// <summary> A lightweight wrapper around the Discord API. </summary> | ||||
| public class DiscordAPIClient | public class DiscordAPIClient | ||||
| { | { | ||||
| @@ -92,12 +93,12 @@ namespace Discord | |||||
| var request = new ReorderChannelsRequest(channels); | var request = new ReorderChannelsRequest(channels); | ||||
| return _rest.Patch(Endpoints.ServerChannels(serverId), request); | return _rest.Patch(Endpoints.ServerChannels(serverId), request); | ||||
| } | } | ||||
| public Task<GetMessagesResponse> GetMessages(string channelId, int count, string beforeMessageId = null) | |||||
| public Task<GetMessagesResponse> GetMessages(string channelId, int count, string relativeMessageId = null, RelativeDirection relativeDir = RelativeDirection.Before) | |||||
| { | { | ||||
| if (channelId == null) throw new ArgumentNullException(nameof(channelId)); | if (channelId == null) throw new ArgumentNullException(nameof(channelId)); | ||||
| if (beforeMessageId != null) | |||||
| return _rest.Get<GetMessagesResponse>(Endpoints.ChannelMessages(channelId, count, beforeMessageId)); | |||||
| if (relativeMessageId != null) | |||||
| return _rest.Get<GetMessagesResponse>(Endpoints.ChannelMessages(channelId, count, relativeMessageId, relativeDir == RelativeDirection.Before ? "before" : "after")); | |||||
| else | else | ||||
| return _rest.Get<GetMessagesResponse>(Endpoints.ChannelMessages(channelId, count)); | return _rest.Get<GetMessagesResponse>(Endpoints.ChannelMessages(channelId, count)); | ||||
| } | } | ||||
| @@ -215,8 +215,9 @@ namespace Discord | |||||
| catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { } | catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { } | ||||
| } | } | ||||
| /// <summary> Downloads last count messages from the server, starting at beforeMessageId if it's provided. </summary> | |||||
| public async Task<Message[]> DownloadMessages(Channel channel, int count, string beforeMessageId = null, bool cache = true) | |||||
| /// <summary> Downloads last count messages from the server, returning all messages before or after relativeMessageId, if it's provided. </summary> | |||||
| public async Task<Message[]> DownloadMessages(Channel channel, int count, string relativeMessageId = null, RelativeDirection relativeDir = RelativeDirection.Before, bool cache = true) | |||||
| { | { | ||||
| if (channel == null) throw new ArgumentNullException(nameof(channel)); | if (channel == null) throw new ArgumentNullException(nameof(channel)); | ||||
| if (count < 0) throw new ArgumentNullException(nameof(count)); | if (count < 0) throw new ArgumentNullException(nameof(count)); | ||||
| @@ -227,8 +228,8 @@ namespace Discord | |||||
| { | { | ||||
| try | try | ||||
| { | { | ||||
| var msgs = await _api.GetMessages(channel.Id, count, beforeMessageId).ConfigureAwait(false); | |||||
| return msgs.Select(x => | |||||
| var msgs = await _api.GetMessages(channel.Id, count, relativeMessageId, relativeDir).ConfigureAwait(false); | |||||
| var result = msgs.Select(x => | |||||
| { | { | ||||
| Message msg = null; | Message msg = null; | ||||
| if (cache) | if (cache) | ||||