forked from space-syndicate/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from Lemirda/master-andromeda
БОЛЬШОЙ БРАТ ЗА АДМИНАМИ
- Loading branch information
Showing
5 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Content.Server/Andromeda/AdministrationNotifications/AdminNotificationsSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Content.Shared.Administration; | ||
using Robust.Shared; | ||
using Robust.Shared.Configuration; | ||
using Robust.Shared.Player; | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
using System.Text; | ||
using Content.Server.Discord; | ||
using Content.Shared.CCVar; | ||
using Content.Shared.Andromeda.CCVar; | ||
using Content.Server.Andromeda.AdministrationNotifications.GameTicking; | ||
|
||
namespace Content.Server.Andromeda.AdministrationNotifications | ||
{ | ||
|
||
public sealed class AdminNotificationsSystem : EntitySystem | ||
{ | ||
[Dependency] private readonly IConfigurationManager _config = default!; | ||
private ISawmill _sawmill = default!; | ||
private readonly HttpClient _httpClient = new(); | ||
private string _webhookUrl = String.Empty; | ||
private int _adminCount = 0; | ||
|
||
public override void Initialize() | ||
{ | ||
_sawmill = Logger.GetSawmill("admin_notifications"); | ||
SubscribeLocalEvent<AdminLoggedInEvent>(OnAdminLoggedIn); | ||
SubscribeLocalEvent<AdminLoggedOutEvent>(OnAdminLoggedOut); | ||
_config.OnValueChanged(AndromedaCCVars.DiscordAdminWebhook, value => _webhookUrl = value, true); | ||
} | ||
|
||
private async void SendDiscordMessage(WebhookPayload payload) | ||
{ | ||
var request = await _httpClient.PostAsync(_webhookUrl, | ||
new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json")); | ||
|
||
_sawmill.Debug($"Вебхук Discord в формате json: {JsonSerializer.Serialize(payload)}"); | ||
|
||
var content = await request.Content.ReadAsStringAsync(); | ||
if (!request.IsSuccessStatusCode) | ||
{ | ||
_sawmill.Error($"Discord вернул неверный код статуса при публикации сообщения: {request.StatusCode}\nResponse: {content}"); | ||
return; | ||
} | ||
} | ||
|
||
private void OnAdminLoggedIn(AdminLoggedInEvent e) | ||
{ | ||
_adminCount++; | ||
SendAdminStatusUpdate(e.Session, "вошёл", 0x00FF00); | ||
} | ||
|
||
private void OnAdminLoggedOut(AdminLoggedOutEvent e) | ||
{ | ||
_adminCount--; | ||
SendAdminStatusUpdate(e.Session, "вышел", 0xFF0000); | ||
} | ||
|
||
private void SendAdminStatusUpdate(ICommonSession session, string action, int color) | ||
{ | ||
if (String.IsNullOrEmpty(_webhookUrl)) | ||
return; | ||
|
||
var message = $"{session.Name} {action}. Всего администраторов онлайн: {_adminCount}"; | ||
|
||
var payload = new WebhookPayload | ||
{ | ||
Username = "Отчёт входов админов", | ||
Embeds = new List<WebhookEmbed> | ||
{ | ||
new() | ||
{ | ||
Description = message, | ||
Color = color, | ||
}, | ||
}, | ||
}; | ||
|
||
SendDiscordMessage(payload); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Content.Server/Andromeda/AdministrationNotifications/GameTicking/AdminLoggedEvent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Robust.Shared.Player; | ||
|
||
namespace Content.Server.Andromeda.AdministrationNotifications.GameTicking; | ||
|
||
public sealed class AdminLoggedInEvent : EntityEventArgs | ||
{ | ||
public ICommonSession Session { get; } | ||
|
||
public AdminLoggedInEvent(ICommonSession session) | ||
{ | ||
Session = session; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Content.Server/Andromeda/AdministrationNotifications/GameTicking/AdminLoggedOutEvent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Robust.Shared.Player; | ||
|
||
namespace Content.Server.Andromeda.AdministrationNotifications.GameTicking; | ||
|
||
public sealed class AdminLoggedOutEvent : EntityEventArgs | ||
{ | ||
public ICommonSession Session { get; } | ||
|
||
public AdminLoggedOutEvent(ICommonSession session) | ||
{ | ||
Session = session; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters