This repository has been archived by the owner on Jul 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiscord.sp
346 lines (266 loc) · 9.57 KB
/
discord.sp
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
#include <sourcemod>
#include <dynamic>
#include <SteamWorks>
#pragma semicolon 1
ConVar hostname = null;
char gS_WebhookURL[1024];
new String:g_szMessage[1024];
new String:g_szMessageTime[1024];
public Plugin myinfo =
{
name = "[INS] Discord",
author = "Neko- (shavit)",
description = "Relays in-game chat into a Discord channel.",
version = "1.0.5"
}
public void OnPluginStart()
{
RegConsoleCmd("calladmin", Cmd_CallAdmin, "Call admin from discord");
hostname = FindConVar("hostname");
char[] sError = new char[256];
if(!LoadConfig(sError, 256))
{
SetFailState("Couldn't load the configuration file. Error: %s", sError);
}
AddCommandListener(Say_Event, "say");
AddCommandListener(SayTeam_Event, "say_team");
AddCommandListener(CallVote_Event, "callvote");
AddCommandListener(Vote_Event, "vote");
}
bool LoadConfig(char[] error, int maxlen)
{
char[] sPath = new char[PLATFORM_MAX_PATH];
BuildPath(Path_SM, sPath, PLATFORM_MAX_PATH, "configs/discord.cfg");
Dynamic dConfigFile = Dynamic();
if(!dConfigFile.ReadKeyValues(sPath))
{
dConfigFile.Dispose();
FormatEx(error, maxlen, "Couldn't access \"%s\". Make sure that the file exists and has correct permissions set.", sPath);
return false;
}
dConfigFile.GetString("WebhookURL", gS_WebhookURL, 1024);
if(StrContains(gS_WebhookURL, "https://discordapp.com/api/webhooks") == -1)
{
FormatEx(error, maxlen, "Please change the value of WebhookURL in the configuration file (\"%s\") to a valid URL. Current value is \"%s\".", sPath, gS_WebhookURL);
return false;
}
return true;
}
void EscapeString(char[] string, int maxlen)
{
ReplaceString(string, maxlen, "@", "@");
ReplaceString(string, maxlen, "'", "\'");
ReplaceString(string, maxlen, "\"", """);
}
void EscapeStringAllowAt(char[] string, int maxlen)
{
ReplaceString(string, maxlen, "'", "\'");
ReplaceString(string, maxlen, "\"", """);
}
public Action:Say_Event(client, const String:cmd[], argc)
{
if(!IsValidClient(client))
{
return Plugin_Continue;
}
decl String:strMsg[255];
GetCmdArgString(strMsg, 255);
StripQuotes(strMsg);
decl String:sAuthID[64];
GetClientAuthId(client, AuthId_Engine, sAuthID, 64);
decl String:strName[64];
GetClientName(client, strName, sizeof(strName));
DiscordMessage(sAuthID, strName, strMsg);
return Plugin_Continue;
}
public Action:SayTeam_Event(client, const String:cmd[], argc)
{
if(!IsValidClient(client))
{
return Plugin_Continue;
}
decl String:strMsg[255];
GetCmdArgString(strMsg, 255);
StripQuotes(strMsg);
decl String:sAuthID[64];
GetClientAuthId(client, AuthId_Engine, sAuthID, 64);
decl String:strName[64];
GetClientName(client, strName, sizeof(strName));
DiscordMessage(sAuthID, strName, strMsg, true);
return Plugin_Continue;
}
DiscordMessage(const String:strAuthID[], const String:strName[], const String:strMessage[], bool bTeamChat = false)
{
char[] sHostname = new char[32];
hostname.GetString(sHostname, 32);
EscapeString(sHostname, 32);
char[] sFormat = new char[1024];
FormatEx(sFormat, 1024, "{\"username\":\"%s\", \"content\":\"{msg}\"}", "In-Game Chat");
char[] sTime = new char[10];
FormatTime(sTime, 10, "%H:%I:%S");
char[] sNewMessage = new char[1024];
if(bTeamChat)
{
FormatEx(sNewMessage, 1024, "**[%s] (TEAM) %s :** %s", strAuthID, strName, strMessage);
}
else
{
FormatEx(sNewMessage, 1024, "**[%s] %s :** %s", strAuthID, strName, strMessage);
}
if(StrContains(sNewMessage, "@admin") > -1)
{
ReplaceString(sNewMessage, 1024, "@admin", "<@&313894579680051200>");
}
EscapeStringAllowAt(sNewMessage, 1024);
//EscapeString(sNewMessage, 1024);
ReplaceString(sFormat, 1024, "{msg}", sNewMessage);
if((!StrEqual(g_szMessageTime, sTime, false)) || (!StrEqual(g_szMessage, sNewMessage, false)))
{
Format(g_szMessageTime, sizeof(g_szMessageTime), "%s", sTime);
Format(g_szMessage, sizeof(g_szMessage), "%s", sNewMessage);
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, gS_WebhookURL);
SteamWorks_SetHTTPRequestRawPostBody(hRequest, "application/json", sFormat, strlen(sFormat));
SteamWorks_SetHTTPCallbacks(hRequest, view_as<SteamWorksHTTPRequestCompleted>(OnRequestComplete));
SteamWorks_SendHTTPRequest(hRequest);
}
}
public void OnRequestComplete(Handle hRequest, bool bFailed, bool bRequestSuccessful, EHTTPStatusCode eStatusCode)
{
delete hRequest;
}
public Action:Cmd_CallAdmin(client, args)
{
if (args < 1)
{
ReplyToCommand(client, "Usage: calladmin <reason>");
return Plugin_Handled;
}
char szReason[500];
GetCmdArgString(szReason, sizeof(szReason));
decl String:sAuthID[64];
GetClientAuthId(client, AuthId_Engine, sAuthID, 64);
decl String:strName[64];
GetClientName(client, strName, sizeof(strName));
EscapeString(strName, 64);
char[] sFormat = new char[1024];
FormatEx(sFormat, 1024, "{\"username\":\"%s\", \"content\":\"{msg}\"}", "In-Game Admin Notification");
char[] sNewMessage = new char[1024];
FormatEx(sNewMessage, 1024, "**[%s] %s** is calling for <@&313894579680051200>```%s```", sAuthID, strName, szReason);
EscapeStringAllowAt(sNewMessage, 1024);
ReplaceString(sFormat, 1024, "{msg}", sNewMessage);
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, gS_WebhookURL);
SteamWorks_SetHTTPRequestRawPostBody(hRequest, "application/json", sFormat, strlen(sFormat));
SteamWorks_SetHTTPCallbacks(hRequest, view_as<SteamWorksHTTPRequestCompleted>(OnRequestComplete));
SteamWorks_SendHTTPRequest(hRequest);
PrintToChat(client, "\x0759b0f9[INS] \x01Please wait for the next available admin to reach you...");
return Plugin_Handled;
}
public Action:CallVote_Event(client, const String:cmd[], argc)
{
if(!IsValidClient(client))
{
return Plugin_Continue;
}
decl String:strAuthID[64];
GetClientAuthId(client, AuthId_Engine, strAuthID, 64);
decl String:strName[64];
GetClientName(client, strName, sizeof(strName));
if(argc == 1)
{
decl String:Cmd1[64];
GetCmdArg(1, Cmd1, sizeof(Cmd1));
char[] sNewMessage = new char[1024];
FormatEx(sNewMessage, 1024, "**[%s] %s** started a **%s** vote", strAuthID, strName, Cmd1);
char[] sFormat = new char[1024];
FormatEx(sFormat, 1024, "{\"username\":\"%s\", \"content\":\"{msg}\"}", "In-Game Vote System");
ReplaceString(sFormat, 1024, "{msg}", sNewMessage);
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, gS_WebhookURL);
SteamWorks_SetHTTPRequestRawPostBody(hRequest, "application/json", sFormat, strlen(sFormat));
SteamWorks_SetHTTPCallbacks(hRequest, view_as<SteamWorksHTTPRequestCompleted>(OnRequestComplete));
SteamWorks_SendHTTPRequest(hRequest);
}
else if(argc == 2)
{
decl String:Cmd1[64];
decl String:Cmd2[64];
GetCmdArg(1, Cmd1, sizeof(Cmd1));
GetCmdArg(2, Cmd2, sizeof(Cmd2));
char[] sNewMessage = new char[1024];
if(StrContains(Cmd2, " ") > -1)
{
decl String:strCmd2Info[4][64];
ExplodeString(Cmd2, " ", strCmd2Info, sizeof(strCmd2Info), sizeof(strCmd2Info[]));
if(GetClientOfUserId(StringToInt(strCmd2Info[0])) != 0)
{
int nTarget = GetClientOfUserId(StringToInt(strCmd2Info[0]));
decl String:sAuthIDTarget[64];
GetClientAuthId(nTarget, AuthId_Engine, sAuthIDTarget, 64);
decl String:strNameTarget[64];
GetClientName(nTarget, strNameTarget, sizeof(strNameTarget));
FormatEx(sNewMessage, 1024, "**[%s] %s** started a **%s** vote on **[%s] %s** with reason **%s**", strAuthID, strName, Cmd1, sAuthIDTarget, strNameTarget, strCmd2Info[1]);
}
else
{
FormatEx(sNewMessage, 1024, "**[%s] %s** started a **%s** vote, **%s %s**", strAuthID, strName, Cmd1, strCmd2Info[0], strCmd2Info[1]);
}
}
else
{
FormatEx(sNewMessage, 1024, "**[%s] %s** started a **%s** vote, **%s**", strAuthID, strName, Cmd1, Cmd2);
}
char[] sFormat = new char[1024];
FormatEx(sFormat, 1024, "{\"username\":\"%s\", \"content\":\"{msg}\"}", "In-Game Vote System");
ReplaceString(sFormat, 1024, "{msg}", sNewMessage);
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, gS_WebhookURL);
SteamWorks_SetHTTPRequestRawPostBody(hRequest, "application/json", sFormat, strlen(sFormat));
SteamWorks_SetHTTPCallbacks(hRequest, view_as<SteamWorksHTTPRequestCompleted>(OnRequestComplete));
SteamWorks_SendHTTPRequest(hRequest);
}
return Plugin_Continue;
}
public Action:Vote_Event(client, const String:cmd[], argc)
{
if(!IsValidClient(client))
{
return Plugin_Continue;
}
if(argc != 1)
{
return Plugin_Continue;
}
decl String:Cmd1[64];
GetCmdArg(1, Cmd1, sizeof(Cmd1));
decl String:strAuthID[64];
GetClientAuthId(client, AuthId_Engine, strAuthID, 64);
decl String:strName[64];
GetClientName(client, strName, sizeof(strName));
char[] szOption = new char[32];
if(StrContains(Cmd1, "option1") > -1)
{
FormatEx(szOption, 32, "Yes");
}
else if(StrContains(Cmd1, "option2") > -1)
{
FormatEx(szOption, 32, "No");
}
else
{
return Plugin_Continue;
}
char[] sNewMessage = new char[1024];
FormatEx(sNewMessage, 1024, "**[%s] %s** voted **%s**", strAuthID, strName, szOption);
char[] sFormat = new char[1024];
FormatEx(sFormat, 1024, "{\"username\":\"%s\", \"content\":\"{msg}\"}", "In-Game Vote System");
ReplaceString(sFormat, 1024, "{msg}", sNewMessage);
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, gS_WebhookURL);
SteamWorks_SetHTTPRequestRawPostBody(hRequest, "application/json", sFormat, strlen(sFormat));
SteamWorks_SetHTTPCallbacks(hRequest, view_as<SteamWorksHTTPRequestCompleted>(OnRequestComplete));
SteamWorks_SendHTTPRequest(hRequest);
return Plugin_Continue;
}
bool:IsValidClient(client)
{
if ( !( 1 <= client <= MaxClients ) || !IsClientInGame(client) )
return false;
return true;
}