-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeamChatInboundProcessor.cs
More file actions
59 lines (52 loc) · 2.76 KB
/
Copy pathTeamChatInboundProcessor.cs
File metadata and controls
59 lines (52 loc) · 2.76 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
using System.Globalization;
using Microsoft.Extensions.DependencyInjection;
using RustPlusBot.Features.Chat.Relaying;
using RustPlusBot.Features.Connections.Listening;
using RustPlusBot.Features.Workspace.Locating;
using RustPlusBot.Persistence.Commands;
namespace RustPlusBot.Features.Chat.Inbound;
/// <summary>Turns a Discord #teamchat message into an in-game relay (record-then-send), reporting the outcome.</summary>
/// <param name="locator">Resolves whether/which server a channel maps to.</param>
/// <param name="sender">Relays the formatted line into the game.</param>
/// <param name="dedup">Records the relayed line so its in-game echo can be dropped.</param>
/// <param name="scopeFactory">Opens a scope to read the scoped <see cref="IMuteStore"/> mute gate.</param>
internal sealed class TeamChatInboundProcessor(
ITeamChatChannelLocator locator,
ITeamChatSender sender,
RelayDedupBuffer dedup,
IServiceScopeFactory scopeFactory)
{
/// <summary>Processes one observed Discord message.</summary>
/// <param name="message">The reduced message.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>What was done with the message.</returns>
public async Task<InboundOutcome> ProcessAsync(InboundMessage message, CancellationToken cancellationToken)
{
if (message.AuthorIsBotOrWebhook || string.IsNullOrWhiteSpace(message.Content))
{
return InboundOutcome.Ignored;
}
var target = await locator.ResolveAsync(message.ChannelId, cancellationToken).ConfigureAwait(false);
if (target is not { } t)
{
return InboundOutcome.Ignored;
}
// A muted server silences ALL bot->game output: ignore fully (neither record a dedup entry nor send).
// IMuteStore is scoped, so resolve it from a fresh scope rather than capturing it on this singleton.
var scope = scopeFactory.CreateAsyncScope();
await using (scope.ConfigureAwait(false))
{
var muteStore = scope.ServiceProvider.GetRequiredService<IMuteStore>();
if (await muteStore.GetMutedAsync(t.GuildId, t.ServerId, cancellationToken).ConfigureAwait(false))
{
return InboundOutcome.Ignored;
}
}
var key = (t.GuildId, t.ServerId);
var text = string.Create(CultureInfo.InvariantCulture, $"[{message.DisplayName}] {message.Content}");
// Record BEFORE sending: the in-game echo can arrive before SendAsync returns. Unused entries expire.
dedup.Record(key, text);
var result = await sender.SendAsync(t.GuildId, t.ServerId, text, cancellationToken).ConfigureAwait(false);
return result == TeamChatSendResult.Sent ? InboundOutcome.Sent : InboundOutcome.Failed;
}
}