Enrich discord round notifications (#19502)

This commit is contained in:
Morb
2023-08-25 05:53:32 +03:00
committed by GitHub
parent 58d1aa3b1e
commit c73a90e16c
4 changed files with 49 additions and 23 deletions

View File

@@ -31,7 +31,7 @@ namespace Content.Server.GameTicking
public string? ServerName { get; private set; } public string? ServerName { get; private set; }
[ViewVariables] [ViewVariables]
private long? DiscordRoundEndRole { get; set; } private string? DiscordRoundEndRole { get; set; }
private WebhookIdentifier? _webhookIdentifier; private WebhookIdentifier? _webhookIdentifier;
@@ -65,7 +65,7 @@ namespace Content.Server.GameTicking
// TODO why tf is the server name on admin logs // TODO why tf is the server name on admin logs
ServerName = value; ServerName = value;
}, true); }, true);
_configurationManager.OnValueChanged(CCVars.DiscordRoundRestartWebhook, value => _configurationManager.OnValueChanged(CCVars.DiscordRoundUpdateWebhook, value =>
{ {
if (!string.IsNullOrWhiteSpace(value)) if (!string.IsNullOrWhiteSpace(value))
{ {
@@ -76,7 +76,7 @@ namespace Content.Server.GameTicking
{ {
DiscordRoundEndRole = value; DiscordRoundEndRole = value;
if (value == 0) if (value == string.Empty)
{ {
DiscordRoundEndRole = null; DiscordRoundEndRole = null;
} }

View File

@@ -252,6 +252,7 @@ namespace Content.Server.GameTicking
UpdateLateJoinStatus(); UpdateLateJoinStatus();
AnnounceRound(); AnnounceRound();
UpdateInfoText(); UpdateInfoText();
SendRoundStartedDiscordMessage();
#if EXCEPTION_TOLERANCE #if EXCEPTION_TOLERANCE
} }
@@ -395,16 +396,21 @@ namespace Content.Server.GameTicking
if (_webhookIdentifier == null) if (_webhookIdentifier == null)
return; return;
var content = "The round has ended."; var duration = RoundDuration();
if (DiscordRoundEndRole != null) var content = Loc.GetString("discord-round-notifications-end",
content += $"\n\n<@&{DiscordRoundEndRole}>, the server will reboot shortly!"; ("id", RoundId),
("hours", Math.Truncate(duration.TotalHours)),
("minutes", duration.Minutes),
("seconds", duration.Seconds));
var payload = new WebhookPayload { Content = content };
var payload = new WebhookPayload await _discord.CreateMessage(_webhookIdentifier.Value, payload);
{
Content = content,
Username = ServerName,
};
if (DiscordRoundEndRole == null)
return;
content = Loc.GetString("discord-round-notifications-end-ping", ("roleId", DiscordRoundEndRole));
payload = new WebhookPayload { Content = content };
payload.AllowedMentions.AllowRoleMentions(); payload.AllowedMentions.AllowRoleMentions();
await _discord.CreateMessage(_webhookIdentifier.Value, payload); await _discord.CreateMessage(_webhookIdentifier.Value, payload);
@@ -439,6 +445,7 @@ namespace Content.Server.GameTicking
RandomizeLobbyBackground(); RandomizeLobbyBackground();
ResettingCleanup(); ResettingCleanup();
IncrementRoundNumber(); IncrementRoundNumber();
SendRoundStartingDiscordMessage();
if (!LobbyEnabled) if (!LobbyEnabled)
{ {
@@ -456,8 +463,6 @@ namespace Content.Server.GameTicking
ReqWindowAttentionAll(); ReqWindowAttentionAll();
} }
SendRoundStartingDiscordMessage();
} }
private async void SendRoundStartingDiscordMessage() private async void SendRoundStartingDiscordMessage()
@@ -467,13 +472,9 @@ namespace Content.Server.GameTicking
if (_webhookIdentifier == null) if (_webhookIdentifier == null)
return; return;
var content = "New round starting!"; var content = Loc.GetString("discord-round-notifications-new");
var payload = new WebhookPayload var payload = new WebhookPayload { Content = content };
{
Content = content,
Username = ServerName,
};
await _discord.CreateMessage(_webhookIdentifier.Value, payload); await _discord.CreateMessage(_webhookIdentifier.Value, payload);
} }
@@ -609,6 +610,26 @@ namespace Content.Server.GameTicking
if (proto.Sound != null) if (proto.Sound != null)
SoundSystem.Play(proto.Sound.GetSound(), Filter.Broadcast()); SoundSystem.Play(proto.Sound.GetSound(), Filter.Broadcast());
} }
private async void SendRoundStartedDiscordMessage()
{
try
{
if (_webhookIdentifier == null)
return;
var mapName = _gameMapManager.GetSelectedMap()?.MapName ?? Loc.GetString("discord-round-notifications-unknown-map");
var content = Loc.GetString("discord-round-notifications-started", ("id", RoundId), ("map", mapName));
var payload = new WebhookPayload { Content = content };
await _discord.CreateMessage(_webhookIdentifier.Value, payload);
}
catch (Exception e)
{
Log.Error($"Error while sending discord round start message:\n{e}");
}
}
} }
public enum GameRunLevel public enum GameRunLevel

View File

@@ -344,14 +344,14 @@ namespace Content.Shared.CCVar
/// <summary> /// <summary>
/// URL of the Discord webhook which will relay round restart messages. /// URL of the Discord webhook which will relay round restart messages.
/// </summary> /// </summary>
public static readonly CVarDef<string> DiscordRoundRestartWebhook = public static readonly CVarDef<string> DiscordRoundUpdateWebhook =
CVarDef.Create("discord.round_restart_webhook", string.Empty, CVar.SERVERONLY); CVarDef.Create("discord.round_update_webhook", string.Empty, CVar.SERVERONLY);
/// <summary> /// <summary>
/// Role id for the Discord webhook to ping when the round ends. /// Role id for the Discord webhook to ping when the round ends.
/// </summary> /// </summary>
public static readonly CVarDef<long> DiscordRoundEndRoleWebhook = public static readonly CVarDef<string> DiscordRoundEndRoleWebhook =
CVarDef.Create("discord.round_end_role", 0L, CVar.SERVERONLY); CVarDef.Create("discord.round_end_role", string.Empty, CVar.SERVERONLY);
/* /*
* Suspicion * Suspicion

View File

@@ -0,0 +1,5 @@
discord-round-notifications-new = A new round is starting!
discord-round-notifications-started = Round #{$id} on map "{$map}" started.
discord-round-notifications-end = Round #{$id} has ended. It lasted for {$hours} hours, {$minutes} minutes, and {$seconds} seconds.
discord-round-notifications-end-ping = <@&{$roleId}>, the server will reboot shortly!
discord-round-notifications-unknown-map = Unknown