From 176267ef7bf36b662b58ad0fc163aced4e7a884a Mon Sep 17 00:00:00 2001 From: Ed <96445749+TheShuEd@users.noreply.github.com> Date: Thu, 30 Jan 2025 15:42:03 +0300 Subject: [PATCH] OOC Relay experiment (#821) --- Content.Server/Chat/Managers/ChatManager.cs | 2 + .../_CP14/Discord/ChatManager.OOCRelay.cs | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 Content.Server/_CP14/Discord/ChatManager.OOCRelay.cs diff --git a/Content.Server/Chat/Managers/ChatManager.cs b/Content.Server/Chat/Managers/ChatManager.cs index 75c46abe37..f5ad23db65 100644 --- a/Content.Server/Chat/Managers/ChatManager.cs +++ b/Content.Server/Chat/Managers/ChatManager.cs @@ -64,6 +64,7 @@ internal sealed partial class ChatManager : IChatManager _configurationManager.OnValueChanged(CCVars.AdminOocEnabled, OnAdminOocEnabledChanged, true); RegisterRateLimits(); + CP14InitializeOOCRelay(); //CP14 OOC Relay } private void OnOocEnabledChanged(bool val) @@ -259,6 +260,7 @@ internal sealed partial class ChatManager : IChatManager //TODO: player.Name color, this will need to change the structure of the MsgChatMessage ChatMessageToAll(ChatChannel.OOC, message, wrappedMessage, EntityUid.Invalid, hideChat: false, recordReplay: true, colorOverride: colorOverride, author: player.UserId); _mommiLink.SendOOCMessage(player.Name, message.Replace("@", "\\@").Replace("<", "\\<").Replace("/", "\\/")); // @ and < are both problematic for discord due to pinging. / is sanitized solely to kneecap links to murder embeds via blunt force + CP14SendOOCInDiscord(player, message); //CP14 OOC Relay _adminLogger.Add(LogType.Chat, LogImpact.Low, $"OOC from {player:Player}: {message}"); } diff --git a/Content.Server/_CP14/Discord/ChatManager.OOCRelay.cs b/Content.Server/_CP14/Discord/ChatManager.OOCRelay.cs new file mode 100644 index 0000000000..325961e5a1 --- /dev/null +++ b/Content.Server/_CP14/Discord/ChatManager.OOCRelay.cs @@ -0,0 +1,55 @@ +using Content.Server.Discord; +using Content.Shared.CCVar; +using Robust.Shared.Configuration; +using Robust.Shared.Player; +using Serilog; + +namespace Content.Server.Chat.Managers; + +/// +/// OOC Relay module +/// +internal sealed partial class ChatManager +{ + [Dependency] private readonly DiscordWebhook _discord = default!; + [Dependency] private readonly IConfigurationManager _cfgManager = default!; + + private WebhookIdentifier? _OOCwebhook; + + private void CP14InitializeOOCRelay() + { + var webhookUrl = _cfgManager.GetCVar(CCVars.DiscordRoundUpdateWebhook); + + if (webhookUrl == string.Empty) + return; + + _discord.GetWebhook(webhookUrl, + data => + { + _OOCwebhook = data.ToIdentifier(); + }); + } + + private async void CP14SendOOCInDiscord(ICommonSession player, string message) + { + try + { + if (_OOCwebhook == null) + return; + + var name = player.Name; + var content = + message.Replace("@", "\\@") + .Replace("<", "\\<") + .Replace("/", + "\\/"); // @ and < are both problematic for discord due to pinging. / is sanitized solely to kneecap links to murder embeds via blunt force + + var payload = new WebhookPayload {Content = $"**`{name}`**: {content}"}; + await _discord.CreateMessage(_OOCwebhook.Value, payload); + } + catch (Exception e) + { + Log.Error($"Error while sending discord OOC message:\n{e}"); + } + } +}