Skip to content

Commit

Permalink
Merge pull request #107 from Lemirda/master-andromeda
Browse files Browse the repository at this point in the history
БОЛЬШОЙ БРАТ ЗА АДМИНАМИ
  • Loading branch information
13lackHawk authored Apr 7, 2024
2 parents 6155ea0 + db20192 commit 3ce1251
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Content.Server/Administration/Managers/AdminManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
using Robust.Shared.Toolshed;
using Robust.Shared.Toolshed.Errors;
using Robust.Shared.Utility;

using Content.Server.Andromeda.AdministrationNotifications.GameTicking; //A-13 AdminNotifications

namespace Content.Server.Administration.Managers
{
public sealed partial class AdminManager : IAdminManager, IPostInjectInit, IConGroupControllerImplementation
{
[Dependency] private readonly IEntityManager _entityManager = default!; //A-13 AdminNotifications
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IServerDbManager _dbManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
Expand Down Expand Up @@ -342,7 +343,7 @@ private void PlayerStatusChanged(object? sender, SessionStatusEventArgs e)
}
else if (e.NewStatus == SessionStatus.Disconnected)
{
if (_admins.Remove(e.Session, out var reg ) && _cfg.GetCVar(CCVars.AdminAnnounceLogout))
if (_admins.TryGetValue(e.Session, out var reg ) && _cfg.GetCVar(CCVars.AdminAnnounceLogout)) //A-13 AdminNotifications
{
if (reg.Data.Stealth)
{
Expand All @@ -355,6 +356,12 @@ private void PlayerStatusChanged(object? sender, SessionStatusEventArgs e)
_chat.SendAdminAnnouncement(Loc.GetString("admin-manager-admin-logout-message",
("name", e.Session.Name)));
}
//A-13 AdminNotifications start
var logoutEvent = new AdminLoggedOutEvent(e.Session);
_entityManager.EventBus.RaiseEvent(EventSource.Local, logoutEvent);

_admins.Remove(e.Session);
//A-13 AdminNotifications end
}
}
}
Expand Down Expand Up @@ -398,6 +405,10 @@ private async void LoginAdminMaybe(ICommonSession session)
_chat.SendAdminAnnouncement(Loc.GetString("admin-manager-admin-login-message",
("name", session.Name)));
}
//A-13 AdminNotifications start
var loginEvent = new AdminLoggedInEvent(session);
_entityManager.EventBus.RaiseEvent(EventSource.Local, loginEvent);
//A-13 AdminNotifications end
}

SendPermsChangedEvent(session);
Expand Down
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);
}
}
}
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;
}
}
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;
}
}
3 changes: 3 additions & 0 deletions Content.Shared/Andromeda/CCVar/CCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ public sealed class AndromedaCCVars : CVars
/// </summary>
public static readonly CVarDef<string> DiscordBanWebhook =
CVarDef.Create("discord.ban_webhook", string.Empty, CVar.SERVERONLY);

public static readonly CVarDef<string> DiscordAdminWebhook =
CVarDef.Create("discord.admin_webhook", string.Empty, CVar.SERVERONLY);
}
}

0 comments on commit 3ce1251

Please sign in to comment.