Add embeds to AHelp relay (#11184)

This commit is contained in:
Visne
2022-09-11 17:43:38 +02:00
committed by GitHub
parent de69830ab7
commit 0a3cf8325f
2 changed files with 141 additions and 45 deletions

View File

@@ -7,6 +7,7 @@ using System.Text.Json.Serialization;
using Content.Server.Administration.Managers; using Content.Server.Administration.Managers;
using Content.Server.GameTicking; using Content.Server.GameTicking;
using Content.Server.GameTicking.Events; using Content.Server.GameTicking.Events;
using Content.Server.Players;
using Content.Shared.Administration; using Content.Shared.Administration;
using Content.Shared.CCVar; using Content.Shared.CCVar;
using JetBrains.Annotations; using JetBrains.Annotations;
@@ -30,20 +31,27 @@ namespace Content.Server.Administration.Systems
private ISawmill _sawmill = default!; private ISawmill _sawmill = default!;
private readonly HttpClient _httpClient = new(); private readonly HttpClient _httpClient = new();
private string _webhookUrl = string.Empty; private string _webhookUrl = string.Empty;
private string _footerIconUrl = string.Empty;
private string _avatarUrl = string.Empty;
private string _serverName = string.Empty; private string _serverName = string.Empty;
private readonly Dictionary<NetUserId, (string id, string username, string content)> _relayMessages = new(); private readonly Dictionary<NetUserId, (string id, string username, string messages, string? characterName)> _relayMessages = new();
private readonly Dictionary<NetUserId, Queue<string>> _messageQueues = new(); private readonly Dictionary<NetUserId, Queue<string>> _messageQueues = new();
private readonly HashSet<NetUserId> _processingChannels = new(); private readonly HashSet<NetUserId> _processingChannels = new();
private const ushort MessageMax = 2000;
// Max embed description length is 4096, according to https://discord.com/developers/docs/resources/channel#embed-object-embed-limits
// Keep small margin, just to be safe
private const ushort DescriptionMax = 4000;
private int _maxAdditionalChars; private int _maxAdditionalChars;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
_config.OnValueChanged(CCVars.DiscordAHelpWebhook, OnWebhookChanged, true); _config.OnValueChanged(CCVars.DiscordAHelpWebhook, OnWebhookChanged, true);
_config.OnValueChanged(CCVars.DiscordAHelpFooterIcon, OnFooterIconChanged, true);
_config.OnValueChanged(CCVars.DiscordAHelpAvatar, OnAvatarChanged, true);
_config.OnValueChanged(CVars.GameHostName, OnServerNameChanged, true); _config.OnValueChanged(CVars.GameHostName, OnServerNameChanged, true);
_sawmill = IoCManager.Resolve<ILogManager>().GetSawmill("AHELP"); _sawmill = IoCManager.Resolve<ILogManager>().GetSawmill("AHELP");
_maxAdditionalChars = GenerateAHelpMessage("", "", true, true).Length; _maxAdditionalChars = GenerateAHelpMessage("", "", true).Length;
SubscribeLocalEvent<RoundStartingEvent>(RoundStarting); SubscribeLocalEvent<RoundStartingEvent>(RoundStarting);
} }
@@ -62,6 +70,7 @@ namespace Content.Server.Administration.Systems
{ {
base.Shutdown(); base.Shutdown();
_config.UnsubValueChanged(CCVars.DiscordAHelpWebhook, OnWebhookChanged); _config.UnsubValueChanged(CCVars.DiscordAHelpWebhook, OnWebhookChanged);
_config.UnsubValueChanged(CCVars.DiscordAHelpFooterIcon, OnFooterIconChanged);
_config.UnsubValueChanged(CVars.GameHostName, OnServerNameChanged); _config.UnsubValueChanged(CVars.GameHostName, OnServerNameChanged);
} }
@@ -70,9 +79,19 @@ namespace Content.Server.Administration.Systems
_webhookUrl = obj; _webhookUrl = obj;
} }
private void OnFooterIconChanged(string url)
{
_footerIconUrl = url;
}
private void OnAvatarChanged(string url)
{
_avatarUrl = url;
}
private async void ProcessQueue(NetUserId channelId, Queue<string> messages) private async void ProcessQueue(NetUserId channelId, Queue<string> messages)
{ {
if (!_relayMessages.TryGetValue(channelId, out var oldMessage) || messages.Sum(x => x.Length+2) + oldMessage.content.Length > MessageMax) if (!_relayMessages.TryGetValue(channelId, out var oldMessage) || messages.Sum(x => x.Length + 2) + oldMessage.messages.Length > DescriptionMax)
{ {
var lookup = await _playerLocator.LookupIdAsync(channelId); var lookup = await _playerLocator.LookupIdAsync(channelId);
@@ -83,18 +102,35 @@ namespace Content.Server.Administration.Systems
return; return;
} }
oldMessage = (string.Empty, lookup.Username, ""); var characterName = _playerManager.GetPlayerData(channelId).ContentData()?.Mind?.CharacterName;
oldMessage = (string.Empty, lookup.Username, string.Empty, characterName);
} }
while (messages.TryDequeue(out var message)) while (messages.TryDequeue(out var message))
{ {
oldMessage.content += $"\n{message}"; oldMessage.messages += $"\n{message}";
} }
var payload = new WebhookPayload() var payload = new WebhookPayload
{ {
Username = $"{oldMessage.username} on {_serverName} (round {_gameTicker.RoundId})", Username = $"{oldMessage.username}{(oldMessage.characterName == null ? string.Empty : $" ({oldMessage.characterName})")}",
Content = oldMessage.content AvatarUrl = _avatarUrl,
Embeds = new List<Embed>
{
new Embed
{
Description = oldMessage.messages,
// If no admins are online, set embed color to red. Otherwise green
Color = GetTargetAdmins().Count > 0 ? 0x41F097 : 0xFF0000,
Footer = new EmbedFooter
{
// Limit server name to 1500 characters, in case someone tries to be a little funny
Text = $"{_serverName[..Math.Min(_serverName.Length, 1500)]} (round {_gameTicker.RoundId})",
IconUrl = _footerIconUrl,
},
},
},
}; };
if (oldMessage.id == string.Empty) if (oldMessage.id == string.Empty)
@@ -105,7 +141,7 @@ namespace Content.Server.Administration.Systems
var content = await request.Content.ReadAsStringAsync(); var content = await request.Content.ReadAsStringAsync();
if (!request.IsSuccessStatusCode) if (!request.IsSuccessStatusCode)
{ {
_sawmill.Log(LogLevel.Error, $"Discord returned bad status code when posting message: {request.StatusCode}\nResponse: {content}"); _sawmill.Log(LogLevel.Error, $"Discord returned bad status code when posting message (perhaps the message is too long?): {request.StatusCode}\nResponse: {content}");
_relayMessages.Remove(channelId); _relayMessages.Remove(channelId);
return; return;
} }
@@ -128,7 +164,7 @@ namespace Content.Server.Administration.Systems
if (!request.IsSuccessStatusCode) if (!request.IsSuccessStatusCode)
{ {
var content = await request.Content.ReadAsStringAsync(); var content = await request.Content.ReadAsStringAsync();
_sawmill.Log(LogLevel.Error, $"Discord returned bad status code when patching message: {request.StatusCode}\nResponse: {content}"); _sawmill.Log(LogLevel.Error, $"Discord returned bad status code when patching message (perhaps the message is too long?): {request.StatusCode}\nResponse: {content}");
_relayMessages.Remove(channelId); _relayMessages.Remove(channelId);
return; return;
} }
@@ -145,11 +181,14 @@ namespace Content.Server.Administration.Systems
foreach (var channelId in _messageQueues.Keys.ToArray()) foreach (var channelId in _messageQueues.Keys.ToArray())
{ {
if(_processingChannels.Contains(channelId)) continue; if (_processingChannels.Contains(channelId))
continue;
var queue = _messageQueues[channelId]; var queue = _messageQueues[channelId];
_messageQueues.Remove(channelId); _messageQueues.Remove(channelId);
if (queue.Count == 0) continue; if (queue.Count == 0)
continue;
_processingChannels.Add(channelId); _processingChannels.Add(channelId);
ProcessQueue(channelId, queue); ProcessQueue(channelId, queue);
@@ -188,20 +227,20 @@ namespace Content.Server.Administration.Systems
LogBwoink(msg); LogBwoink(msg);
// Admins w/ AHelp access var admins = GetTargetAdmins();
var targets = _adminManager.ActiveAdmins
.Where(p => _adminManager.GetAdminData(p)?.HasFlag(AdminFlags.Adminhelp) ?? false)
.Select(p => p.ConnectedClient).ToList();
// And involved player // Notify all admins
if (_playerManager.TryGetSessionById(message.ChannelId, out var session)) foreach (var channel in admins)
if (!targets.Contains(session.ConnectedClient)) {
targets.Add(session.ConnectedClient);
foreach (var channel in targets)
RaiseNetworkEvent(msg, channel); RaiseNetworkEvent(msg, channel);
}
var noReceivers = targets.Count == 1; // Notify player
if (_playerManager.TryGetSessionById(message.ChannelId, out var session))
{
if (!admins.Contains(session.ConnectedClient))
RaiseNetworkEvent(msg, session.ConnectedClient);
}
var sendsWebhook = _webhookUrl != string.Empty; var sendsWebhook = _webhookUrl != string.Empty;
if (sendsWebhook) if (sendsWebhook)
@@ -212,52 +251,93 @@ namespace Content.Server.Administration.Systems
var str = message.Text; var str = message.Text;
var unameLength = senderSession.Name.Length; var unameLength = senderSession.Name.Length;
if (unameLength+str.Length+_maxAdditionalChars > MessageMax) if (unameLength + str.Length + _maxAdditionalChars > DescriptionMax)
{ {
str = str[..(MessageMax - _maxAdditionalChars - unameLength)]; str = str[..(DescriptionMax - _maxAdditionalChars - unameLength)];
} }
_messageQueues[msg.ChannelId].Enqueue(GenerateAHelpMessage(senderSession.Name, str, !personalChannel, noReceivers)); _messageQueues[msg.ChannelId].Enqueue(GenerateAHelpMessage(senderSession.Name, str, !personalChannel));
} }
if (noReceivers) if (admins.Count != 0)
{ return;
var systemText = sendsWebhook ?
Loc.GetString("bwoink-system-starmute-message-no-other-users-webhook") : // No admin online, let the player know
Loc.GetString("bwoink-system-starmute-message-no-other-users"); var systemText = sendsWebhook ?
var starMuteMsg = new BwoinkTextMessage(message.ChannelId, SystemUserId, systemText); Loc.GetString("bwoink-system-starmute-message-no-other-users-webhook") :
RaiseNetworkEvent(starMuteMsg, senderSession.ConnectedClient); Loc.GetString("bwoink-system-starmute-message-no-other-users");
} var starMuteMsg = new BwoinkTextMessage(message.ChannelId, SystemUserId, systemText);
RaiseNetworkEvent(starMuteMsg, senderSession.ConnectedClient);
} }
private string GenerateAHelpMessage(string username, string message, bool admin, bool noReceiver) // Returns all online admins with AHelp access
private IList<INetChannel> GetTargetAdmins()
{
return _adminManager.ActiveAdmins
.Where(p => _adminManager.GetAdminData(p)?.HasFlag(AdminFlags.Adminhelp) ?? false)
.Select(p => p.ConnectedClient)
.ToList();
}
private static string GenerateAHelpMessage(string username, string message, bool admin)
{ {
var stringbuilder = new StringBuilder(); var stringbuilder = new StringBuilder();
if (noReceiver)
stringbuilder.Append(":sos:");
stringbuilder.Append(admin ? ":outbox_tray:" : ":inbox_tray:"); stringbuilder.Append(admin ? ":outbox_tray:" : ":inbox_tray:");
stringbuilder.Append(" **"); stringbuilder.Append($" **{username}:** ");
stringbuilder.Append(username);
stringbuilder.Append(":** ");
stringbuilder.Append(message); stringbuilder.Append(message);
return stringbuilder.ToString(); return stringbuilder.ToString();
} }
// https://discord.com/developers/docs/resources/channel#message-object-message-structure
private struct WebhookPayload private struct WebhookPayload
{ {
[JsonPropertyName("username")] [JsonPropertyName("username")]
public string Username { get; set; } = ""; public string Username { get; set; } = "";
[JsonPropertyName("content")] [JsonPropertyName("avatar_url")]
public string Content { get; set; } = ""; public string AvatarUrl { get; set; } = "";
[JsonPropertyName("embeds")]
public List<Embed>? Embeds { get; set; } = null;
[JsonPropertyName("allowed_mentions")] [JsonPropertyName("allowed_mentions")]
public Dictionary<string, string[]> AllowedMentions { get; set; } = public Dictionary<string, string[]> AllowedMentions { get; set; } =
new() new()
{ {
{ "parse", Array.Empty<string>() } { "parse", Array.Empty<string>() },
}; };
public WebhookPayload() {} public WebhookPayload() { }
}
// https://discord.com/developers/docs/resources/channel#embed-object-embed-structure
private struct Embed
{
[JsonPropertyName("description")]
public string Description { get; set; } = "";
[JsonPropertyName("color")]
public int Color { get; set; } = 0;
[JsonPropertyName("footer")]
public EmbedFooter? Footer { get; set; } = null;
public Embed()
{
}
}
// https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure
private struct EmbedFooter
{
[JsonPropertyName("text")]
public string Text { get; set; } = "";
[JsonPropertyName("icon_url")]
public string IconUrl { get; set; } = "";
public EmbedFooter()
{
}
} }
} }
} }

View File

@@ -250,9 +250,25 @@ namespace Content.Shared.CCVar
* Discord * Discord
*/ */
/// <summary>
/// URL of the Discord webhook which will relay all ahelp messages.
/// </summary>
public static readonly CVarDef<string> DiscordAHelpWebhook = public static readonly CVarDef<string> DiscordAHelpWebhook =
CVarDef.Create("discord.ahelp_webhook", string.Empty, CVar.SERVERONLY); CVarDef.Create("discord.ahelp_webhook", string.Empty, CVar.SERVERONLY);
/// <summary>
/// The server icon to use in the Discord ahelp embed footer.
/// Valid values are specified at https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure.
/// </summary>
public static readonly CVarDef<string> DiscordAHelpFooterIcon =
CVarDef.Create("discord.ahelp_footer_icon", string.Empty, CVar.SERVERONLY);
/// <summary>
/// The avatar to use for the webhook. Should be an URL.
/// </summary>
public static readonly CVarDef<string> DiscordAHelpAvatar =
CVarDef.Create("discord.ahelp_avatar", string.Empty, CVar.SERVERONLY);
/* /*
* Suspicion * Suspicion
*/ */