forked from SerbiaStrong-220/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordBanPostManager.cs
155 lines (126 loc) · 4.23 KB
/
DiscordBanPostManager.cs
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
// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt
using Content.Shared.Corvax.CCCVars;
using Robust.Shared.Configuration;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using System.Text.Json;
namespace Content.Server.SS220.Discord;
public sealed class DiscordBanPostManager
{
[Dependency] private readonly IConfigurationManager _cfg = default!;
private ISawmill _sawmill = default!;
private readonly HttpClient _httpClient = new();
private string _apiUrl = string.Empty;
public void Initialize()
{
_sawmill = Logger.GetSawmill("DiscordPlayerManager");
_cfg.OnValueChanged(CCCVars.DiscordAuthApiUrl, v => _apiUrl = v, true);
_cfg.OnValueChanged(CCCVars.DiscordAuthApiKey, v =>
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", v);
},
true);
}
public async Task PostUserBanInfo(int banId)
{
if (string.IsNullOrEmpty(_apiUrl))
{
return;
}
try
{
var url = $"{_apiUrl}/userBan/{banId}";
var response = await _httpClient.PostAsync(url, content: null);
if (response.StatusCode != HttpStatusCode.OK)
{
var errorText = await response.Content.ReadAsStringAsync();
_sawmill.Error(
"Failed to post user ban: [{StatusCode}] {Response}",
response.StatusCode,
errorText);
}
}
catch (Exception exc)
{
_sawmill.Error($"Error while posting user ban. {exc.Message}");
}
}
private readonly Dictionary<string, List<int>> _userBanCache = new();
private readonly Dictionary<string, Timer> _userJobBanPostTimers = new();
public async Task PostUserJobBanInfo(int banId, string? targetUserName)
{
try
{
if (!string.IsNullOrWhiteSpace(targetUserName))
{
AddUserJobBanToCache(banId, targetUserName);
AddUserJobBanTimer(targetUserName);
}
}
catch (Exception exc)
{
_sawmill.Error($"Error while cached user role ban. {exc.Message}");
}
}
private void AddUserJobBanTimer(string targetUserName)
{
if (!_userJobBanPostTimers.TryGetValue(targetUserName, out var timer))
{
timer = new()
{
AutoReset = false
};
timer.Elapsed += async (sender, e) => await JobBanProccessComplete(targetUserName);
_userJobBanPostTimers[targetUserName] = timer;
}
timer.Stop();
timer.Interval = TimeSpan.FromMinutes(1).TotalMilliseconds;
timer.Start();
}
private async Task JobBanProccessComplete(string userName)
{
if (string.IsNullOrEmpty(_apiUrl))
{
return;
}
_userBanCache.Remove(userName, out var bans);
if (bans is null)
{
return;
}
try
{
var url = $"{_apiUrl}/userBan/roleBan";
var response = await _httpClient.PostAsync(url,
new StringContent(
JsonSerializer.Serialize(bans),
Encoding.UTF8,
"application/json"));
if (response.StatusCode != HttpStatusCode.OK)
{
var errorText = await response.Content.ReadAsStringAsync();
_sawmill.Error(
"Failed to post user role ban: [{StatusCode}] {Response}",
response.StatusCode,
errorText);
}
}
catch (Exception exc)
{
_sawmill.Error($"Error while posting user role ban. {exc.Message}");
}
}
private void AddUserJobBanToCache(int banId, string targetUsername)
{
if (!_userBanCache.TryGetValue(targetUsername, out var cache))
{
cache = [];
_userBanCache[targetUsername] = cache;
}
cache.Add(banId);
}
}