Skip to content

Commit

Permalink
Real fix of BansNotificationsSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr0maks committed Aug 12, 2023
1 parent 2572a3f commit f913f44
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Content.Server/AruMoon/BansNotificationsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Content.Shared.Database;
using Content.Shared.GameTicking;
using Content.Shared.Roles;
using Robust.Shared;
using Robust.Shared.Configuration;
using System.Net.Http;
using System.Net;
Expand All @@ -28,13 +29,16 @@ public sealed class BansNotificationsSystem : EntitySystem, IBansNotificationsSy
private ISawmill _sawmill = default!;
private readonly HttpClient _httpClient = new();
private string _webhookUrl = String.Empty;
private string _serverName = String.Empty;

public override void Initialize()
{
_sawmill = Logger.GetSawmill("bans_notifications");
SubscribeLocalEvent<BanEvent>(OnBan);
SubscribeLocalEvent<JobBanEvent>(OnJobBan);
SubscribeLocalEvent<DepartmentBanEvent>(OnDepartmentBan);
_config.OnValueChanged(CCVars.DiscordBanWebhook, value => _webhookUrl = value, true);
_config.OnValueChanged(CVars.GameHostName, value => _serverName = value, true);
}

public void RaiseLocalBanEvent(string username, DateTimeOffset? expires, string reason, NoteSeverity severity, string adminusername)
Expand All @@ -57,12 +61,12 @@ private async void SendDiscordMessage(WebhookPayload payload)
var request = await _httpClient.PostAsync(_webhookUrl,
new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"));

// _sawmill.Log(LogLevel.Debug, $"Discord webhook message: {JsonSerializer.Serialize(payload)}");
_sawmill.Debug($"Discord webhook json: {JsonSerializer.Serialize(payload)}");

var content = await request.Content.ReadAsStringAsync();
if (!request.IsSuccessStatusCode)
{
// _sawmill.Log(LogLevel.Error, $"Discord returned bad status code when posting message: {request.StatusCode}\nResponse: {content}");
_sawmill.Error($"Discord returned bad status code when posting message: {request.StatusCode}\nResponse: {content}");
return;
}
}
Expand All @@ -89,8 +93,9 @@ public void OnBan(BanEvent e)

var payload = new WebhookPayload
{

Username = _serverName,
/*
Username = username,
AvatarUrl = string.IsNullOrWhiteSpace(_avatarUrl) ? null : _avatarUrl,
*/
Embeds = new List<Embed>
Expand Down Expand Up @@ -137,8 +142,8 @@ public void OnJobBan(JobBanEvent e)

var payload = new WebhookPayload
{
Username = _serverName,
/*
Username = username,
AvatarUrl = string.IsNullOrWhiteSpace(_avatarUrl) ? null : _avatarUrl,
*/
Embeds = new List<Embed>
Expand Down

0 comments on commit f913f44

Please sign in to comment.