Add Discord round start notifications (#95)

* Add new round Discord role alert

* Add russian locale for Discord alert

* Make Discord role id var optional

* Rename new_round ccvars to just round
This commit is contained in:
Morbo 2022-02-11 00:09:44 +03:00 • committed by GitHub
parent c2f0a5e385
commit f40e2acdd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 0 deletions

View file

@ -27,6 +27,12 @@ namespace Content.Server.GameTicking
[ViewVariables]
public float MaxStationOffset { get; private set; } = 0f;
[ViewVariables]
public string DiscordWebhook { get; private set; } = "";
[ViewVariables]
public string DiscordRoleId { get; private set; } = "";
private void InitializeCVars()
{
_configurationManager.OnValueChanged(CCVars.GameLobbyEnabled, value => LobbyEnabled = value, true);
@ -37,6 +43,8 @@ namespace Content.Server.GameTicking
_configurationManager.OnValueChanged(CCVars.StationOffset, value => StationOffset = value, true);
_configurationManager.OnValueChanged(CCVars.StationRotation, value => StationRotation = value, true);
_configurationManager.OnValueChanged(CCVars.MaxStationOffset, value => MaxStationOffset = value, true);
_configurationManager.OnValueChanged(CCVars.DiscordRoundWebhook, value => DiscordWebhook = value, true);
_configurationManager.OnValueChanged(CCVars.DiscordRoundRoleId, value => DiscordRoleId = value, true);
}
}
}

View file

@ -449,6 +449,7 @@ namespace Content.Server.GameTicking
Logger.InfoS("ticker", "Restarting round!");
SendServerMessage(Loc.GetString("game-ticker-restart-round"));
SendDiscordNewRoundAlert();
RoundNumberMetric.Inc();

View file

@ -0,0 +1,64 @@

using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Content.Server.GameTicking
{
public partial class GameTicker
{
private readonly HttpClient _httpClient = new();
private async void SendDiscordNewRoundAlert()
{
var sendsWebhook = DiscordWebhook != string.Empty;
if (sendsWebhook)
{
WebhookPayload payload;
if (DiscordRoleId != string.Empty)
{
payload = new WebhookPayload()
{
Content = $"<@&{DiscordRoleId}> {Loc.GetString("discord-round-new")}",
AllowedMentions = new Dictionary<string, string[]>
{
{ "roles", new []{ DiscordRoleId } }
},
};
}
else
{
payload = new WebhookPayload()
{
Content = Loc.GetString("discord-round-new"),
};
}
var request = await _httpClient.PostAsync(DiscordWebhook,
new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"));
var content = await request.Content.ReadAsStringAsync();
if (!request.IsSuccessStatusCode)
{
Logger.ErrorS("ticker", $"Discord returned bad status code when posting message: {request.StatusCode}\nResponse: {content}");
return;
}
}
}
private struct WebhookPayload
{
[JsonPropertyName("content")]
public string Content { get; set; } = "";
[JsonPropertyName("allowed_mentions")]
public Dictionary<string, string[]> AllowedMentions { get; set; } =
new()
{
{ "parse", Array.Empty<string>() }
};
}
}
}

View file

@ -161,6 +161,12 @@ namespace Content.Shared.CCVar
public static readonly CVarDef<string> DiscordAHelpWebhook =
CVarDef.Create("discord.ahelp_webhook", string.Empty, CVar.SERVERONLY);
public static readonly CVarDef<string> DiscordRoundWebhook =
CVarDef.Create("discord.round_webhook", string.Empty, CVar.SERVERONLY);
public static readonly CVarDef<string> DiscordRoundRoleId =
CVarDef.Create("discord.round_roleid", string.Empty, CVar.SERVERONLY);
/*
* Suspicion
*/

View file

@ -0,0 +1 @@
discord-round-new = New round started!

View file

@ -0,0 +1 @@
discord-round-new = Новый раунд начался!