-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdminSessionLog.cs
More file actions
242 lines (213 loc) · 10.4 KB
/
Copy pathAdminSessionLog.cs
File metadata and controls
242 lines (213 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Text;
using System.Text.Json.Serialization;
namespace AdminSessionLog
{
public class AdminSessionLog : BasePlugin
{
public override string ModuleName => "AdminSessionLog";
public override string ModuleVersion => "v1.1.0";
public override string ModuleAuthor => "E!N";
private AdminSessionLogConfig? _config;
private readonly Dictionary<ulong, DateTime> sessionStartTimes = [];
private readonly HttpClient _httpClient = new();
public override void OnAllPluginsLoaded(bool hotReload)
{
string configDirectory = GetConfigDirectory();
EnsureConfigDirectory(configDirectory);
string configPath = Path.Combine(configDirectory, "AdminSessionLogConfig.json");
_config = AdminSessionLogConfig.Load(configPath);
Logger.LogInformation($"Configuration loaded successfully. DiscordWebhookUrl = {_config.DiscordWebhookUrl}, AdminFlag = {string.Join(", ", _config.AdminFlag ?? [])}, AllowConnectMessage = {_config.AllowConnectMessage}");
}
private static string GetConfigDirectory()
{
return Path.Combine(Server.GameDirectory, "csgo/addons/counterstrikesharp/configs/plugins/AdminSessionLog/");
}
private void EnsureConfigDirectory(string directoryPath)
{
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
Logger.LogInformation($"Created configuration directory at: {directoryPath}");
}
}
[GameEventHandler]
public HookResult PlayerConnect(EventPlayerConnectFull @event, GameEventInfo info)
{
if (_config?.AdminFlag == null || @event.Userid == null)
{
Logger.LogError($"Admin flags are missing or Userid is null");
return HookResult.Continue;
}
if (_config.AdminFlag.Any(flag => AdminManager.PlayerHasPermissions(@event.Userid, flag)))
{
sessionStartTimes[@event.Userid.SteamID] = DateTime.Now;
if (_config.AllowConnectMessage)
{
SendDiscordMessage(CreateEmbedMessage(
Localizer["asl.TitleConnect"],
Localizer["asl.EmbedColorConnect"],
Localizer["asl.Administrator"],
Localizer["asl.AdministratorDescription", @event.Userid.PlayerName, @event.Userid.SteamID]
));
}
}
return HookResult.Continue;
}
[GameEventHandler]
public HookResult PlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo info)
{
if (@event.Userid == null)
{
return HookResult.Continue;
}
if (sessionStartTimes.TryGetValue(@event.Userid.SteamID, out DateTime startTime))
{
TimeSpan sessionLength = DateTime.Now - startTime;
string formattedLength = FormatSessionLength(sessionLength);
SendDiscordMessage(CreateEmbedMessage(
Localizer["asl.TitleDisconnect"],
Localizer["asl.EmbedColorDisconnect"],
Localizer["asl.Administrator"],
Localizer["asl.AdministratorDescription", @event.Name, @event.Xuid],
Localizer["asl.SessionLength"],
formattedLength
));
}
return HookResult.Continue;
}
private string FormatSessionLength(TimeSpan sessionLength)
{
int hours = sessionLength.Hours;
int minutes = sessionLength.Minutes;
int seconds = sessionLength.Seconds;
string hoursText = hours > 0 ? $"{hours} " + GetLocalizedPluralForm(hours, "Hours") + " " : "";
string minutesText = minutes > 0 ? $"{minutes} " + GetLocalizedPluralForm(minutes, "Minutes") + " " : "";
string secondsText = seconds > 0 ? $"{seconds} " + GetLocalizedPluralForm(seconds, "Seconds") : "";
string formattedLength = hoursText + minutesText + secondsText;
return string.IsNullOrWhiteSpace(formattedLength) ? Localizer["asl.LessThanSecond"] : formattedLength.Trim();
}
private string GetLocalizedPluralForm(int number, string type)
{
number %= 100;
if (number >= 11 && number <= 19)
{
return Localizer[$"asl.{type}.Many"];
}
int lastDigit = number % 10;
return lastDigit switch
{
1 => (string)Localizer[$"asl.{type}.One"],
2 or 3 or 4 => (string)Localizer[$"asl.{type}.Few"],
_ => (string)Localizer[$"asl.{type}.Many"],
};
}
private string CreateEmbedMessage(string title, string hexColor, string field1Title, string field1Description, string? field2Title = null, string? field2Description = null)
{
hexColor = hexColor.TrimStart('#');
if (!int.TryParse(hexColor, System.Globalization.NumberStyles.HexNumber, null, out int colorInt))
{
Logger.LogError($"Ошибка преобразования цвета: {hexColor}");
return string.Empty;
}
var fields = new List<Dictionary<string, object>>
{
new() {
{ "name", field1Title },
{ "value", field1Description },
{ "inline", false }
}
};
if (field2Title != null && field2Description != null)
{
fields.Add(new Dictionary<string, object>
{
{ "name", field2Title },
{ "value", field2Description },
{ "inline", true }
});
}
var embed = new Dictionary<string, object>
{
{ "title", title },
{ "color", colorInt },
{ "fields", fields }
};
string? hostname = ConVar.Find("hostname")?.StringValue;
if (!string.IsNullOrEmpty(hostname))
{
embed["description"] = $"{Localizer["asl.Server", hostname]}";
}
return JsonConvert.SerializeObject(new { embeds = new[] { embed } }, Formatting.Indented);
}
private async void SendDiscordMessage(string jsonPayload)
{
try
{
if (_config!.DiscordWebhookUrl != null)
{
using var request = new HttpRequestMessage(HttpMethod.Post, _config.DiscordWebhookUrl);
request.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
using var response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
Logger.LogError($"Ошибка отправки сообщения в Discord: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}");
}
}
}
catch (Exception ex)
{
Logger.LogError($"Ошибка отправки сообщения в Discord: {ex.Message}");
}
}
[ConsoleCommand("css_aslreload", "Reload Config")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/root")]
public void OnReloadConfigCommand(CCSPlayerController? player, CommandInfo commandInfo)
{
string configPath = Path.Combine(GetConfigDirectory(), "AdminSessionLogConfig.json");
_config = AdminSessionLogConfig.Load(configPath);
Logger.LogInformation($"Configuration reloaded successfully. DiscordWebhookUrl = {_config.DiscordWebhookUrl}, AdminFlag = {string.Join(", ", _config.AdminFlag ?? [])}, AllowConnectMessage = {_config.AllowConnectMessage}");
}
}
public class AdminSessionLogConfig
{
[JsonPropertyName("DiscordWebhookUrl")]
public string? DiscordWebhookUrl { get; set; }
public string[]? AdminFlag { get; set; } = ["@css/reservation",
"@css/generic",
"@css/kick",
"@css/ban",
"@css/unban",
"@css/vip",
"@css/slay",
"@css/changemap",
"@css/cvar",
"@css/config",
"@css/chat",
"@css/vote",
"@css/password",
"@css/rcon",
"@css/cheats",
"@css/root"];
public bool AllowConnectMessage { get; set; } = true;
public static AdminSessionLogConfig Load(string configPath)
{
if (!File.Exists(configPath))
{
AdminSessionLogConfig defaultConfig = new();
File.WriteAllText(configPath, JsonConvert.SerializeObject(defaultConfig, Formatting.Indented));
return defaultConfig;
}
string json = File.ReadAllText(configPath);
return JsonConvert.DeserializeObject<AdminSessionLogConfig>(json) ?? new AdminSessionLogConfig();
}
}
}