-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathdiscord_api.sp
313 lines (254 loc) · 9.7 KB
/
discord_api.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
#pragma semicolon 1
#define PLUGIN_VERSION "0.1.103"
#include <sourcemod>
#include <discord>
#include <SteamWorks>
#include <smjansson>
#include "discord/DiscordRequest.sp"
#include "discord/SendMessage.sp"
#include "discord/GetGuilds.sp"
#include "discord/GetGuildChannels.sp"
#include "discord/ListenToChannel.sp"
#include "discord/SendWebHook.sp"
#include "discord/reactions.sp"
#include "discord/UserObject.sp"
#include "discord/MessageObject.sp"
#include "discord/GuildMembers.sp"
#include "discord/GuildRole.sp"
#include "discord/deletemessage.sp"
//For rate limitation
Handle hRateLimit = null;
Handle hRateReset = null;
Handle hRateLeft = null;
public Plugin myinfo = {
name = "Discord API",
author = "Deathknife",
description = "",
version = PLUGIN_VERSION,
url = ""
};
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max) {
CreateNative("DiscordBot.GetToken", Native_DiscordBot_Token_Get);
//SendMessage.sp
CreateNative("DiscordBot.SendMessage", Native_DiscordBot_SendMessage);
CreateNative("DiscordBot.SendMessageToChannelID", Native_DiscordBot_SendMessageToChannel);
CreateNative("DiscordChannel.SendMessage", Native_DiscordChannel_SendMessage);
//deletemessage.sp
CreateNative("DiscordBot.DeleteMessageID", Native_DiscordBot_DeleteMessageID);
CreateNative("DiscordBot.DeleteMessage", Native_DiscordBot_DeleteMessage);
//ListenToChannel.sp
CreateNative("DiscordBot.StartTimer", Native_DiscordBot_StartTimer);
//GetGuilds.sp
CreateNative("DiscordBot.GetGuilds", Native_DiscordBot_GetGuilds);
//GetGuildChannels.sp
CreateNative("DiscordBot.GetGuildChannels", Native_DiscordBot_GetGuildChannels);
//GuildRole.sp
CreateNative("DiscordBot.GetGuildRoles", Native_DiscordBot_GetGuildRoles);
//reactions.sp
CreateNative("DiscordBot.AddReactionID", Native_DiscordBot_AddReaction);
CreateNative("DiscordBot.DeleteReactionID", Native_DiscordBot_DeleteReaction);
CreateNative("DiscordBot.GetReactionID", Native_DiscordBot_GetReaction);
//GuildMembers.sp
CreateNative("DiscordBot.GetGuildMembers", Native_DiscordBot_GetGuildMembers);
CreateNative("DiscordBot.GetGuildMembersAll", Native_DiscordBot_GetGuildMembersAll);
//CreateNative("DiscordChannel.Destroy", Native_DiscordChannel_Destroy);
//SendWebHook.sp
CreateNative("DiscordWebHook.Send", Native_DiscordWebHook_Send);
//CreateNative("DiscordWebHook.AddField", Native_DiscordWebHook_AddField);
//CreateNative("DiscordWebHook.DeleteFields", Native_DiscordWebHook_DeleteFields);
//UserObject.sp
CreateNative("DiscordUser.GetID", Native_DiscordUser_GetID);
CreateNative("DiscordUser.GetUsername", Native_DiscordUser_GetUsername);
CreateNative("DiscordUser.GetDiscriminator", Native_DiscordUser_GetDiscriminator);
CreateNative("DiscordUser.GetAvatar", Native_DiscordUser_GetAvatar);
CreateNative("DiscordUser.IsVerified", Native_DiscordUser_IsVerified);
CreateNative("DiscordUser.GetEmail", Native_DiscordUser_GetEmail);
CreateNative("DiscordUser.IsBot", Native_DiscordUser_IsBot);
//MessageObject.sp
CreateNative("DiscordMessage.GetID", Native_DiscordMessage_GetID);
CreateNative("DiscordMessage.IsPinned", Native_DiscordMessage_IsPinned);
CreateNative("DiscordMessage.GetAuthor", Native_DiscordMessage_GetAuthor);
CreateNative("DiscordMessage.GetContent", Native_DiscordMessage_GetContent);
CreateNative("DiscordMessage.GetChannelID", Native_DiscordMessage_GetChannelID);
RegPluginLibrary("discord-api");
return APLRes_Success;
}
public void OnPluginStart() {
hRateLeft = new StringMap();
hRateReset = new StringMap();
hRateLimit = new StringMap();
}
public int Native_DiscordBot_Token_Get(Handle plugin, int numParams) {
DiscordBot bot = GetNativeCell(1);
static char token[196];
JsonObjectGetString(bot, "token", token, sizeof(token));
SetNativeString(2, token, GetNativeCell(3));
}
stock void BuildAuthHeader(Handle request, DiscordBot Bot) {
static char buffer[256];
static char token[196];
JsonObjectGetString(Bot, "token", token, sizeof(token));
FormatEx(buffer, sizeof(buffer), "Bot %s", token);
SteamWorks_SetHTTPRequestHeaderValue(request, "Authorization", buffer);
}
stock Handle PrepareRequest(DiscordBot bot, char[] url, EHTTPMethod method=k_EHTTPMethodGET, Handle hJson=null, SteamWorksHTTPDataReceived DataReceived = INVALID_FUNCTION, SteamWorksHTTPRequestCompleted RequestCompleted = INVALID_FUNCTION) {
static char stringJson[16384];
stringJson[0] = '\0';
if(hJson != null) {
json_dump(hJson, stringJson, sizeof(stringJson), 0, true);
}
//Format url
static char turl[128];
FormatEx(turl, sizeof(turl), "https://discord.com/api/%s", url);
Handle request = SteamWorks_CreateHTTPRequest(method, turl);
if(request == null) {
return null;
}
if(bot != null) {
BuildAuthHeader(request, bot);
}
SteamWorks_SetHTTPRequestRawPostBody(request, "application/json; charset=UTF-8", stringJson, strlen(stringJson));
SteamWorks_SetHTTPRequestNetworkActivityTimeout(request, 30);
if(RequestCompleted == INVALID_FUNCTION) {
//I had some bugs previously where it wouldn't send request and return code 0 if I didn't set request completed.
//This is just a safety then, my issue could have been something else and I will test more later on
RequestCompleted = HTTPCompleted;
}
if(DataReceived == INVALID_FUNCTION) {
//Need to close the request handle
DataReceived = HTTPDataReceive;
}
SteamWorks_SetHTTPCallbacks(request, RequestCompleted, HeadersReceived, DataReceived);
if(hJson != null) delete hJson;
return request;
}
stock Handle PrepareRequestRaw(DiscordBot bot, char[] url, EHTTPMethod method=k_EHTTPMethodGET, Handle hJson=null, SteamWorksHTTPDataReceived DataReceived = INVALID_FUNCTION, SteamWorksHTTPRequestCompleted RequestCompleted = INVALID_FUNCTION) {
static char stringJson[16384];
stringJson[0] = '\0';
if(hJson != null) {
json_dump(hJson, stringJson, sizeof(stringJson), 0, true);
}
Handle request = SteamWorks_CreateHTTPRequest(method, url);
if(request == null) {
return null;
}
if(bot != null) {
BuildAuthHeader(request, bot);
}
SteamWorks_SetHTTPRequestRawPostBody(request, "application/json; charset=UTF-8", stringJson, strlen(stringJson));
SteamWorks_SetHTTPRequestNetworkActivityTimeout(request, 30);
if(RequestCompleted == INVALID_FUNCTION) {
//I had some bugs previously where it wouldn't send request and return code 0 if I didn't set request completed.
//This is just a safety then, my issue could have been something else and I will test more later on
RequestCompleted = HTTPCompleted;
}
if(DataReceived == INVALID_FUNCTION) {
//Need to close the request handle
DataReceived = HTTPDataReceive;
}
SteamWorks_SetHTTPCallbacks(request, RequestCompleted, HeadersReceived, DataReceived);
if(hJson != null) delete hJson;
return request;
}
public int HTTPCompleted(Handle request, bool failure, bool requestSuccessful, EHTTPStatusCode statuscode, any data, any data2) {
}
public int HTTPDataReceive(Handle request, bool failure, int offset, int statuscode, any dp) {
delete request;
}
public int HeadersReceived(Handle request, bool failure, any data, any datapack) {
DataPack dp = view_as<DataPack>(datapack);
if(failure) {
delete dp;
return;
}
char xRateLimit[16];
char xRateLeft[16];
char xRateReset[32];
bool exists = false;
exists = SteamWorks_GetHTTPResponseHeaderValue(request, "X-RateLimit-Limit", xRateLimit, sizeof(xRateLimit));
exists = SteamWorks_GetHTTPResponseHeaderValue(request, "X-RateLimit-Remaining", xRateLeft, sizeof(xRateLeft));
exists = SteamWorks_GetHTTPResponseHeaderValue(request, "X-RateLimit-Reset", xRateReset, sizeof(xRateReset));
//Get url
char route[128];
ResetPack(dp);
ReadPackString(dp, route, sizeof(route));
delete dp;
int reset = StringToInt(xRateReset);
if(reset > GetTime() + 3) {
reset = GetTime() + 3;
}
if(exists) {
SetTrieValue(hRateReset, route, reset);
SetTrieValue(hRateLeft, route, StringToInt(xRateLeft));
SetTrieValue(hRateLimit, route, StringToInt(xRateLimit));
}else {
SetTrieValue(hRateReset, route, -1);
SetTrieValue(hRateLeft, route, -1);
SetTrieValue(hRateLimit, route, -1);
}
}
/*
This is rate limit imposing for per-route basis. Doesn't support global limit yet.
*/
public void DiscordSendRequest(Handle request, const char[] route) {
//Check for reset
int time = GetTime();
int resetTime;
int defLimit = 0;
if(!GetTrieValue(hRateLimit, route, defLimit)) {
defLimit = 1;
}
bool exists = GetTrieValue(hRateReset, route, resetTime);
if(!exists) {
SetTrieValue(hRateReset, route, GetTime() + 5);
SetTrieValue(hRateLeft, route, defLimit - 1);
SteamWorks_SendHTTPRequest(request);
return;
}
if(time == -1) {
//No x-rate-limit send
SteamWorks_SendHTTPRequest(request);
return;
}
if(time > resetTime) {
SetTrieValue(hRateLeft, route, defLimit - 1);
SteamWorks_SendHTTPRequest(request);
return;
}else {
int left;
GetTrieValue(hRateLeft, route, left);
if(left == 0) {
float remaining = float(resetTime) - float(time) + 1.0;
Handle dp = new DataPack();
WritePackCell(dp, request);
WritePackString(dp, route);
CreateTimer(remaining, SendRequestAgain, dp);
}else {
left--;
SetTrieValue(hRateLeft, route, left);
SteamWorks_SendHTTPRequest(request);
}
}
}
public Handle UrlToDP(char[] url) {
DataPack dp = new DataPack();
WritePackString(dp, url);
return dp;
}
public Action SendRequestAgain(Handle timer, any dp) {
ResetPack(dp);
Handle request = ReadPackCell(dp);
char route[128];
ReadPackString(dp, route, sizeof(route));
delete view_as<Handle>(dp);
DiscordSendRequest(request, route);
}
stock bool RenameJsonObject(Handle hJson, char[] key, char[] toKey) {
Handle hObject = json_object_get(hJson, key);
if(hObject != null) {
json_object_set_new(hJson, toKey, hObject);
json_object_del(hJson, key);
return true;
}
return false;
}