-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeamChatRelay.cs
More file actions
36 lines (32 loc) · 1.58 KB
/
Copy pathTeamChatRelay.cs
File metadata and controls
36 lines (32 loc) · 1.58 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
using RustPlusBot.Abstractions.Events;
using RustPlusBot.Features.Chat.Webhooks;
using RustPlusBot.Features.Workspace.Locating;
namespace RustPlusBot.Features.Chat.Relaying;
/// <summary>Relays one received in-game team message into its Discord #teamchat channel, dropping our own echoes.</summary>
/// <param name="locator">Resolves the target #teamchat channel.</param>
/// <param name="poster">Posts the line via webhook.</param>
/// <param name="dedup">Tracks lines the bridge relayed into the game so their echoes can be dropped.</param>
internal sealed class TeamChatRelay(
ITeamChatChannelLocator locator,
ITeamChatWebhookPoster poster,
RelayDedupBuffer dedup)
{
/// <summary>Handles one <see cref="TeamMessageReceivedEvent"/>.</summary>
/// <param name="evt">The received team message.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that completes when the line has been relayed or dropped.</returns>
public async Task RelayAsync(TeamMessageReceivedEvent evt, CancellationToken cancellationToken)
{
if (evt.FromActivePlayer && dedup.TryConsume((evt.GuildId, evt.ServerId), evt.Message))
{
return; // Our own relayed line echoing back; do not re-post.
}
var channelId = await locator.GetChannelIdAsync(evt.GuildId, evt.ServerId, cancellationToken)
.ConfigureAwait(false);
if (channelId is null)
{
return;
}
await poster.PostAsync(channelId.Value, evt.SenderName, evt.Message, cancellationToken).ConfigureAwait(false);
}
}