mirror of
https://github.com/space-syndicate/space-station-14.git
synced 2026-10-24 17:21:18 +03:00
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:
parent
c2f0a5e385
commit
f40e2acdd8
6 changed files with 81 additions and 0 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -449,6 +449,7 @@ namespace Content.Server.GameTicking
|
|||
Logger.InfoS("ticker", "Restarting round!");
|
||||
|
||||
SendServerMessage(Loc.GetString("game-ticker-restart-round"));
|
||||
SendDiscordNewRoundAlert();
|
||||
|
||||
RoundNumberMetric.Inc();
|
||||
|
||||
|
|
|
|||
64
Content.Server/GameTicking/GameTicket.Discord.cs
Normal file
64
Content.Server/GameTicking/GameTicket.Discord.cs
Normal 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>() }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
*/
|
||||
|
|
|
|||
1
Resources/Locale/en-US/game-ticking/discord-round.ftl
Normal file
1
Resources/Locale/en-US/game-ticking/discord-round.ftl
Normal file
|
|
@ -0,0 +1 @@
|
|||
discord-round-new = New round started!
|
||||
1
Resources/Locale/ru-RU/game-ticking/discord-round.ftl
Normal file
1
Resources/Locale/ru-RU/game-ticking/discord-round.ftl
Normal file
|
|
@ -0,0 +1 @@
|
|||
discord-round-new = Новый раунд начался!
|
||||
Loading…
Add table
Reference in a new issue