-
Notifications
You must be signed in to change notification settings - Fork 1
/
Punishments.cs
451 lines (368 loc) · 17.8 KB
/
Punishments.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using BBRAPIModules;
using Commands;
using Permissions;
using System.ComponentModel;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using System.IO;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using BattleBitAPI.Features;
namespace BBRModules
{
[Module("A module with punishments such as ban and mute.", "1.0.0")]
[RequireModule(typeof(GranularPermissions))]
[RequireModule(typeof(CommandHandler))]
[RequireModule(typeof(SQLite))]
public class Punishments : BattleBitModule
{
#region Data files
public static PunishmentsConfiguration Configuration { get; set; }
public static SQLite Database { get; set; }
public static SqliteConnection Connection { get; set; }
#endregion
#region Modules
[ModuleReference]
public GranularPermissions GranularPermissions { get; set; } = null!;
[ModuleReference]
public CommandHandler CommandHandler { get; set; } = null!;
#endregion
public override void OnModulesLoaded()
{
CommandHandler.Register(this);
}
public override async Task OnConnected()
{
Database = new("Punishments");
await Database.Open();
await Database.CreateTable("punishments", new List<string>() {
"steamId TEXT PRIMARY KEY", "punishment INT", "punishedAt INT", "punishedFor INT", "punishedBy TEXT", "reason TEXT"
});
Connection = Database.Connection;
}
public override async Task OnDisconnected()
{
await Database.Close();
}
#region Commands
[CommandCallback("ban", Description = "Ban a player permanently.", Permissions = new[] { "Punishments.Ban" })]
public void BanCommand(Context context, RunnerPlayer target, string? reason = "No reason provided.")
{
if (!(context.Source is ChatSource))
return;
ChatSource source = (ChatSource)context.Source;
var punishment = new Punishment(target.SteamID.ToString(), PunishmentType.Ban, -1, source.Invoker.SteamID.ToString(), reason);
punishment.Punish();
string banMessage = new PlaceholderLib(Configuration.BanMessage)
.AddParam("ServerName", Configuration.ServerName)
.AddParam("ExpireTime", "Never")
.AddParam("Reason", reason)
.AddParam("AppealMessage", Configuration.AppealMessage)
.Build();
target.Kick(banMessage);
}
[CommandCallback("tempban", Description = "Ban a player temporarily.", Permissions = new[] { "Punishments.TempBan" })]
public void TempBanCommand(Context context, RunnerPlayer target, string timestring, string? reason = "No reason provided.")
{
if (!(context.Source is ChatSource))
return;
ChatSource source = (ChatSource)context.Source;
var seconds = PunishmentsUtils.GetTimestringAsSeconds(timestring);
var punishment = new Punishment(target.SteamID.ToString(), PunishmentType.Ban, seconds, source.Invoker.SteamID.ToString(), reason);
punishment.Punish();
var expireTime = PunishmentsUtils.GetSecondsAsTimeUnit(seconds);
string banMessage = new PlaceholderLib(Configuration.BanMessage)
.AddParam("ServerName", Configuration.ServerName)
.AddParam("ExpireTime", expireTime)
.AddParam("Reason", reason)
.AddParam("AppealMessage", Configuration.AppealMessage)
.Build();
target.Kick(banMessage);
}
[CommandCallback("banid", Description = "Ban a SteamID permanently.", ConsoleCommand = true, Permissions = new[] { "Punishments.BanId" })]
public void BanIdCommand(Context context, string targetId, string? reason = "No reason provided.")
{
string admin = context.Source is ChatSource ? (context.Source as ChatSource)!.Invoker.SteamID.ToString() : "Server";
if (!targetId.StartsWith("756511"))
return;
var punishment = new Punishment(targetId.ToString(), PunishmentType.Ban, -1, admin, reason);
punishment.Punish();
string banMessage = new PlaceholderLib(Configuration.BanMessage)
.AddParam("ServerName", Configuration.ServerName)
.AddParam("ExpireTime", "Never")
.AddParam("Reason", reason)
.AddParam("AppealMessage", Configuration.AppealMessage)
.Build();
}
[CommandCallback("tempbanid", Description = "Ban a SteamID temporarily.", ConsoleCommand = true, Permissions = new[] { "Punishments.TempBanId" })]
public void TempBanIdCommand(Context context, string targetId, string timestring, string? reason = "No reason provided.")
{
string admin = context.Source is ChatSource ? (context.Source as ChatSource)!.Invoker.SteamID.ToString() : "Server";
var seconds = PunishmentsUtils.GetTimestringAsSeconds(timestring);
var punishment = new Punishment(targetId.ToString(), PunishmentType.Ban, seconds, admin, reason);
punishment.Punish();
var expireTime = PunishmentsUtils.GetSecondsAsTimeUnit(seconds);
string banMessage = new PlaceholderLib(Configuration.BanMessage)
.AddParam("ServerName", Configuration.ServerName)
.AddParam("ExpireTime", expireTime)
.AddParam("Reason", reason)
.AddParam("AppealMessage", Configuration.AppealMessage)
.Build();
}
#endregion
}
public class PunishmentsConfiguration : ModuleConfiguration
{
public string ServerName { get; set; } = "YourServerName";
public string AppealMessage { get; set; } = "You may appeal at some.link.com!";
public string BanMessage { get; set; } = "You were banned from {#ffaaaa ServerName /}!\n\nExpires in: {#ffaaaa ExpireTime /}\nReason: {#ffaaaa Reason /}\n\n{#ffaaaa AppealMessage /}";
public string MuteMessage { get; set; } = "You were muted!\nExpires: {#ffaaaa ExpireTime /}\nReason: {#ffaaaa Reason /}\n{ffaaaa AppealMessage /}";
}
public class Punishment
{
public string SteamId { get; set; }
public PunishmentType PunishmentType { get; set; }
public long PunishedDate { get; set; }
public long PunishedFor { get; set; }
public string AdminId { get; set; }
public string Reason { get; set; }
public Punishment(string steamId)
{
SteamId = steamId;
}
public Punishment(string steamId, PunishmentType punishmentType, long punishedFor, string adminId, string reason)
{
SteamId = steamId;
PunishmentType = punishmentType;
PunishedDate = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
PunishedFor = punishedFor;
AdminId = adminId;
Reason = reason;
}
public async Task PunishAsync()
{
var insert = Punishments.Connection.CreateCommand();
insert.CommandText = @"INSERT OR IGNORE INTO punishments (steamId, punishment, punishedAt, punishedFor, punishedBy, reason) VALUES ($steamId, $punishment, $punishedAt, $punishedFor, $punishedBy, $reason)";
insert.Parameters.AddWithValue("$steamId", SteamId);
insert.Parameters.AddWithValue("$punishment", PunishmentType);
insert.Parameters.AddWithValue("$punishedAt", PunishedDate);
insert.Parameters.AddWithValue("$punishedFor", PunishedFor);
insert.Parameters.AddWithValue("$punishedBy", AdminId);
insert.Parameters.AddWithValue("$reason", Reason);
await insert.ExecuteNonQueryAsync();
}
public void Punish()
{
var insert = Punishments.Connection.CreateCommand();
insert.CommandText = @"INSERT OR IGNORE INTO punishments (steamId, punishment, punishedAt, punishedFor, punishedBy, reason) VALUES ($steamId, $punishment, $punishedAt, $punishedFor, $punishedBy, $reason)";
insert.Parameters.AddWithValue("$steamId", SteamId);
insert.Parameters.AddWithValue("$punishment", PunishmentType);
insert.Parameters.AddWithValue("$punishedAt", PunishedDate);
insert.Parameters.AddWithValue("$punishedFor", PunishedFor);
insert.Parameters.AddWithValue("$punishedBy", AdminId);
insert.Parameters.AddWithValue("$reason", Reason);
insert.ExecuteNonQuery();
}
public bool IsPunished(PunishmentType? punishmentType = null)
{
var get = Punishments.Connection.CreateCommand();
get.CommandText = punishmentType != null ? "SELECT * FROM punishments WHERE steamId = $steamId AND punishment = $punishment LIMIT 1" : "SELECT * FROM punishments WHERE steamId = $steamId LIMIT 1";
get.Parameters.AddWithValue("$steamId", SteamId);
get.Parameters.AddWithValue("$punishment", punishmentType);
using (SqliteDataReader reader = get.ExecuteReader())
{
if (!reader.HasRows)
return false;
long current = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
long punishedAt = reader.GetInt64(2);
long punishedFor = reader.GetInt64(3);
return punishedFor == -1 || current - (punishedAt + punishedFor) < 0;
}
}
public async Task<bool> IsPunishedAsync(PunishmentType? punishmentType = null)
{
var get = Punishments.Connection.CreateCommand();
get.CommandText = punishmentType != null ? "SELECT * FROM punishments WHERE steamId = $steamId AND punishment = $punishment LIMIT 1" : "SELECT * FROM punishments WHERE steamId = $steamId LIMIT 1";
get.Parameters.AddWithValue("$steamId", SteamId);
get.Parameters.AddWithValue("$punishment", punishmentType);
using (SqliteDataReader reader = await get.ExecuteReaderAsync())
{
if (!reader.HasRows)
return false;
long current = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
long punishedAt = reader.GetInt64(2);
long punishedFor = reader.GetInt64(3);
return punishedFor == -1 || current - (punishedAt + punishedFor) < 0;
}
}
public void UnpunishIfReady(PunishmentType? punishmentType = null)
{
var get = Punishments.Connection.CreateCommand();
get.CommandText = punishmentType != null ? "SELECT * FROM punishments WHERE steamId = $steamId AND punishment = $punishment LIMIT 1" : "SELECT * FROM punishments WHERE steamId = $steamId LIMIT 1";
get.Parameters.AddWithValue("$steamId", SteamId);
get.Parameters.AddWithValue("$punishment", punishmentType);
using (SqliteDataReader reader = get.ExecuteReader())
{
if (!reader.HasRows)
return;
long current = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
long punishedAt = reader.GetInt64(2);
long punishedFor = reader.GetInt64(3);
if (punishedFor != -1 && current - (punishedAt + punishedFor) < 0)
{
var remove = Punishments.Connection.CreateCommand();
remove.CommandText = "DELETE * FROM punishments WHERE steamId=$steamId";
remove.Parameters.AddWithValue("$steamId", SteamId);
remove.ExecuteNonQuery();
}
}
}
public async Task UnpunishIfReadyAsync(PunishmentType? punishmentType = null)
{
var get = Punishments.Connection.CreateCommand();
get.CommandText = punishmentType != null ? "SELECT * FROM punishments WHERE steamId = $steamId AND punishment = $punishment LIMIT 1" : "SELECT * FROM punishments WHERE steamId = $steamId LIMIT 1";
get.Parameters.AddWithValue("$steamId", SteamId);
get.Parameters.AddWithValue("$punishment", punishmentType);
using (SqliteDataReader reader = await get.ExecuteReaderAsync())
{
if (!reader.HasRows)
return;
long current = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
long punishedAt = reader.GetInt64(2);
long punishedFor = reader.GetInt64(3);
if (punishedFor != -1 && current - (punishedAt + punishedFor) < 0)
{
var remove = Punishments.Connection.CreateCommand();
remove.CommandText = "DELETE * FROM punishments WHERE steamId=$steamId";
remove.Parameters.AddWithValue("$steamId", SteamId);
await remove.ExecuteNonQueryAsync();
}
}
}
public void Unpunish(PunishmentType? punishmentType = null)
{
var remove = Punishments.Connection.CreateCommand();
remove.CommandText = punishmentType == null ? "DELETE * FROM punishments WHERE steamId=$steamId" : "DELETE * FROM punishments WHERE steamId=$steamId AND punishment=$punishment";
remove.Parameters.AddWithValue("$steamId", SteamId);
remove.Parameters.AddWithValue("$punishment", PunishmentType);
remove.ExecuteNonQuery();
}
public async Task UnpunishAsync(PunishmentType? punishmentType = null)
{
var remove = Punishments.Connection.CreateCommand();
remove.CommandText = punishmentType == null ? "DELETE * FROM punishments WHERE steamId=$steamId" : "DELETE * FROM punishments WHERE steamId=$steamId AND punishment=$punishment";
remove.Parameters.AddWithValue("$steamId", SteamId);
remove.Parameters.AddWithValue("$punishment", PunishmentType);
await remove.ExecuteNonQueryAsync();
}
}
public enum PunishmentType
{
Ban,
Mute
}
public class PunishmentsUtils
{
static Dictionary<string, int> lettersToSeconds = new Dictionary<string, int> {
{ "y", 31556952 },
{ "mo", 2629746 },
{ "w", 604800 },
{ "d", 86400 },
{ "h", 3600 },
{ "m", 60 }
};
static Dictionary<string, string> lettersToUnit = new Dictionary<string, string> {
{ "y", "year" },
{ "mo", "month" },
{ "w", "week" },
{ "d", "day" },
{ "h", "hour" },
{ "m", "minute" }
};
public static string GetTimestringAsUnitResult(string timestring)
{
string unitResult = "";
bool isNumber = int.TryParse(timestring, out int secondsTime);
bool valid = true;
if (isNumber)
return secondsTime + " seconds";
MatchCollection collection = Regex.Matches(timestring, "(\\d+)([A-Za-z]+)");
int i = 0;
foreach (Match match in collection)
{
valid = int.TryParse(match.Groups[1].Value, out int num);
string time = match.Groups[2].Value;
if (!lettersToUnit.ContainsKey(time.ToLower()))
valid = false;
if (!valid)
return "";
bool result = lettersToUnit.TryGetValue(time, out string unit);
if (result)
{
if (num != 1)
unit += "s";
string beginning = "";
string end = "";
if (collection.Count > i + 1)
{
end += " ";
}
else
{
beginning = "and ";
}
unitResult += beginning + num + " " + unit + end;
}
i++;
}
return unitResult;
}
public static int GetTimestringAsSeconds(string timestring)
{
bool valid = false;
int total = 0;
bool isNumber = int.TryParse(timestring, out int secondsTime);
if (isNumber)
return secondsTime;
foreach (Match match in Regex.Matches(timestring, "(\\d+)([A-Za-z]+)"))
{
valid = int.TryParse(match.Groups[1].Value, out int num);
string time = match.Groups[2].Value;
if (!lettersToSeconds.ContainsKey(time.ToLower()))
valid = false;
if (!valid)
return -1;
lettersToSeconds.TryGetValue(time, out int seconds);
total += seconds * num;
}
return total;
}
public static string GetSecondsAsTimeUnit(int seconds)
{
string timeunit = "Never";
if (seconds != -1)
{
timeunit = "";
int remaining = seconds;
int quantity = 0;
for (int i = 0; i < lettersToSeconds.Count; i++)
{
KeyValuePair<string, int> pair = lettersToSeconds.ElementAt(i);
if (remaining == 0)
break;
if (pair.Value > remaining)
continue;
if (remaining / pair.Value >= 1)
{
quantity = (int)Math.Floor((decimal)remaining / pair.Value);
remaining -= quantity * pair.Value;
}
timeunit += $"{quantity}{pair.Key}";
}
}
return GetTimestringAsUnitResult(timeunit);
}
}
}