1
0

BwoinkSystem.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. using System.Linq;
  2. using System.Net.Http;
  3. using System.Text;
  4. using System.Text.Json;
  5. using System.Text.Json.Nodes;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using Content.Server.Administration.Managers;
  9. using Content.Server.Afk;
  10. using Content.Server.Database;
  11. using Content.Server.Discord;
  12. using Content.Server.GameTicking;
  13. using Content.Server.Players.RateLimiting;
  14. using Content.Shared.Administration;
  15. using Content.Shared.CCVar;
  16. using Content.Shared.GameTicking;
  17. using Content.Shared.Mind;
  18. using Content.Shared.Players.RateLimiting;
  19. using JetBrains.Annotations;
  20. using Robust.Server.Player;
  21. using Robust.Shared;
  22. using Robust.Shared.Configuration;
  23. using Robust.Shared.Enums;
  24. using Robust.Shared.Network;
  25. using Robust.Shared.Player;
  26. using Robust.Shared.Timing;
  27. using Robust.Shared.Utility;
  28. namespace Content.Server.Administration.Systems
  29. {
  30. [UsedImplicitly]
  31. public sealed partial class BwoinkSystem : SharedBwoinkSystem
  32. {
  33. private const string RateLimitKey = "AdminHelp";
  34. [Dependency] private readonly IPlayerManager _playerManager = default!;
  35. [Dependency] private readonly IAdminManager _adminManager = default!;
  36. [Dependency] private readonly IConfigurationManager _config = default!;
  37. [Dependency] private readonly IGameTiming _timing = default!;
  38. [Dependency] private readonly IPlayerLocator _playerLocator = default!;
  39. [Dependency] private readonly GameTicker _gameTicker = default!;
  40. [Dependency] private readonly SharedMindSystem _minds = default!;
  41. [Dependency] private readonly IAfkManager _afkManager = default!;
  42. [Dependency] private readonly IServerDbManager _dbManager = default!;
  43. [Dependency] private readonly PlayerRateLimitManager _rateLimit = default!;
  44. [GeneratedRegex(@"^https://discord\.com/api/webhooks/(\d+)/((?!.*/).*)$")]
  45. private static partial Regex DiscordRegex();
  46. private string _webhookUrl = string.Empty;
  47. private WebhookData? _webhookData;
  48. private string _onCallUrl = string.Empty;
  49. private WebhookData? _onCallData;
  50. private ISawmill _sawmill = default!;
  51. private readonly HttpClient _httpClient = new();
  52. private string _footerIconUrl = string.Empty;
  53. private string _avatarUrl = string.Empty;
  54. private string _serverName = string.Empty;
  55. private readonly Dictionary<NetUserId, DiscordRelayInteraction> _relayMessages = new();
  56. private Dictionary<NetUserId, string> _oldMessageIds = new();
  57. private readonly Dictionary<NetUserId, Queue<DiscordRelayedData>> _messageQueues = new();
  58. private readonly HashSet<NetUserId> _processingChannels = new();
  59. private readonly Dictionary<NetUserId, (TimeSpan Timestamp, bool Typing)> _typingUpdateTimestamps = new();
  60. private string _overrideClientName = string.Empty;
  61. // Max embed description length is 4096, according to https://discord.com/developers/docs/resources/channel#embed-object-embed-limits
  62. // Keep small margin, just to be safe
  63. private const ushort DescriptionMax = 4000;
  64. // Maximum length a message can be before it is cut off
  65. // Should be shorter than DescriptionMax
  66. private const ushort MessageLengthCap = 3000;
  67. // Text to be used to cut off messages that are too long. Should be shorter than MessageLengthCap
  68. private const string TooLongText = "... **(too long)**";
  69. private int _maxAdditionalChars;
  70. private readonly Dictionary<NetUserId, DateTime> _activeConversations = new();
  71. public override void Initialize()
  72. {
  73. base.Initialize();
  74. Subs.CVar(_config, CCVars.DiscordOnCallWebhook, OnCallChanged, true);
  75. Subs.CVar(_config, CCVars.DiscordAHelpWebhook, OnWebhookChanged, true);
  76. Subs.CVar(_config, CCVars.DiscordAHelpFooterIcon, OnFooterIconChanged, true);
  77. Subs.CVar(_config, CCVars.DiscordAHelpAvatar, OnAvatarChanged, true);
  78. Subs.CVar(_config, CVars.GameHostName, OnServerNameChanged, true);
  79. Subs.CVar(_config, CCVars.AdminAhelpOverrideClientName, OnOverrideChanged, true);
  80. _sawmill = IoCManager.Resolve<ILogManager>().GetSawmill("AHELP");
  81. var defaultParams = new AHelpMessageParams(
  82. string.Empty,
  83. string.Empty,
  84. true,
  85. _gameTicker.RoundDuration().ToString("hh\\:mm\\:ss"),
  86. _gameTicker.RunLevel,
  87. playedSound: false
  88. );
  89. _maxAdditionalChars = GenerateAHelpMessage(defaultParams).Message.Length;
  90. _playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
  91. SubscribeLocalEvent<GameRunLevelChangedEvent>(OnGameRunLevelChanged);
  92. SubscribeNetworkEvent<BwoinkClientTypingUpdated>(OnClientTypingUpdated);
  93. SubscribeLocalEvent<RoundRestartCleanupEvent>(_ => _activeConversations.Clear());
  94. _rateLimit.Register(
  95. RateLimitKey,
  96. new RateLimitRegistration(CCVars.AhelpRateLimitPeriod,
  97. CCVars.AhelpRateLimitCount,
  98. PlayerRateLimitedAction)
  99. );
  100. }
  101. private async void OnCallChanged(string url)
  102. {
  103. _onCallUrl = url;
  104. if (url == string.Empty)
  105. return;
  106. var match = DiscordRegex().Match(url);
  107. if (!match.Success)
  108. {
  109. Log.Error("On call URL does not appear to be valid.");
  110. return;
  111. }
  112. if (match.Groups.Count <= 2)
  113. {
  114. Log.Error("Could not get webhook ID or token for on call URL.");
  115. return;
  116. }
  117. var webhookId = match.Groups[1].Value;
  118. var webhookToken = match.Groups[2].Value;
  119. _onCallData = await GetWebhookData(webhookId, webhookToken);
  120. }
  121. private void PlayerRateLimitedAction(ICommonSession obj)
  122. {
  123. RaiseNetworkEvent(
  124. new BwoinkTextMessage(obj.UserId, default, Loc.GetString("bwoink-system-rate-limited"), playSound: false),
  125. obj.Channel);
  126. }
  127. private void OnOverrideChanged(string obj)
  128. {
  129. _overrideClientName = obj;
  130. }
  131. private async void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
  132. {
  133. if (e.NewStatus == SessionStatus.Disconnected)
  134. {
  135. if (_activeConversations.TryGetValue(e.Session.UserId, out var lastMessageTime))
  136. {
  137. var timeSinceLastMessage = DateTime.Now - lastMessageTime;
  138. if (timeSinceLastMessage > TimeSpan.FromMinutes(5))
  139. {
  140. _activeConversations.Remove(e.Session.UserId);
  141. return; // Do not send disconnect message if timeout exceeded
  142. }
  143. }
  144. // Check if the user has been banned
  145. var ban = await _dbManager.GetServerBanAsync(null, e.Session.UserId, null, null);
  146. if (ban != null)
  147. {
  148. var banMessage = Loc.GetString("bwoink-system-player-banned", ("banReason", ban.Reason));
  149. NotifyAdmins(e.Session, banMessage, PlayerStatusType.Banned);
  150. _activeConversations.Remove(e.Session.UserId);
  151. return;
  152. }
  153. }
  154. // Notify all admins if a player disconnects or reconnects
  155. var message = e.NewStatus switch
  156. {
  157. SessionStatus.Connected => Loc.GetString("bwoink-system-player-reconnecting"),
  158. SessionStatus.Disconnected => Loc.GetString("bwoink-system-player-disconnecting"),
  159. _ => null
  160. };
  161. if (message != null)
  162. {
  163. var statusType = e.NewStatus == SessionStatus.Connected
  164. ? PlayerStatusType.Connected
  165. : PlayerStatusType.Disconnected;
  166. NotifyAdmins(e.Session, message, statusType);
  167. }
  168. if (e.NewStatus != SessionStatus.InGame)
  169. return;
  170. RaiseNetworkEvent(new BwoinkDiscordRelayUpdated(!string.IsNullOrWhiteSpace(_webhookUrl)), e.Session);
  171. }
  172. private void NotifyAdmins(ICommonSession session, string message, PlayerStatusType statusType)
  173. {
  174. if (!_activeConversations.ContainsKey(session.UserId))
  175. {
  176. // If the user is not part of an active conversation, do not notify admins.
  177. return;
  178. }
  179. // Get the current timestamp
  180. var timestamp = DateTime.Now.ToString("HH:mm:ss");
  181. var roundTime = _gameTicker.RoundDuration().ToString("hh\\:mm\\:ss");
  182. // Determine the icon based on the status type
  183. string icon = statusType switch
  184. {
  185. PlayerStatusType.Connected => ":green_circle:",
  186. PlayerStatusType.Disconnected => ":red_circle:",
  187. PlayerStatusType.Banned => ":no_entry:",
  188. _ => ":question:"
  189. };
  190. // Create the message parameters for Discord
  191. var messageParams = new AHelpMessageParams(
  192. session.Name,
  193. message,
  194. true,
  195. roundTime,
  196. _gameTicker.RunLevel,
  197. playedSound: true,
  198. icon: icon
  199. );
  200. // Create the message for in-game with username
  201. var color = statusType switch
  202. {
  203. PlayerStatusType.Connected => Color.Green.ToHex(),
  204. PlayerStatusType.Disconnected => Color.Yellow.ToHex(),
  205. PlayerStatusType.Banned => Color.Orange.ToHex(),
  206. _ => Color.Gray.ToHex(),
  207. };
  208. var inGameMessage = $"[color={color}]{session.Name} {message}[/color]";
  209. var bwoinkMessage = new BwoinkTextMessage(
  210. userId: session.UserId,
  211. trueSender: SystemUserId,
  212. text: inGameMessage,
  213. sentAt: DateTime.Now,
  214. playSound: false
  215. );
  216. var admins = GetTargetAdmins();
  217. foreach (var admin in admins)
  218. {
  219. RaiseNetworkEvent(bwoinkMessage, admin);
  220. }
  221. // Enqueue the message for Discord relay
  222. if (_webhookUrl != string.Empty)
  223. {
  224. // if (!_messageQueues.ContainsKey(session.UserId))
  225. // _messageQueues[session.UserId] = new Queue<string>();
  226. //
  227. // var escapedText = FormattedMessage.EscapeText(message);
  228. // messageParams.Message = escapedText;
  229. //
  230. // var discordMessage = GenerateAHelpMessage(messageParams);
  231. // _messageQueues[session.UserId].Enqueue(discordMessage);
  232. var queue = _messageQueues.GetOrNew(session.UserId);
  233. var escapedText = FormattedMessage.EscapeText(message);
  234. messageParams.Message = escapedText;
  235. var discordMessage = GenerateAHelpMessage(messageParams);
  236. queue.Enqueue(discordMessage);
  237. }
  238. }
  239. private void OnGameRunLevelChanged(GameRunLevelChangedEvent args)
  240. {
  241. // Don't make a new embed if we
  242. // 1. were in the lobby just now, and
  243. // 2. are not entering the lobby or directly into a new round.
  244. if (args.Old is GameRunLevel.PreRoundLobby ||
  245. args.New is not (GameRunLevel.PreRoundLobby or GameRunLevel.InRound))
  246. {
  247. return;
  248. }
  249. // Store the Discord message IDs of the previous round
  250. _oldMessageIds = new Dictionary<NetUserId, string>();
  251. foreach (var (user, interaction) in _relayMessages)
  252. {
  253. var id = interaction.Id;
  254. if (id == null)
  255. return;
  256. _oldMessageIds[user] = id;
  257. }
  258. _relayMessages.Clear();
  259. }
  260. private void OnClientTypingUpdated(BwoinkClientTypingUpdated msg, EntitySessionEventArgs args)
  261. {
  262. if (_typingUpdateTimestamps.TryGetValue(args.SenderSession.UserId, out var tuple) &&
  263. tuple.Typing == msg.Typing &&
  264. tuple.Timestamp + TimeSpan.FromSeconds(1) > _timing.RealTime)
  265. {
  266. return;
  267. }
  268. _typingUpdateTimestamps[args.SenderSession.UserId] = (_timing.RealTime, msg.Typing);
  269. // Non-admins can only ever type on their own ahelp, guard against fake messages
  270. var isAdmin = _adminManager.GetAdminData(args.SenderSession)?.HasFlag(AdminFlags.Adminhelp) ?? false;
  271. var channel = isAdmin ? msg.Channel : args.SenderSession.UserId;
  272. var update = new BwoinkPlayerTypingUpdated(channel, args.SenderSession.Name, msg.Typing);
  273. foreach (var admin in GetTargetAdmins())
  274. {
  275. if (admin.UserId == args.SenderSession.UserId)
  276. continue;
  277. RaiseNetworkEvent(update, admin);
  278. }
  279. }
  280. private void OnServerNameChanged(string obj)
  281. {
  282. _serverName = obj;
  283. }
  284. private async void OnWebhookChanged(string url)
  285. {
  286. _webhookUrl = url;
  287. RaiseNetworkEvent(new BwoinkDiscordRelayUpdated(!string.IsNullOrWhiteSpace(url)));
  288. if (url == string.Empty)
  289. return;
  290. // Basic sanity check and capturing webhook ID and token
  291. var match = DiscordRegex().Match(url);
  292. if (!match.Success)
  293. {
  294. // TODO: Ideally, CVar validation during setting should be better integrated
  295. Log.Warning("Webhook URL does not appear to be valid. Using anyways...");
  296. return;
  297. }
  298. if (match.Groups.Count <= 2)
  299. {
  300. Log.Error("Could not get webhook ID or token.");
  301. return;
  302. }
  303. var webhookId = match.Groups[1].Value;
  304. var webhookToken = match.Groups[2].Value;
  305. // Fire and forget
  306. _webhookData = await GetWebhookData(webhookId, webhookToken);
  307. }
  308. private async Task<WebhookData?> GetWebhookData(string id, string token)
  309. {
  310. var response = await _httpClient.GetAsync($"https://discord.com/api/v10/webhooks/{id}/{token}");
  311. var content = await response.Content.ReadAsStringAsync();
  312. if (!response.IsSuccessStatusCode)
  313. {
  314. _sawmill.Log(LogLevel.Error,
  315. $"Discord returned bad status code when trying to get webhook data (perhaps the webhook URL is invalid?): {response.StatusCode}\nResponse: {content}");
  316. return null;
  317. }
  318. return JsonSerializer.Deserialize<WebhookData>(content);
  319. }
  320. private void OnFooterIconChanged(string url)
  321. {
  322. _footerIconUrl = url;
  323. }
  324. private void OnAvatarChanged(string url)
  325. {
  326. _avatarUrl = url;
  327. }
  328. private async void ProcessQueue(NetUserId userId, Queue<DiscordRelayedData> messages)
  329. {
  330. // Whether an embed already exists for this player
  331. var exists = _relayMessages.TryGetValue(userId, out var existingEmbed);
  332. // Whether the message will become too long after adding these new messages
  333. var tooLong = exists && messages.Sum(msg => Math.Min(msg.Message.Length, MessageLengthCap) + "\n".Length)
  334. + existingEmbed?.Description.Length > DescriptionMax;
  335. // If there is no existing embed, or it is getting too long, we create a new embed
  336. if (!exists || tooLong)
  337. {
  338. var lookup = await _playerLocator.LookupIdAsync(userId);
  339. if (lookup == null)
  340. {
  341. _sawmill.Log(LogLevel.Error,
  342. $"Unable to find player for NetUserId {userId} when sending discord webhook.");
  343. _relayMessages.Remove(userId);
  344. return;
  345. }
  346. var linkToPrevious = string.Empty;
  347. // If we have all the data required, we can link to the embed of the previous round or embed that was too long
  348. if (_webhookData is { GuildId: { } guildId, ChannelId: { } channelId })
  349. {
  350. if (tooLong && existingEmbed?.Id != null)
  351. {
  352. linkToPrevious =
  353. $"**[Go to previous embed of this round](https://discord.com/channels/{guildId}/{channelId}/{existingEmbed.Id})**\n";
  354. }
  355. else if (_oldMessageIds.TryGetValue(userId, out var id) && !string.IsNullOrEmpty(id))
  356. {
  357. linkToPrevious =
  358. $"**[Go to last round's conversation with this player](https://discord.com/channels/{guildId}/{channelId}/{id})**\n";
  359. }
  360. }
  361. var characterName = _minds.GetCharacterName(userId);
  362. existingEmbed = new DiscordRelayInteraction()
  363. {
  364. Id = null,
  365. CharacterName = characterName,
  366. Description = linkToPrevious,
  367. Username = lookup.Username,
  368. LastRunLevel = _gameTicker.RunLevel,
  369. };
  370. _relayMessages[userId] = existingEmbed;
  371. }
  372. // Previous message was in another RunLevel, so show that in the embed
  373. if (existingEmbed!.LastRunLevel != _gameTicker.RunLevel)
  374. {
  375. existingEmbed.Description += _gameTicker.RunLevel switch
  376. {
  377. GameRunLevel.PreRoundLobby => "\n\n:arrow_forward: _**Pre-round lobby started**_\n",
  378. GameRunLevel.InRound => "\n\n:arrow_forward: _**Round started**_\n",
  379. GameRunLevel.PostRound => "\n\n:stop_button: _**Post-round started**_\n",
  380. _ => throw new ArgumentOutOfRangeException(nameof(_gameTicker.RunLevel),
  381. $"{_gameTicker.RunLevel} was not matched."),
  382. };
  383. existingEmbed.LastRunLevel = _gameTicker.RunLevel;
  384. }
  385. // If last message of the new batch is SOS then relay it to on-call.
  386. // ... as long as it hasn't been relayed already.
  387. var discordMention = messages.Last();
  388. var onCallRelay = !discordMention.Receivers && !existingEmbed.OnCall;
  389. // Add available messages to the embed description
  390. while (messages.TryDequeue(out var message))
  391. {
  392. string text;
  393. // In case someone thinks they're funny
  394. if (message.Message.Length > MessageLengthCap)
  395. text = message.Message[..(MessageLengthCap - TooLongText.Length)] + TooLongText;
  396. else
  397. text = message.Message;
  398. existingEmbed.Description += $"\n{text}";
  399. }
  400. var payload = GeneratePayload(existingEmbed.Description,
  401. existingEmbed.Username,
  402. existingEmbed.CharacterName);
  403. // If there is no existing embed, create a new one
  404. // Otherwise patch (edit) it
  405. if (existingEmbed.Id == null)
  406. {
  407. var request = await _httpClient.PostAsync($"{_webhookUrl}?wait=true",
  408. new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"));
  409. var content = await request.Content.ReadAsStringAsync();
  410. if (!request.IsSuccessStatusCode)
  411. {
  412. _sawmill.Log(LogLevel.Error,
  413. $"Discord returned bad status code when posting message (perhaps the message is too long?): {request.StatusCode}\nResponse: {content}");
  414. _relayMessages.Remove(userId);
  415. return;
  416. }
  417. var id = JsonNode.Parse(content)?["id"];
  418. if (id == null)
  419. {
  420. _sawmill.Log(LogLevel.Error,
  421. $"Could not find id in json-content returned from discord webhook: {content}");
  422. _relayMessages.Remove(userId);
  423. return;
  424. }
  425. existingEmbed.Id = id.ToString();
  426. }
  427. else
  428. {
  429. var request = await _httpClient.PatchAsync($"{_webhookUrl}/messages/{existingEmbed.Id}",
  430. new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"));
  431. if (!request.IsSuccessStatusCode)
  432. {
  433. var content = await request.Content.ReadAsStringAsync();
  434. _sawmill.Log(LogLevel.Error,
  435. $"Discord returned bad status code when patching message (perhaps the message is too long?): {request.StatusCode}\nResponse: {content}");
  436. _relayMessages.Remove(userId);
  437. return;
  438. }
  439. }
  440. _relayMessages[userId] = existingEmbed;
  441. // Actually do the on call relay last, we just need to grab it before we dequeue every message above.
  442. if (onCallRelay &&
  443. _onCallData != null)
  444. {
  445. existingEmbed.OnCall = true;
  446. var roleMention = _config.GetCVar(CCVars.DiscordAhelpMention);
  447. if (!string.IsNullOrEmpty(roleMention))
  448. {
  449. var message = new StringBuilder();
  450. message.AppendLine($"<@&{roleMention}>");
  451. message.AppendLine("Unanswered SOS");
  452. // Need webhook data to get the correct link for that channel rather than on-call data.
  453. if (_webhookData is { GuildId: { } guildId, ChannelId: { } channelId })
  454. {
  455. message.AppendLine(
  456. $"**[Go to ahelp](https://discord.com/channels/{guildId}/{channelId}/{existingEmbed.Id})**");
  457. }
  458. payload = GeneratePayload(message.ToString(), existingEmbed.Username, existingEmbed.CharacterName);
  459. var request = await _httpClient.PostAsync($"{_onCallUrl}?wait=true",
  460. new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"));
  461. var content = await request.Content.ReadAsStringAsync();
  462. if (!request.IsSuccessStatusCode)
  463. {
  464. _sawmill.Log(LogLevel.Error, $"Discord returned bad status code when posting relay message (perhaps the message is too long?): {request.StatusCode}\nResponse: {content}");
  465. }
  466. }
  467. }
  468. else
  469. {
  470. existingEmbed.OnCall = false;
  471. }
  472. _processingChannels.Remove(userId);
  473. }
  474. private WebhookPayload GeneratePayload(string messages, string username, string? characterName = null)
  475. {
  476. // Add character name
  477. if (characterName != null)
  478. username += $" ({characterName})";
  479. // If no admins are online, set embed color to red. Otherwise green
  480. var color = GetNonAfkAdmins().Count > 0 ? 0x41F097 : 0xFF0000;
  481. // Limit server name to 1500 characters, in case someone tries to be a little funny
  482. var serverName = _serverName[..Math.Min(_serverName.Length, 1500)];
  483. var round = _gameTicker.RunLevel switch
  484. {
  485. GameRunLevel.PreRoundLobby => _gameTicker.RoundId == 0
  486. ? "pre-round lobby after server restart" // first round after server restart has ID == 0
  487. : $"pre-round lobby for round {_gameTicker.RoundId + 1}",
  488. GameRunLevel.InRound => $"round {_gameTicker.RoundId}",
  489. GameRunLevel.PostRound => $"post-round {_gameTicker.RoundId}",
  490. _ => throw new ArgumentOutOfRangeException(nameof(_gameTicker.RunLevel),
  491. $"{_gameTicker.RunLevel} was not matched."),
  492. };
  493. return new WebhookPayload
  494. {
  495. Username = username,
  496. AvatarUrl = string.IsNullOrWhiteSpace(_avatarUrl) ? null : _avatarUrl,
  497. Embeds = new List<WebhookEmbed>
  498. {
  499. new()
  500. {
  501. Description = messages,
  502. Color = color,
  503. Footer = new WebhookEmbedFooter
  504. {
  505. Text = $"{serverName} ({round})",
  506. IconUrl = string.IsNullOrWhiteSpace(_footerIconUrl) ? null : _footerIconUrl
  507. },
  508. },
  509. },
  510. };
  511. }
  512. public override void Update(float frameTime)
  513. {
  514. base.Update(frameTime);
  515. foreach (var userId in _messageQueues.Keys.ToArray())
  516. {
  517. if (_processingChannels.Contains(userId))
  518. continue;
  519. var queue = _messageQueues[userId];
  520. _messageQueues.Remove(userId);
  521. if (queue.Count == 0)
  522. continue;
  523. _processingChannels.Add(userId);
  524. ProcessQueue(userId, queue);
  525. }
  526. }
  527. protected override void OnBwoinkTextMessage(BwoinkTextMessage message, EntitySessionEventArgs eventArgs)
  528. {
  529. base.OnBwoinkTextMessage(message, eventArgs);
  530. _activeConversations[message.UserId] = DateTime.Now;
  531. var senderSession = eventArgs.SenderSession;
  532. // TODO: Sanitize text?
  533. // Confirm that this person is actually allowed to send a message here.
  534. var personalChannel = senderSession.UserId == message.UserId;
  535. var senderAdmin = _adminManager.GetAdminData(senderSession);
  536. var senderAHelpAdmin = senderAdmin?.HasFlag(AdminFlags.Adminhelp) ?? false;
  537. var authorized = personalChannel && !message.AdminOnly || senderAHelpAdmin;
  538. if (!authorized)
  539. {
  540. // Unauthorized bwoink (log?)
  541. return;
  542. }
  543. if (_rateLimit.CountAction(eventArgs.SenderSession, RateLimitKey) != RateLimitStatus.Allowed)
  544. return;
  545. var escapedText = FormattedMessage.EscapeText(message.Text);
  546. string bwoinkText;
  547. string adminPrefix = "";
  548. //Getting an administrator position
  549. if (_config.GetCVar(CCVars.AhelpAdminPrefix) && senderAdmin is not null && senderAdmin.Title is not null)
  550. {
  551. adminPrefix = $"[bold]\\[{senderAdmin.Title}\\][/bold] ";
  552. }
  553. if (senderAdmin is not null &&
  554. senderAdmin.Flags ==
  555. AdminFlags.Adminhelp) // Mentor. Not full admin. That's why it's colored differently.
  556. {
  557. bwoinkText = $"[color=purple]{adminPrefix}{senderSession.Name}[/color]";
  558. }
  559. else if (senderAdmin is not null && senderAdmin.HasFlag(AdminFlags.Adminhelp))
  560. {
  561. bwoinkText = $"[color=red]{adminPrefix}{senderSession.Name}[/color]";
  562. }
  563. else
  564. {
  565. bwoinkText = $"{senderSession.Name}";
  566. }
  567. bwoinkText = $"{(message.AdminOnly ? Loc.GetString("bwoink-message-admin-only") : !message.PlaySound ? Loc.GetString("bwoink-message-silent") : "")} {bwoinkText}: {escapedText}";
  568. // If it's not an admin / admin chooses to keep the sound and message is not an admin only message, then play it.
  569. var playSound = (!senderAHelpAdmin || message.PlaySound) && !message.AdminOnly;
  570. var msg = new BwoinkTextMessage(message.UserId, senderSession.UserId, bwoinkText, playSound: playSound, adminOnly: message.AdminOnly);
  571. LogBwoink(msg);
  572. var admins = GetTargetAdmins();
  573. // Notify all admins
  574. foreach (var channel in admins)
  575. {
  576. RaiseNetworkEvent(msg, channel);
  577. }
  578. string adminPrefixWebhook = "";
  579. if (_config.GetCVar(CCVars.AhelpAdminPrefixWebhook) && senderAdmin is not null && senderAdmin.Title is not null)
  580. {
  581. adminPrefixWebhook = $"[bold]\\[{senderAdmin.Title}\\][/bold] ";
  582. }
  583. // Notify player
  584. if (_playerManager.TryGetSessionById(message.UserId, out var session) && !message.AdminOnly)
  585. {
  586. if (!admins.Contains(session.Channel))
  587. {
  588. // If _overrideClientName is set, we generate a new message with the override name. The admins name will still be the original name for the webhooks.
  589. if (_overrideClientName != string.Empty)
  590. {
  591. string overrideMsgText;
  592. // Doing the same thing as above, but with the override name. Theres probably a better way to do this.
  593. if (senderAdmin is not null &&
  594. senderAdmin.Flags ==
  595. AdminFlags.Adminhelp) // Mentor. Not full admin. That's why it's colored differently.
  596. {
  597. overrideMsgText = $"[color=purple]{adminPrefixWebhook}{_overrideClientName}[/color]";
  598. }
  599. else if (senderAdmin is not null && senderAdmin.HasFlag(AdminFlags.Adminhelp))
  600. {
  601. overrideMsgText = $"[color=red]{adminPrefixWebhook}{_overrideClientName}[/color]";
  602. }
  603. else
  604. {
  605. overrideMsgText = $"{senderSession.Name}"; // Not an admin, name is not overridden.
  606. }
  607. overrideMsgText = $"{(message.PlaySound ? "" : "(S) ")}{overrideMsgText}: {escapedText}";
  608. RaiseNetworkEvent(new BwoinkTextMessage(message.UserId,
  609. senderSession.UserId,
  610. overrideMsgText,
  611. playSound: playSound),
  612. session.Channel);
  613. }
  614. else
  615. RaiseNetworkEvent(msg, session.Channel);
  616. }
  617. }
  618. var sendsWebhook = _webhookUrl != string.Empty;
  619. if (sendsWebhook)
  620. {
  621. if (!_messageQueues.ContainsKey(msg.UserId))
  622. _messageQueues[msg.UserId] = new Queue<DiscordRelayedData>();
  623. var str = message.Text;
  624. var unameLength = senderSession.Name.Length;
  625. if (unameLength + str.Length + _maxAdditionalChars > DescriptionMax)
  626. {
  627. str = str[..(DescriptionMax - _maxAdditionalChars - unameLength)];
  628. }
  629. var nonAfkAdmins = GetNonAfkAdmins();
  630. var messageParams = new AHelpMessageParams(
  631. senderSession.Name,
  632. str,
  633. !personalChannel,
  634. _gameTicker.RoundDuration().ToString("hh\\:mm\\:ss"),
  635. _gameTicker.RunLevel,
  636. playedSound: playSound,
  637. adminOnly: message.AdminOnly,
  638. noReceivers: nonAfkAdmins.Count == 0
  639. );
  640. _messageQueues[msg.UserId].Enqueue(GenerateAHelpMessage(messageParams));
  641. }
  642. if (admins.Count != 0 || sendsWebhook)
  643. return;
  644. // No admin online, let the player know
  645. var systemText = Loc.GetString("bwoink-system-starmute-message-no-other-users");
  646. var starMuteMsg = new BwoinkTextMessage(message.UserId, SystemUserId, systemText);
  647. RaiseNetworkEvent(starMuteMsg, senderSession.Channel);
  648. }
  649. private IList<INetChannel> GetNonAfkAdmins()
  650. {
  651. return _adminManager.ActiveAdmins
  652. .Where(p => (_adminManager.GetAdminData(p)?.HasFlag(AdminFlags.Adminhelp) ?? false) &&
  653. !_afkManager.IsAfk(p))
  654. .Select(p => p.Channel)
  655. .ToList();
  656. }
  657. private IList<INetChannel> GetTargetAdmins()
  658. {
  659. return _adminManager.ActiveAdmins
  660. .Where(p => _adminManager.GetAdminData(p)?.HasFlag(AdminFlags.Adminhelp) ?? false)
  661. .Select(p => p.Channel)
  662. .ToList();
  663. }
  664. private DiscordRelayedData GenerateAHelpMessage(AHelpMessageParams parameters)
  665. {
  666. var stringbuilder = new StringBuilder();
  667. if (parameters.Icon != null)
  668. stringbuilder.Append(parameters.Icon);
  669. else if (parameters.IsAdmin)
  670. stringbuilder.Append(":outbox_tray:");
  671. else if (parameters.NoReceivers)
  672. stringbuilder.Append(":sos:");
  673. else
  674. stringbuilder.Append(":inbox_tray:");
  675. if (parameters.RoundTime != string.Empty && parameters.RoundState == GameRunLevel.InRound)
  676. stringbuilder.Append($" **{parameters.RoundTime}**");
  677. if (!parameters.PlayedSound)
  678. stringbuilder.Append($" **{(parameters.AdminOnly ? Loc.GetString("bwoink-message-admin-only") : Loc.GetString("bwoink-message-silent"))}**");
  679. if (parameters.Icon == null)
  680. stringbuilder.Append($" **{parameters.Username}:** ");
  681. else
  682. stringbuilder.Append($" **{parameters.Username}** ");
  683. stringbuilder.Append(parameters.Message);
  684. return new DiscordRelayedData()
  685. {
  686. Receivers = !parameters.NoReceivers,
  687. Message = stringbuilder.ToString(),
  688. };
  689. }
  690. private record struct DiscordRelayedData
  691. {
  692. /// <summary>
  693. /// Was anyone online to receive it.
  694. /// </summary>
  695. public bool Receivers;
  696. /// <summary>
  697. /// What's the payload to send to discord.
  698. /// </summary>
  699. public string Message;
  700. }
  701. /// <summary>
  702. /// Class specifically for holding information regarding existing Discord embeds
  703. /// </summary>
  704. private sealed class DiscordRelayInteraction
  705. {
  706. public string? Id;
  707. public string Username = String.Empty;
  708. public string? CharacterName;
  709. /// <summary>
  710. /// Contents for the discord message.
  711. /// </summary>
  712. public string Description = string.Empty;
  713. /// <summary>
  714. /// Run level of the last interaction. If different we'll link to the last Id.
  715. /// </summary>
  716. public GameRunLevel LastRunLevel;
  717. /// <summary>
  718. /// Did we relay this interaction to OnCall previously.
  719. /// </summary>
  720. public bool OnCall;
  721. }
  722. }
  723. public sealed class AHelpMessageParams
  724. {
  725. public string Username { get; set; }
  726. public string Message { get; set; }
  727. public bool IsAdmin { get; set; }
  728. public string RoundTime { get; set; }
  729. public GameRunLevel RoundState { get; set; }
  730. public bool PlayedSound { get; set; }
  731. public readonly bool AdminOnly;
  732. public bool NoReceivers { get; set; }
  733. public string? Icon { get; set; }
  734. public AHelpMessageParams(
  735. string username,
  736. string message,
  737. bool isAdmin,
  738. string roundTime,
  739. GameRunLevel roundState,
  740. bool playedSound,
  741. bool adminOnly = false,
  742. bool noReceivers = false,
  743. string? icon = null)
  744. {
  745. Username = username;
  746. Message = message;
  747. IsAdmin = isAdmin;
  748. RoundTime = roundTime;
  749. RoundState = roundState;
  750. PlayedSound = playedSound;
  751. AdminOnly = adminOnly;
  752. NoReceivers = noReceivers;
  753. Icon = icon;
  754. }
  755. }
  756. public enum PlayerStatusType
  757. {
  758. Connected,
  759. Disconnected,
  760. Banned,
  761. }
  762. }