Repository navigation
Expand file tree
/
Copy pathReportSystem.cs
More file actions
240 lines (200 loc) · 8.87 KB
/
Copy pathReportSystem.cs
File metadata and controls
240 lines (200 loc) · 8.87 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
using System.Text;
using System.Text.Json;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Menu;
namespace ReportSystem;
public class ReportSystem : BasePlugin
{
public override string ModuleAuthor => "phantom";
public override string ModuleName => "[Discord] ReportSystem";
public override string ModuleVersion => "v1.0.1";
private Config _config = null!;
private PersonTargetData[] _selectedReason = new PersonTargetData[65];
public override void Load(bool hotReload)
{
_config = LoadConfig();
var mapsFilePath = Path.Combine(ModuleDirectory, "reasons.txt");
if (!File.Exists(mapsFilePath))
File.WriteAllText(mapsFilePath, "");
RegisterListener<Listeners.OnClientConnected>(slot => _selectedReason[slot + 1] = new PersonTargetData{Target = -1, IsSelectedReason = false});
RegisterListener<Listeners.OnClientDisconnectPost>(slot => _selectedReason[slot + 1] = null);
AddCommand("css_report", "", (controller, info) =>
{
if(controller == null) return;
var reportMenu = new ChatMenu("Report");
reportMenu.MenuOptions.Clear();
foreach (var player in Utilities.GetPlayers())
{
if(player.IsBot || player == controller) continue;
reportMenu.AddMenuOption($"{player.PlayerName} [{player.Index}]", HandleMenu);
}
ChatMenus.OpenMenu(controller, reportMenu);
});
AddCommandListener("say", Listener_Say);
AddCommandListener("say_team", Listener_Say);
}
private HookResult Listener_Say(CCSPlayerController? player, CommandInfo commandinfo)
{
if (player == null) return HookResult.Continue;
if (_selectedReason[player.Index] != null && _selectedReason[player.Index]!.IsSelectedReason)
{
var msg = GetTextInsideQuotes(commandinfo.ArgString);
var target = Utilities.GetPlayerFromIndex(_selectedReason[player.Index]!.Target);
switch (msg)
{
case "cancel":
_selectedReason[player.Index]!.IsSelectedReason = false;
return HookResult.Handled;
default:
Task.Run(() => SendMessageToDiscord(player.PlayerName, new SteamID(player.SteamID).SteamId2, target.PlayerName,
new SteamID(target.SteamID).SteamId2, commandinfo.ArgString, $"{ConVar.Find("hostname")!.StringValue}\n{ConVar.Find("ip")!.StringValue}:{ConVar.Find("hostport")!.GetPrimitiveValue<int>()}"));
_selectedReason[player.Index]!.IsSelectedReason = false;
player.PrintToChat($"[\x0C ReportSystem\x01 ] You have successfully filed a report on player `{target.PlayerName}`");
return HookResult.Handled;
}
}
return HookResult.Continue;
}
private void HandleMenu(CCSPlayerController controller, ChatMenuOption option)
{
var parts = option.Text.Split('[', ']');
var lastPart = parts[^2];
var numbersOnly = string.Join("", lastPart.Where(char.IsDigit));
var index = int.Parse(numbersOnly.Trim());
var reason = File.ReadAllLines(Path.Combine(ModuleDirectory, "reasons.txt"));
var reasonMenu = new ChatMenu("Reasons");
reasonMenu.MenuOptions.Clear();
reasonMenu.AddMenuOption($"My reason [{index}]", (playerController, menuOption) =>
{
if (_selectedReason[playerController.Index] == null) return;
_selectedReason[playerController.Index]!.IsSelectedReason = true;
_selectedReason[playerController.Index]!.Target = index;
playerController.PrintToChat($"[\x0C ReportSystem\x01 ] Write a reason, or write\x02 cancel\x01 to abort the submission.");
});
foreach (var a in reason)
{
reasonMenu.AddMenuOption($"{a} [{index}]", HandleMenu2);
}
ChatMenus.OpenMenu(controller, reasonMenu);
}
private void HandleMenu2(CCSPlayerController controller, ChatMenuOption option)
{
var parts = option.Text.Split('[', ']');
var lastPart = parts[^2];
var numbersOnly = string.Join("", lastPart.Where(char.IsDigit));
var target = Utilities.GetPlayerFromIndex(int.Parse(numbersOnly.Trim()));
var playerName = controller.PlayerName;
var playerSid = controller.SteamID.ToString();
var targetName = target.PlayerName;
var targetSid = target.SteamID.ToString();
var ip = target.SteamID.ToString();
Task.Run(() => SendMessageToDiscord(playerName, playerSid, targetName,
targetSid, parts[0], ip));
controller.PrintToChat($"[\x0C ReportSystem\x01 ] You have successfully filed a report on player `{target.PlayerName}`");
}
private async void SendMessageToDiscord(string clientName, string clientSteamId, string targetName,
string targetSteamId, string msg, string ip)
{
try
{
var webhookUrl = GetWebhook();
if (string.IsNullOrEmpty(webhookUrl)) return;
var httpClient = new HttpClient();
if (msg == "") return;
var payload = new
{
embeds = new[]
{
new
{
title = "Report",
description = ip,
color = 16711680, //255204255
fields = new[]
{
new
{
name = "Dispatcher",
value =
$"**Name:** {clientName}\n**SteamID:** {clientSteamId}\n**Link:** [steam account link](https://steamcommunity.com/profiles/{clientSteamId}/)",
inline = false
},
new
{
name = "Victim",
value =
$"**Name:** {targetName}\n**SteamID:** {targetSteamId}\n**Link:** [steam account link](https://steamcommunity.com/profiles/{targetSteamId}/)",
inline = false
},
new
{
name = "Reason",
value = msg,
inline = false
}
}
}
}
};
var json = JsonSerializer.Serialize(payload);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(webhookUrl, content);
Console.ForegroundColor = response.IsSuccessStatusCode ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine(response.IsSuccessStatusCode
? "Success"
: $"Error: {response.StatusCode}");
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
private Config LoadConfig()
{
var configPath = Path.Combine(ModuleDirectory, "settings.json");
if (!File.Exists(configPath)) return CreateConfig(configPath);
var config = JsonSerializer.Deserialize<Config>(File.ReadAllText(configPath))!;
return config;
}
private Config CreateConfig(string configPath)
{
var config = new Config
{
WebhookUrl = ""
};
File.WriteAllText(configPath,
JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }));
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("[ReportSystem] The configuration was successfully saved to a file: " + configPath);
Console.ResetColor();
return config;
}
private string GetWebhook()
{
return _config.WebhookUrl;
}
private string GetTextInsideQuotes(string input)
{
var startIndex = input.IndexOf('"');
var endIndex = input.LastIndexOf('"');
if (startIndex != -1 && endIndex != -1 && startIndex < endIndex)
{
return input.Substring(startIndex + 1, endIndex - startIndex - 1);
}
return string.Empty;
}
}
public class Config
{
public required string WebhookUrl { get; set; }
}
public class PersonTargetData
{
public int Target { get; set; }
public bool IsSelectedReason { get; set; }
}