-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscordAlarmChannelPoster.cs
More file actions
98 lines (90 loc) · 3.45 KB
/
Copy pathDiscordAlarmChannelPoster.cs
File metadata and controls
98 lines (90 loc) · 3.45 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
using Discord;
using Discord.WebSocket;
using Microsoft.Extensions.Logging;
using RustPlusBot.Discord.Posting;
namespace RustPlusBot.Features.Alarms.Posting;
/// <summary>Posts/edits alarm embeds in #alarms by message id. Untested integration shim.</summary>
/// <param name="client">The Discord socket client (used directly for the raw @everyone ping).</param>
/// <param name="messenger">The shared gated channel messenger.</param>
/// <param name="logger">The logger.</param>
internal sealed partial class DiscordAlarmChannelPoster(
DiscordSocketClient client,
DiscordChannelMessenger messenger,
ILogger<DiscordAlarmChannelPoster> logger) : IAlarmChannelPoster
{
/// <inheritdoc />
public Task<ulong?> EnsureAsync(
ulong channelId,
ulong? messageId,
Embed embed,
MessageComponent components,
CancellationToken cancellationToken)
=> messenger.EnsureAsync(channelId, messageId, embed, components, logger, cancellationToken);
/// <inheritdoc />
public async Task SendEveryonePingAsync(ulong channelId, string content, CancellationToken cancellationToken)
{
try
{
var options = new RequestOptions
{
CancelToken = cancellationToken
};
if (await client.GetChannelAsync(channelId, options).ConfigureAwait(false)
is not ITextChannel channel)
{
return;
}
await channel.SendMessageAsync(
content,
options: options,
allowedMentions: AllowedMentions.All)
.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw; // Shutdown — let the loop unwind.
}
#pragma warning disable CA1031 // Broad catch: a Discord hiccup must not crash the alarm ping; swallow the failure.
catch (Exception ex)
#pragma warning restore CA1031
{
LogPingFailed(logger, ex, channelId);
}
}
/// <inheritdoc />
public async Task DeleteMessageAsync(ulong channelId, ulong messageId, CancellationToken cancellationToken)
{
try
{
var options = new RequestOptions
{
CancelToken = cancellationToken
};
if (await client.GetChannelAsync(channelId, options).ConfigureAwait(false)
is not ITextChannel channel)
{
return;
}
await channel.DeleteMessageAsync(messageId, options).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw; // Shutdown — let the loop unwind.
}
#pragma warning disable CA1031 // Broad catch: a Discord hiccup (or already-deleted message) must not crash the purge.
catch (Exception ex)
#pragma warning restore CA1031
{
LogDeleteFailed(logger, ex, messageId, channelId);
}
}
[LoggerMessage(Level = LogLevel.Warning,
Message = "Sending @everyone ping in channel {ChannelId} failed.")]
private static partial void LogPingFailed(ILogger logger, Exception exception, ulong channelId);
[LoggerMessage(Level = LogLevel.Debug,
Message = "Deleting message {MessageId} in channel {ChannelId} failed (may already be gone).")]
private static partial void LogDeleteFailed(ILogger logger,
Exception exception,
ulong messageId,
ulong channelId);
}