-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscordChannelMessenger.cs
More file actions
155 lines (143 loc) · 7.08 KB
/
Copy pathDiscordChannelMessenger.cs
File metadata and controls
155 lines (143 loc) · 7.08 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using Discord;
using Discord.Net;
using Discord.WebSocket;
using Microsoft.Extensions.Logging;
namespace RustPlusBot.Discord.Posting;
/// <summary>
/// Shared Discord channel post/edit boilerplate: fetch, options, self-heal, broad-catch — plus a
/// render gate that skips edits whose content is identical to the last successful send, keeping
/// boot primes and periodic republishes out of Discord's per-channel PATCH rate-limit bucket.
/// </summary>
/// <param name="client">The Discord socket client.</param>
/// <param name="gate">The per-message no-op edit detector.</param>
public sealed class DiscordChannelMessenger(DiscordSocketClient client, RenderGate gate)
{
/// <summary>Request timeout generous enough to ride out a queued rate-limit burst (default is 15 s).</summary>
private const int RequestTimeoutMs = 30_000;
/// <summary>
/// Edits the message by id (self-healing on 404 by reposting) or posts a new one.
/// Identical re-renders are skipped without calling Discord's edit endpoint.
/// Returns the message id, or null on failure.
/// </summary>
/// <param name="channelId">The target channel id.</param>
/// <param name="messageId">The existing message id to edit, or null to post a new one.</param>
/// <param name="embed">The embed to post or update.</param>
/// <param name="components">The message components to post or update.</param>
/// <param name="logger">The caller's logger.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public async Task<ulong?> EnsureAsync(
ulong channelId,
ulong? messageId,
Embed embed,
MessageComponent components,
ILogger logger,
CancellationToken cancellationToken)
{
try
{
var options = CreateOptions(cancellationToken);
if (await client.GetChannelAsync(channelId, options).ConfigureAwait(false)
is not ITextChannel channel)
{
return null;
}
var canonical = RenderCanonicalizer.Canonicalize(embed, components);
if (messageId is { } id)
{
// Inner try: some Discord.Net versions THROW (HttpException 404/Unknown Message)
// rather than return null for a deleted message. Catch it and fall through to repost
// so the self-heal path always runs.
try
{
var existing = await channel.GetMessageAsync(id, options: options).ConfigureAwait(false);
if (existing is IUserMessage userMessage)
{
if (!gate.ShouldSend(id, canonical))
{
// Same content as the last successful send: don't spend the PATCH bucket.
return userMessage.Id;
}
await userMessage.ModifyAsync(m =>
{
m.Embed = embed;
m.Components = components;
}, options).ConfigureAwait(false);
gate.Commit(id, canonical);
return userMessage.Id;
}
// Message was deleted (returned null / not a user message); fall through to repost.
}
catch (HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.NotFound)
{
// Deleted/unknown message; fall through to repost and return the new id.
#pragma warning disable CA1848, CA1873 // Use LoggerMessage delegates / avoid expensive log-arg evaluation — plain logger.Log is fine for a shared helper (no source-gen partial context); ulong boxing is negligible vs. the caught exception.
logger.LogDebug(ex, "Embed {MessageId} in channel {ChannelId} was deleted; reposting.", id,
channelId);
#pragma warning restore CA1848, CA1873
}
// The tracked message is gone; the repost below re-keys the gate under the new id.
gate.Invalidate(id);
}
var posted = await channel
.SendMessageAsync(embed: embed, options: options, components: components)
.ConfigureAwait(false);
gate.Commit(posted.Id, canonical);
return posted.Id;
}
catch (OperationCanceledException)
{
throw; // Shutdown — let the loop unwind.
}
#pragma warning disable CA1031 // Broad catch: a Discord hiccup must not crash the relay; report failure as null.
catch (Exception ex)
#pragma warning restore CA1031
{
if (messageId is { } failedId)
{
// Outcome unknown (e.g. timeout mid-flight): forget the entry so the next render retries.
gate.Invalidate(failedId);
}
#pragma warning disable CA1848, CA1873 // Use LoggerMessage delegates / avoid expensive log-arg evaluation — plain logger.Log is fine for a shared helper (no source-gen partial context); ulong boxing is negligible vs. the caught exception.
logger.LogWarning(ex, "Posting/editing an embed in channel {ChannelId} failed.", channelId);
#pragma warning restore CA1848, CA1873
return null;
}
}
/// <summary>Posts an embed fire-and-forget; Discord hiccups are logged and swallowed.</summary>
/// <param name="channelId">The target channel id.</param>
/// <param name="embed">The embed to post.</param>
/// <param name="logger">The caller's logger.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public async Task PostAsync(
ulong channelId,
Embed embed,
ILogger logger,
CancellationToken cancellationToken)
{
try
{
var options = CreateOptions(cancellationToken);
if (await client.GetChannelAsync(channelId, options).ConfigureAwait(false) is not ITextChannel channel)
{
return;
}
await channel.SendMessageAsync(embed: embed, options: options).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw; // Cancellation (shutdown) is not a post failure; let the relay loop unwind cleanly.
}
#pragma warning disable CA1031 // Broad catch: a Discord hiccup must not crash the relay.
catch (Exception ex)
#pragma warning restore CA1031
{
#pragma warning disable CA1848, CA1873 // Use LoggerMessage delegates / avoid expensive log-arg evaluation — plain logger.Log is fine for a shared helper (no source-gen partial context); ulong boxing is negligible vs. the caught exception.
logger.LogWarning(ex, "Posting an embed to channel {ChannelId} failed.", channelId);
#pragma warning restore CA1848, CA1873
}
}
private static RequestOptions CreateOptions(CancellationToken cancellationToken) => new()
{
CancelToken = cancellationToken, RetryMode = RetryMode.AlwaysRetry, Timeout = RequestTimeoutMs,
};
}