Cleaned up paged enumerables

This commit is contained in:
RogueException
2016-10-04 14:16:18 -03:00
parent b6ed02f88a
commit a93ddbaeb2
16 changed files with 79 additions and 107 deletions

View File

@@ -19,6 +19,8 @@ namespace Discord.Rest
IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch); IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch);
/// <summary> Gets a collection of messages in this channel. </summary> /// <summary> Gets a collection of messages in this channel. </summary>
IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch); IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch);
/// <summary> Gets a collection of messages in this channel. </summary>
IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch);
/// <summary> Gets a collection of pinned messages in this channel. </summary> /// <summary> Gets a collection of pinned messages in this channel. </summary>
new Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(); new Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync();
} }

View File

@@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -50,6 +49,6 @@ namespace Discord.Rest
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode) Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode)
=> Task.FromResult<IUser>(null); //Overriden => Task.FromResult<IUser>(null); //Overriden
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode)
=> ImmutableArray.Create<IReadOnlyCollection<IUser>>().ToAsyncEnumerable(); //Overriden => AsyncEnumerable.Empty<IReadOnlyCollection<IUser>>(); //Overriden
} }
} }

View File

@@ -99,14 +99,14 @@ namespace Discord.Rest
if (mode == CacheMode.AllowDownload) if (mode == CacheMode.AllowDownload)
return GetMessagesAsync(limit); return GetMessagesAsync(limit);
else else
return ImmutableArray.Create<IReadOnlyCollection<IMessage>>().ToAsyncEnumerable(); return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
} }
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode)
{ {
if (mode == CacheMode.AllowDownload) if (mode == CacheMode.AllowDownload)
return GetMessagesAsync(fromMessageId, dir, limit); return GetMessagesAsync(fromMessageId, dir, limit);
else else
return ImmutableArray.Create<IReadOnlyCollection<IMessage>>().ToAsyncEnumerable(); return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
} }
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync() async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync()
=> await GetPinnedMessagesAsync().ConfigureAwait(false); => await GetPinnedMessagesAsync().ConfigureAwait(false);

View File

@@ -105,14 +105,14 @@ namespace Discord.Rest
if (mode == CacheMode.AllowDownload) if (mode == CacheMode.AllowDownload)
return GetMessagesAsync(limit); return GetMessagesAsync(limit);
else else
return ImmutableArray.Create<IReadOnlyCollection<IMessage>>().ToAsyncEnumerable(); return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
} }
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode)
{ {
if (mode == CacheMode.AllowDownload) if (mode == CacheMode.AllowDownload)
return GetMessagesAsync(fromMessageId, dir, limit); return GetMessagesAsync(fromMessageId, dir, limit);
else else
return ImmutableArray.Create<IReadOnlyCollection<IMessage>>().ToAsyncEnumerable(); return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
} }
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync() async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync()
=> await GetPinnedMessagesAsync(); => await GetPinnedMessagesAsync();

View File

@@ -138,13 +138,13 @@ namespace Discord.Rest
=> await RemovePermissionOverwriteAsync(user); => await RemovePermissionOverwriteAsync(user);
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode)
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>().ToAsyncEnumerable(); //Overriden in Text/Voice //TODO: Does this actually override? => AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>(); //Overriden //Overriden in Text/Voice //TODO: Does this actually override?
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode) Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode)
=> Task.FromResult<IGuildUser>(null); //Overriden in Text/Voice //TODO: Does this actually override? => Task.FromResult<IGuildUser>(null); //Overriden in Text/Voice //TODO: Does this actually override?
//IChannel //IChannel
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode)
=> ImmutableArray.Create<IReadOnlyCollection<IUser>>().ToAsyncEnumerable(); //Overriden in Text/Voice //TODO: Does this actually override? => AsyncEnumerable.Empty<IReadOnlyCollection<IUser>>(); //Overriden in Text/Voice //TODO: Does this actually override?
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode) Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode)
=> Task.FromResult<IUser>(null); //Overriden in Text/Voice //TODO: Does this actually override? => Task.FromResult<IUser>(null); //Overriden in Text/Voice //TODO: Does this actually override?
} }

View File

@@ -80,7 +80,7 @@ namespace Discord.Rest
if (mode == CacheMode.AllowDownload) if (mode == CacheMode.AllowDownload)
return GetUsersAsync(); return GetUsersAsync();
else else
return ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>().ToAsyncEnumerable(); return AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>(); //Overriden
} }
//IMessageChannel //IMessageChannel
@@ -96,14 +96,14 @@ namespace Discord.Rest
if (mode == CacheMode.AllowDownload) if (mode == CacheMode.AllowDownload)
return GetMessagesAsync(limit); return GetMessagesAsync(limit);
else else
return ImmutableArray.Create<IReadOnlyCollection<IMessage>>().ToAsyncEnumerable(); return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
} }
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode)
{ {
if (mode == CacheMode.AllowDownload) if (mode == CacheMode.AllowDownload)
return GetMessagesAsync(fromMessageId, dir, limit); return GetMessagesAsync(fromMessageId, dir, limit);
else else
return ImmutableArray.Create<IReadOnlyCollection<IMessage>>().ToAsyncEnumerable(); return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
} }
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync() async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync()
=> await GetPinnedMessagesAsync(); => await GetPinnedMessagesAsync();

View File

@@ -44,6 +44,6 @@ namespace Discord.Rest
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode) Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode)
=> Task.FromResult<IGuildUser>(null); => Task.FromResult<IGuildUser>(null);
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode)
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>().ToAsyncEnumerable(); => AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>();
} }
} }

View File

@@ -162,11 +162,31 @@ namespace Discord.Rest
{ {
return await GetUserAsync(guild, client, client.CurrentUser.Id).ConfigureAwait(false); return await GetUserAsync(guild, client, client.CurrentUser.Id).ConfigureAwait(false);
} }
public static async Task<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuild guild, BaseDiscordClient client) public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuild guild, BaseDiscordClient client,
ulong? fromUserId = null, int limit = DiscordConfig.MaxMessagesPerBatch)
{ {
var args = new GetGuildMembersParams(); return new PagedAsyncEnumerable<RestGuildUser>(
var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args).ConfigureAwait(false); DiscordConfig.MaxMessagesPerBatch,
async (info, ct) =>
{
var args = new GetGuildMembersParams
{
Limit = info.PageSize
};
if (info.Position != null)
args.AfterUserId = info.Position.Value;
var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args);
return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray(); return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray();
},
nextPage: (info, lastPage) =>
{
info.Position = lastPage.Max(x => x.Id);
if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
info.Remaining = 0;
},
start: fromUserId,
count: (uint)limit
);
} }
public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client, public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client,
int days = 30, bool simulate = false) int days = 30, bool simulate = false)

View File

@@ -162,7 +162,7 @@ namespace Discord.Rest
//Users //Users
public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync() public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync()
=> GuildHelper.GetUsersAsync(this, Discord).ToAsyncEnumerable(); => GuildHelper.GetUsersAsync(this, Discord);
public Task<RestGuildUser> GetUserAsync(ulong id) public Task<RestGuildUser> GetUserAsync(ulong id)
=> GuildHelper.GetUserAsync(this, Discord, id); => GuildHelper.GetUserAsync(this, Discord, id);
public Task<RestGuildUser> GetCurrentUserAsync() public Task<RestGuildUser> GetCurrentUserAsync()

View File

@@ -21,11 +21,11 @@ namespace Discord.WebSocket
/// <summary> Gets a message from this message channel with the given id, or null if not found. </summary> /// <summary> Gets a message from this message channel with the given id, or null if not found. </summary>
Task<IMessage> GetMessageAsync(ulong id); Task<IMessage> GetMessageAsync(ulong id);
/// <summary> Gets the last N messages from this message channel. </summary> /// <summary> Gets the last N messages from this message channel. </summary>
Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch); IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch);
/// <summary> Gets a collection of messages in this channel. </summary> /// <summary> Gets a collection of messages in this channel. </summary>
Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch); IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch);
/// <summary> Gets a collection of messages in this channel. </summary> /// <summary> Gets a collection of messages in this channel. </summary>
Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch); IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch);
/// <summary> Gets a collection of pinned messages in this channel. </summary> /// <summary> Gets a collection of pinned messages in this channel. </summary>
new Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(); new Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync();
} }

View File

@@ -42,6 +42,6 @@ namespace Discord.WebSocket
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode) Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode)
=> Task.FromResult<IUser>(null); //Overridden => Task.FromResult<IUser>(null); //Overridden
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode)
=> ImmutableArray.Create<IReadOnlyCollection<IUser>>().ToAsyncEnumerable(); //Overridden => AsyncEnumerable.Empty<IReadOnlyCollection<IUser>>(); //Overridden
} }
} }

View File

@@ -3,44 +3,25 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace Discord.WebSocket namespace Discord.WebSocket
{ {
internal static class SocketChannelHelper internal static class SocketChannelHelper
{ {
public static async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(SocketChannel channel, DiscordSocketClient discord, MessageCache messages,
ulong? fromMessageId, Direction dir, int limit)
{
if (messages == null) //Cache disabled
{
var msgs = await ChannelHelper.GetMessagesAsync(channel, discord, fromMessageId, dir, limit).Flatten();
return msgs.ToImmutableArray();
}
var cachedMessages = messages.GetMany(fromMessageId, dir, limit);
limit -= cachedMessages.Count;
if (limit == 0)
return cachedMessages;
if (dir == Direction.Before)
fromMessageId = cachedMessages.Min(x => x.Id);
else
fromMessageId = cachedMessages.Max(x => x.Id);
var downloadedMessages = await ChannelHelper.GetMessagesAsync(channel, discord, fromMessageId, dir, limit).Flatten();
return cachedMessages.Concat<IMessage>(downloadedMessages).ToImmutableArray();
}
public static IAsyncEnumerable<IReadOnlyCollection<IMessage>> PagedGetMessagesAsync(SocketChannel channel, DiscordSocketClient discord, MessageCache messages, public static IAsyncEnumerable<IReadOnlyCollection<IMessage>> PagedGetMessagesAsync(SocketChannel channel, DiscordSocketClient discord, MessageCache messages,
ulong? fromMessageId, Direction dir, int limit) ulong? fromMessageId, Direction dir, int limit, CacheMode mode)
{ {
if (messages == null) //Cache disabled IReadOnlyCollection<SocketMessage> cachedMessages;
return ChannelHelper.GetMessagesAsync(channel, discord, fromMessageId, dir, limit); IAsyncEnumerable<IReadOnlyCollection<IMessage>> result;
var cachedMessages = messages.GetMany(fromMessageId, dir, limit); if (messages != null) //Cache enabled
var result = ImmutableArray.Create(cachedMessages).ToAsyncEnumerable<IReadOnlyCollection<IMessage>>(); cachedMessages = messages.GetMany(fromMessageId, dir, limit);
else
cachedMessages = ImmutableArray.Create<SocketMessage>();
result = ImmutableArray.Create(cachedMessages).ToAsyncEnumerable<IReadOnlyCollection<IMessage>>();
limit -= cachedMessages.Count; limit -= cachedMessages.Count;
if (limit == 0) if (limit == 0 || mode == CacheMode.CacheOnly)
return result; return result;
if (dir == Direction.Before) if (dir == Direction.Before)

View File

@@ -51,12 +51,12 @@ namespace Discord.WebSocket
msg = await ChannelHelper.GetMessageAsync(this, Discord, id); msg = await ChannelHelper.GetMessageAsync(this, Discord, id);
return msg; return msg;
} }
public Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch) public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch)
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit); => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, CacheMode.AllowDownload);
public Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch) public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit); => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, CacheMode.AllowDownload);
public Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch) public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit); => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, CacheMode.AllowDownload);
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync() public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync()
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord); => ChannelHelper.GetPinnedMessagesAsync(this, Discord);
@@ -115,19 +115,9 @@ namespace Discord.WebSocket
return GetCachedMessage(id); return GetCachedMessage(id);
} }
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode)
{ => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode);
if (mode == CacheMode.AllowDownload)
return SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit);
else
return ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit);
}
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode)
{ => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode);
if (mode == CacheMode.AllowDownload)
return SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit);
else
return ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit);
}
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync() async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync()
=> await GetPinnedMessagesAsync().ConfigureAwait(false); => await GetPinnedMessagesAsync().ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS) async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS)

View File

@@ -74,12 +74,12 @@ namespace Discord.WebSocket
msg = await ChannelHelper.GetMessageAsync(this, Discord, id); msg = await ChannelHelper.GetMessageAsync(this, Discord, id);
return msg; return msg;
} }
public Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch) public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch)
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit); => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, CacheMode.AllowDownload);
public Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch) public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit); => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, CacheMode.AllowDownload);
public Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch) public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit); => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, CacheMode.AllowDownload);
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync() public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync()
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord); => ChannelHelper.GetPinnedMessagesAsync(this, Discord);
@@ -178,19 +178,9 @@ namespace Discord.WebSocket
return GetCachedMessage(id); return GetCachedMessage(id);
} }
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode)
{ => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode);
if (mode == CacheMode.AllowDownload)
return SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit);
else
return ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit);
}
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode)
{ => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode);
if (mode == CacheMode.AllowDownload)
return SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit);
else
return ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit);
}
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync() async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync()
=> await GetPinnedMessagesAsync(); => await GetPinnedMessagesAsync();

View File

@@ -57,12 +57,12 @@ namespace Discord.WebSocket
msg = await ChannelHelper.GetMessageAsync(this, Discord, id); msg = await ChannelHelper.GetMessageAsync(this, Discord, id);
return msg; return msg;
} }
public Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch) public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch)
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit); => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, CacheMode.AllowDownload);
public Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch) public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit); => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, CacheMode.AllowDownload);
public Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch) public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit); => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, CacheMode.AllowDownload);
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync() public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync()
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord); => ChannelHelper.GetPinnedMessagesAsync(this, Discord);
@@ -113,22 +113,12 @@ namespace Discord.WebSocket
if (mode == CacheMode.AllowDownload) if (mode == CacheMode.AllowDownload)
return await GetMessageAsync(id); return await GetMessageAsync(id);
else else
throw new NotImplementedException(); return GetCachedMessage(id);
} }
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode)
{ => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode);
if (mode == CacheMode.AllowDownload)
return SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit);
else
return ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit);
}
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode) IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode)
{ => SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode);
if (mode == CacheMode.AllowDownload)
return SocketChannelHelper.PagedGetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit);
else
return ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit);
}
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync() async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync()
=> await GetPinnedMessagesAsync().ConfigureAwait(false); => await GetPinnedMessagesAsync().ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS) async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS)