adds ahelp relay (#5837)
This commit is contained in:
@@ -1,12 +1,21 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Content.Server.Administration.Managers;
|
||||
using Content.Shared.Administration;
|
||||
using Content.Shared.CCVar;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Administration
|
||||
@@ -16,6 +25,37 @@ namespace Content.Server.Administration
|
||||
{
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IAdminManager _adminManager = default!;
|
||||
[Dependency] private readonly IConfigurationManager _config = default!;
|
||||
[Dependency] private readonly IPlayerLocator _playerLocator = default!;
|
||||
|
||||
private ISawmill? _sawmill;
|
||||
private readonly HttpClient _httpClient = new();
|
||||
private string _webhookUrl = string.Empty;
|
||||
private string _serverName = string.Empty;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_config.OnValueChanged(CCVars.DiscordAHelpWebhook, OnWebhookChanged, true);
|
||||
_config.OnValueChanged(CVars.GameHostName, OnServerNameChanged, true);
|
||||
}
|
||||
|
||||
private void OnServerNameChanged(string obj)
|
||||
{
|
||||
_serverName = obj;
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
_config.UnsubValueChanged(CCVars.DiscordAHelpWebhook, OnWebhookChanged);
|
||||
_config.UnsubValueChanged(CVars.GameHostName, OnServerNameChanged);
|
||||
}
|
||||
|
||||
private void OnWebhookChanged(string obj)
|
||||
{
|
||||
_webhookUrl = obj;
|
||||
}
|
||||
|
||||
protected override void OnBwoinkTextMessage(BwoinkTextMessage message, EntitySessionEventArgs eventArgs)
|
||||
{
|
||||
@@ -24,9 +64,9 @@ namespace Content.Server.Administration
|
||||
|
||||
// TODO: Sanitize text?
|
||||
// Confirm that this person is actually allowed to send a message here.
|
||||
var senderPersonalChannel = senderSession.UserId == message.ChannelId;
|
||||
var personalChannel = senderSession.UserId == message.ChannelId;
|
||||
var senderAdmin = _adminManager.GetAdminData(senderSession);
|
||||
var authorized = senderPersonalChannel || senderAdmin != null;
|
||||
var authorized = personalChannel || senderAdmin != null;
|
||||
if (!authorized)
|
||||
{
|
||||
// Unauthorized bwoink (log?)
|
||||
@@ -60,15 +100,65 @@ namespace Content.Server.Administration
|
||||
foreach (var channel in targets)
|
||||
RaiseNetworkEvent(msg, channel);
|
||||
|
||||
var sendsWebhook = _webhookUrl != string.Empty;
|
||||
if (sendsWebhook)
|
||||
{
|
||||
async void SendWebhook()
|
||||
{
|
||||
_sawmill ??= IoCManager.Resolve<ILogManager>().GetSawmill("AHELP");
|
||||
|
||||
var lookup = await _playerLocator.LookupIdAsync(message.ChannelId);
|
||||
|
||||
if (lookup == null)
|
||||
{
|
||||
_sawmill.Log(LogLevel.Error, $"Unable to find player for netuserid {msg.ChannelId} when sending discord webhook.");
|
||||
return;
|
||||
}
|
||||
|
||||
var payload = new WebhookPayload()
|
||||
{
|
||||
username = _serverName,
|
||||
content = $"`[{lookup.Username}]` {senderSession.Name}: \"{message.Text}\""
|
||||
};
|
||||
|
||||
var request = await _httpClient.PostAsync(_webhookUrl,
|
||||
new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"));
|
||||
|
||||
if (!request.IsSuccessStatusCode)
|
||||
{
|
||||
var content = await request.Content.ReadAsStringAsync();
|
||||
_sawmill.Log(LogLevel.Error, $"Discord returned bad status code: {request.StatusCode}\nResponse: {content}");
|
||||
}
|
||||
}
|
||||
|
||||
SendWebhook();
|
||||
}
|
||||
|
||||
if (targets.Count == 1)
|
||||
{
|
||||
var systemText = senderPersonalChannel ?
|
||||
Loc.GetString("bwoink-system-starmute-message-no-other-users-primary") :
|
||||
Loc.GetString("bwoink-system-starmute-message-no-other-users-secondary");
|
||||
var systemText = sendsWebhook ?
|
||||
Loc.GetString("bwoink-system-starmute-message-no-other-users-webhook") :
|
||||
Loc.GetString("bwoink-system-starmute-message-no-other-users");
|
||||
var starMuteMsg = new BwoinkTextMessage(message.ChannelId, SystemUserId, systemText);
|
||||
RaiseNetworkEvent(starMuteMsg, senderSession.ConnectedClient);
|
||||
}
|
||||
}
|
||||
|
||||
private struct WebhookPayload
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public string username { get; set; } = "";
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public string content { get; set; } = "";
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public Dictionary<string, string[]> allowed_mentions { get; set; } =
|
||||
new()
|
||||
{
|
||||
{ "parse", Array.Empty<string>() }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user