/
SocketChannelHelper.cs
117 lines (108 loc) · 5.19 KB
/
SocketChannelHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Discord.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
namespace Discord.WebSocket
{
internal static class SocketChannelHelper
{
public static IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ISocketMessageChannel channel, DiscordSocketClient discord, MessageCache messages,
ulong? fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
{
if (dir == Direction.After && fromMessageId == null)
return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
var cachedMessages = GetCachedMessages(channel, discord, messages, fromMessageId, dir, limit);
var result = ImmutableArray.Create(cachedMessages).ToAsyncEnumerable<IReadOnlyCollection<IMessage>>();
if (dir == Direction.Before)
{
limit -= cachedMessages.Count;
if (mode == CacheMode.CacheOnly || limit <= 0)
return result;
//Download remaining messages
ulong? minId = cachedMessages.Count > 0 ? cachedMessages.Min(x => x.Id) : fromMessageId;
var downloadedMessages = ChannelHelper.GetMessagesAsync(channel, discord, minId, dir, limit, options);
if (cachedMessages.Count != 0)
return result.Concat(downloadedMessages);
else
return downloadedMessages;
}
else if (dir == Direction.After)
{
if (mode == CacheMode.CacheOnly)
return result;
bool ignoreCache = false;
// We can find two cases:
// 1. fromMessageId is not null and corresponds to a message that is not in the cache,
// so we have to make a request and ignore the cache
if (fromMessageId.HasValue && messages?.Get(fromMessageId.Value) == null)
{
ignoreCache = true;
}
// 2. fromMessageId is null or already in the cache, so we start from the cache
else if (cachedMessages.Count > 0)
{
fromMessageId = cachedMessages.Max(x => x.Id);
limit -= cachedMessages.Count;
if (limit <= 0)
return result;
}
//Download remaining messages
var downloadedMessages = ChannelHelper.GetMessagesAsync(channel, discord, fromMessageId, dir, limit, options);
if (!ignoreCache && cachedMessages.Count != 0)
return result.Concat(downloadedMessages);
else
return downloadedMessages;
}
else //Direction.Around
{
if (mode == CacheMode.CacheOnly || limit <= cachedMessages.Count)
return result;
//Cache isn't useful here since Discord will send them anyways
return ChannelHelper.GetMessagesAsync(channel, discord, fromMessageId, dir, limit, options);
}
}
public static IReadOnlyCollection<SocketMessage> GetCachedMessages(ISocketMessageChannel channel, DiscordSocketClient discord, MessageCache messages,
ulong? fromMessageId, Direction dir, int limit)
{
if (messages != null) //Cache enabled
return messages.GetMany(fromMessageId, dir, limit);
else
return ImmutableArray.Create<SocketMessage>();
}
/// <exception cref="NotSupportedException">Unexpected <see cref="ISocketMessageChannel"/> type.</exception>
public static void AddMessage(ISocketMessageChannel channel, DiscordSocketClient discord,
SocketMessage msg)
{
switch (channel)
{
case SocketDMChannel dmChannel:
dmChannel.AddMessage(msg);
break;
case SocketGroupChannel groupChannel:
groupChannel.AddMessage(msg);
break;
case SocketThreadChannel threadChannel:
threadChannel.AddMessage(msg);
break;
case SocketTextChannel textChannel:
textChannel.AddMessage(msg);
break;
default:
throw new NotSupportedException($"Unexpected {nameof(ISocketMessageChannel)} type.");
}
}
/// <exception cref="NotSupportedException">Unexpected <see cref="ISocketMessageChannel"/> type.</exception>
public static SocketMessage RemoveMessage(ISocketMessageChannel channel, DiscordSocketClient discord,
ulong id)
{
return channel switch
{
SocketDMChannel dmChannel => dmChannel.RemoveMessage(id),
SocketGroupChannel groupChannel => groupChannel.RemoveMessage(id),
SocketTextChannel textChannel => textChannel.RemoveMessage(id),
_ => throw new NotSupportedException($"Unexpected {nameof(ISocketMessageChannel)} type."),
};
}
}
}