Initial commit
This commit is contained in:
94
scripting/discord/DiscordRequest.sp
Normal file
94
scripting/discord/DiscordRequest.sp
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
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://discordapp.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;
|
||||
}
|
||||
*/
|
||||
|
||||
methodmap DiscordRequest < Handle {
|
||||
public DiscordRequest(char[] url, EHTTPMethod method) {
|
||||
Handle request = SteamWorks_CreateHTTPRequest(method, url);
|
||||
return view_as<DiscordRequest>(request);
|
||||
}
|
||||
|
||||
public void SetJsonBody(Handle hJson) {
|
||||
static char stringJson[16384];
|
||||
stringJson[0] = '\0';
|
||||
if(hJson != null) {
|
||||
json_dump(hJson, stringJson, sizeof(stringJson), 0, true);
|
||||
}
|
||||
SteamWorks_SetHTTPRequestRawPostBody(this, "application/json; charset=UTF-8", stringJson, strlen(stringJson));
|
||||
if(hJson != null) delete hJson;
|
||||
}
|
||||
|
||||
public void SetJsonBodyEx(Handle hJson) {
|
||||
static char stringJson[16384];
|
||||
stringJson[0] = '\0';
|
||||
if(hJson != null) {
|
||||
json_dump(hJson, stringJson, sizeof(stringJson), 0, true);
|
||||
}
|
||||
SteamWorks_SetHTTPRequestRawPostBody(this, "application/json; charset=UTF-8", stringJson, strlen(stringJson));
|
||||
}
|
||||
|
||||
property int Timeout {
|
||||
public set(int timeout) {
|
||||
SteamWorks_SetHTTPRequestNetworkActivityTimeout(this, timeout);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCallbacks(SteamWorksHTTPRequestCompleted OnComplete, SteamWorksHTTPDataReceived DataReceived) {
|
||||
SteamWorks_SetHTTPCallbacks(this, OnComplete, HeadersReceived, DataReceived);
|
||||
}
|
||||
|
||||
public void SetContextValue(any data1, any data2) {
|
||||
SteamWorks_SetHTTPRequestContextValue(this, data1, data2);
|
||||
}
|
||||
|
||||
public void SetData(any data1, char[] route) {
|
||||
SteamWorks_SetHTTPRequestContextValue(this, data1, UrlToDP(route));
|
||||
}
|
||||
|
||||
public void SetBot(DiscordBot bot) {
|
||||
BuildAuthHeader(this, bot);
|
||||
}
|
||||
|
||||
public void Send(char[] route) {
|
||||
DiscordSendRequest(this, route);
|
||||
}
|
||||
}
|
152
scripting/discord/GetGuildChannels.sp
Normal file
152
scripting/discord/GetGuildChannels.sp
Normal file
@@ -0,0 +1,152 @@
|
||||
public int Native_DiscordBot_GetGuildChannels(Handle plugin, int numParams) {
|
||||
DiscordBot bot = GetNativeCell(1);
|
||||
|
||||
char guild[32];
|
||||
GetNativeString(2, guild, sizeof(guild));
|
||||
|
||||
Function fCallback = GetNativeCell(3);
|
||||
Function fCallbackAll = GetNativeCell(4);
|
||||
any data = GetNativeCell(5);
|
||||
|
||||
DataPack dp = CreateDataPack();
|
||||
WritePackCell(dp, bot);
|
||||
WritePackString(dp, guild);
|
||||
WritePackCell(dp, plugin);
|
||||
WritePackFunction(dp, fCallback);
|
||||
WritePackFunction(dp, fCallbackAll);
|
||||
WritePackCell(dp, data);
|
||||
|
||||
ThisSendRequest(bot, guild, dp);
|
||||
}
|
||||
|
||||
static void ThisSendRequest(DiscordBot bot, char[] guild, DataPack dp) {
|
||||
char url[64];
|
||||
FormatEx(url, sizeof(url), "guilds/%s/channels", guild);
|
||||
|
||||
Handle request = PrepareRequest(bot, url, k_EHTTPMethodGET, null, GetGuildChannelsData);
|
||||
if(request == null) {
|
||||
CreateTimer(2.0, GetGuildChannelsDelayed, dp);
|
||||
return;
|
||||
}
|
||||
|
||||
SteamWorks_SetHTTPRequestContextValue(request, dp, UrlToDP(url));
|
||||
|
||||
DiscordSendRequest(request, url);
|
||||
}
|
||||
|
||||
public Action GetGuildChannelsDelayed(Handle timer, any data) {
|
||||
DataPack dp = view_as<DataPack>(data);
|
||||
ResetPack(dp);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
|
||||
char guild[32];
|
||||
ReadPackString(dp, guild, sizeof(guild));
|
||||
|
||||
ThisSendRequest(bot, guild, dp);
|
||||
}
|
||||
|
||||
public int GetGuildChannelsData(Handle request, bool failure, int offset, int statuscode, any dp) {
|
||||
if(failure || statuscode != 200) {
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
ResetPack(dp);
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
|
||||
char guild[32];
|
||||
ReadPackString(dp, guild, sizeof(guild));
|
||||
|
||||
ThisSendRequest(bot, guild, view_as<DataPack>(dp));
|
||||
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't Retrieve Guild Channels - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
delete view_as<Handle>(dp);
|
||||
return;
|
||||
}
|
||||
SteamWorks_GetHTTPResponseBodyCallback(request, GetGuildChannelsData_Data, dp);
|
||||
delete request;
|
||||
}
|
||||
|
||||
public int GetGuildChannelsData_Data(const char[] data, any datapack) {
|
||||
Handle hJson = json_load(data);
|
||||
|
||||
//Read from datapack to get info
|
||||
Handle dp = view_as<Handle>(datapack);
|
||||
ResetPack(dp);
|
||||
int bot = ReadPackCell(dp);
|
||||
|
||||
char guild[32];
|
||||
ReadPackString(dp, guild, sizeof(guild));
|
||||
|
||||
Handle plugin = view_as<Handle>(ReadPackCell(dp));
|
||||
Function func = ReadPackFunction(dp);
|
||||
Function funcAll = ReadPackFunction(dp);
|
||||
any pluginData = ReadPackCell(dp);
|
||||
delete dp;
|
||||
|
||||
//Create forwards
|
||||
Handle fForward = INVALID_HANDLE;
|
||||
Handle fForwardAll = INVALID_HANDLE;
|
||||
if(func != INVALID_FUNCTION) {
|
||||
fForward = CreateForward(ET_Ignore, Param_Cell, Param_String, Param_Cell, Param_Cell);
|
||||
AddToForward(fForward, plugin, func);
|
||||
}
|
||||
|
||||
if(funcAll != INVALID_FUNCTION) {
|
||||
fForwardAll = CreateForward(ET_Ignore, Param_Cell, Param_String, Param_Cell, Param_Cell);
|
||||
AddToForward(fForwardAll, plugin, funcAll);
|
||||
}
|
||||
|
||||
ArrayList alChannels = null;
|
||||
|
||||
if(funcAll != INVALID_FUNCTION) {
|
||||
alChannels = CreateArray();
|
||||
}
|
||||
|
||||
//Loop through json
|
||||
for(int i = 0; i < json_array_size(hJson); i++) {
|
||||
Handle hObject = json_array_get(hJson, i);
|
||||
|
||||
DiscordChannel Channel = view_as<DiscordChannel>(hObject);
|
||||
|
||||
if(fForward != INVALID_HANDLE) {
|
||||
Call_StartForward(fForward);
|
||||
Call_PushCell(bot);
|
||||
Call_PushString(guild);
|
||||
Call_PushCell(Channel);
|
||||
Call_PushCell(pluginData);
|
||||
Call_Finish();
|
||||
}
|
||||
|
||||
if(fForwardAll != INVALID_HANDLE) {
|
||||
alChannels.Push(Channel);
|
||||
}else {
|
||||
delete Channel;
|
||||
}
|
||||
}
|
||||
|
||||
if(fForwardAll != INVALID_HANDLE) {
|
||||
Call_StartForward(fForwardAll);
|
||||
Call_PushCell(bot);
|
||||
Call_PushString(guild);
|
||||
Call_PushCell(alChannels);
|
||||
Call_PushCell(pluginData);
|
||||
Call_Finish();
|
||||
|
||||
for(int i = 0; i < alChannels.Length; i++) {
|
||||
Handle hChannel = view_as<Handle>(alChannels.Get(i));
|
||||
delete hChannel;
|
||||
}
|
||||
|
||||
delete alChannels;
|
||||
delete fForwardAll;
|
||||
}
|
||||
|
||||
if(fForward != INVALID_HANDLE) {
|
||||
delete fForward;
|
||||
}
|
||||
|
||||
delete hJson;
|
||||
}
|
165
scripting/discord/GetGuilds.sp
Normal file
165
scripting/discord/GetGuilds.sp
Normal file
@@ -0,0 +1,165 @@
|
||||
public int Native_DiscordBot_GetGuilds(Handle plugin, int numParams) {
|
||||
DiscordBot bot = GetNativeCell(1);
|
||||
Function fCallback = GetNativeCell(2);
|
||||
Function fCallbackAll = GetNativeCell(3);
|
||||
any data = GetNativeCell(4);
|
||||
|
||||
DataPack dp = CreateDataPack();
|
||||
WritePackCell(dp, bot);
|
||||
WritePackCell(dp, plugin);
|
||||
WritePackFunction(dp, fCallback);
|
||||
WritePackFunction(dp, fCallbackAll);
|
||||
WritePackCell(dp, data);
|
||||
|
||||
ThisSendRequest(bot, dp);
|
||||
}
|
||||
|
||||
static void ThisSendRequest(DiscordBot bot, DataPack dp) {
|
||||
char url[64];
|
||||
FormatEx(url, sizeof(url), "users/@me/guilds");
|
||||
|
||||
Handle request = PrepareRequest(bot, url, k_EHTTPMethodGET, null, GetGuildsData);
|
||||
if(request == null) {
|
||||
CreateTimer(2.0, GetGuildsDelayed, dp);
|
||||
return;
|
||||
}
|
||||
|
||||
SteamWorks_SetHTTPRequestContextValue(request, dp, UrlToDP(url));
|
||||
|
||||
DiscordSendRequest(request, url);
|
||||
}
|
||||
|
||||
public Action GetGuildsDelayed(Handle timer, any data) {
|
||||
DataPack dp = view_as<DataPack>(data);
|
||||
ResetPack(dp);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
|
||||
ThisSendRequest(bot, dp);
|
||||
}
|
||||
|
||||
public int GetGuildsData(Handle request, bool failure, int offset, int statuscode, any dp) {
|
||||
if(failure || statuscode != 200) {
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
ResetPack(dp);
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
ThisSendRequest(bot, dp);
|
||||
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't Retrieve Guilds - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
delete view_as<Handle>(dp);
|
||||
return;
|
||||
}
|
||||
SteamWorks_GetHTTPResponseBodyCallback(request, GetGuildsData_Data, dp);
|
||||
delete request;
|
||||
}
|
||||
|
||||
public int GetGuildsData_Data(const char[] data, any datapack) {
|
||||
Handle hJson = json_load(data);
|
||||
|
||||
//Read from datapack to get info
|
||||
Handle dp = view_as<Handle>(datapack);
|
||||
ResetPack(dp);
|
||||
int bot = ReadPackCell(dp);
|
||||
Handle plugin = view_as<Handle>(ReadPackCell(dp));
|
||||
Function func = ReadPackFunction(dp);
|
||||
Function funcAll = ReadPackFunction(dp);
|
||||
any pluginData = ReadPackCell(dp);
|
||||
delete dp;
|
||||
|
||||
//Create forwards
|
||||
Handle fForward = INVALID_HANDLE;
|
||||
Handle fForwardAll = INVALID_HANDLE;
|
||||
if(func != INVALID_FUNCTION) {
|
||||
fForward = CreateForward(ET_Ignore, Param_Cell, Param_String, Param_String, Param_String, Param_Cell, Param_Cell, Param_Cell);
|
||||
AddToForward(fForward, plugin, func);
|
||||
}
|
||||
|
||||
if(funcAll != INVALID_FUNCTION) {
|
||||
fForwardAll = CreateForward(ET_Ignore, Param_Cell, Param_Cell, Param_Cell, Param_Cell, Param_Cell, Param_Cell, Param_Cell);
|
||||
AddToForward(fForwardAll, plugin, funcAll);
|
||||
}
|
||||
|
||||
ArrayList alId = null;
|
||||
ArrayList alName = null;
|
||||
ArrayList alIcon = null;
|
||||
ArrayList alOwner = null;
|
||||
ArrayList alPermissions = null;
|
||||
|
||||
if(funcAll != INVALID_FUNCTION) {
|
||||
alId = CreateArray(32);
|
||||
alName = CreateArray(64);
|
||||
alIcon = CreateArray(128);
|
||||
alOwner = CreateArray();
|
||||
alPermissions = CreateArray();
|
||||
}
|
||||
|
||||
//Loop through json
|
||||
for(int i = 0; i < json_array_size(hJson); i++) {
|
||||
Handle hObject = json_array_get(hJson, i);
|
||||
|
||||
static char id[32];
|
||||
static char name[64];
|
||||
static char icon[128];
|
||||
bool owner = false;
|
||||
int permissions;
|
||||
|
||||
JsonObjectGetString(hObject, "id", id, sizeof(id));
|
||||
JsonObjectGetString(hObject, "name", name, sizeof(name));
|
||||
JsonObjectGetString(hObject, "icon", icon, sizeof(icon));
|
||||
|
||||
owner = JsonObjectGetBool(hObject, "owner");
|
||||
permissions = JsonObjectGetBool(hObject, "permissions");
|
||||
|
||||
if(fForward != INVALID_HANDLE) {
|
||||
Call_StartForward(fForward);
|
||||
Call_PushCell(bot);
|
||||
Call_PushString(id);
|
||||
Call_PushString(name);
|
||||
Call_PushString(icon);
|
||||
Call_PushCell(owner);
|
||||
Call_PushCell(permissions);
|
||||
Call_PushCell(pluginData);
|
||||
Call_Finish();
|
||||
}
|
||||
|
||||
if(fForwardAll != INVALID_HANDLE) {
|
||||
alId.PushString(id);
|
||||
alName.PushString(name);
|
||||
alIcon.PushString(icon);
|
||||
alOwner.Push(owner);
|
||||
alPermissions.Push(permissions);
|
||||
}
|
||||
|
||||
delete hObject;
|
||||
}
|
||||
|
||||
if(fForwardAll != INVALID_HANDLE) {
|
||||
Call_StartForward(fForwardAll);
|
||||
Call_PushCell(bot);
|
||||
Call_PushCell(alId);
|
||||
Call_PushCell(alName);
|
||||
Call_PushCell(alIcon);
|
||||
Call_PushCell(alOwner);
|
||||
Call_PushCell(alPermissions);
|
||||
Call_PushCell(pluginData);
|
||||
Call_Finish();
|
||||
|
||||
delete alId;
|
||||
delete alName;
|
||||
delete alIcon;
|
||||
delete alOwner;
|
||||
delete alPermissions;
|
||||
|
||||
delete fForwardAll;
|
||||
}
|
||||
|
||||
if(fForward != INVALID_HANDLE) {
|
||||
delete fForward;
|
||||
}
|
||||
|
||||
delete hJson;
|
||||
}
|
156
scripting/discord/GuildMembers.sp
Normal file
156
scripting/discord/GuildMembers.sp
Normal file
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* public native void GetGuildMembers(char[] guild, OnGetMembers fCallback, char[] afterUserID="", int limit=250);
|
||||
*/
|
||||
public int Native_DiscordBot_GetGuildMembers(Handle plugin, int numParams) {
|
||||
DiscordBot bot = view_as<DiscordBot>(CloneHandle(GetNativeCell(1)));
|
||||
|
||||
char guild[32];
|
||||
GetNativeString(2, guild, sizeof(guild));
|
||||
|
||||
Function fCallback = GetNativeCell(3);
|
||||
|
||||
int limit = GetNativeCell(4);
|
||||
|
||||
char afterID[32];
|
||||
GetNativeString(5, afterID, sizeof(afterID));
|
||||
|
||||
Handle hData = json_object();
|
||||
json_object_set_new(hData, "bot", bot);
|
||||
json_object_set_new(hData, "guild", json_string(guild));
|
||||
json_object_set_new(hData, "limit", json_integer(limit));
|
||||
json_object_set_new(hData, "afterID", json_string(afterID));
|
||||
|
||||
Handle fwd = CreateForward(ET_Ignore, Param_Cell, Param_String, Param_Cell);
|
||||
AddToForward(fwd, plugin, fCallback);
|
||||
json_object_set_new(hData, "callback", json_integer(view_as<int>(fwd)));
|
||||
|
||||
GetMembers(hData);
|
||||
}
|
||||
|
||||
public int Native_DiscordBot_GetGuildMembersAll(Handle plugin, int numParams) {
|
||||
DiscordBot bot = view_as<DiscordBot>(CloneHandle(GetNativeCell(1)));
|
||||
|
||||
char guild[32];
|
||||
GetNativeString(2, guild, sizeof(guild));
|
||||
|
||||
Function fCallback = GetNativeCell(3);
|
||||
|
||||
int limit = GetNativeCell(4);
|
||||
|
||||
char afterID[32];
|
||||
GetNativeString(5, afterID, sizeof(afterID));
|
||||
|
||||
Handle hData = json_object();
|
||||
json_object_set_new(hData, "bot", bot);
|
||||
json_object_set_new(hData, "guild", json_string(guild));
|
||||
json_object_set_new(hData, "limit", json_integer(limit));
|
||||
json_object_set_new(hData, "afterID", json_string(afterID));
|
||||
|
||||
Handle fwd = CreateForward(ET_Ignore, Param_Cell, Param_String, Param_Cell);
|
||||
AddToForward(fwd, plugin, fCallback);
|
||||
json_object_set_new(hData, "callback", json_integer(view_as<int>(fwd)));
|
||||
|
||||
GetMembers(hData);
|
||||
}
|
||||
|
||||
static void GetMembers(Handle hData) {
|
||||
DiscordBot bot = view_as<DiscordBot>(json_object_get(hData, "bot"));
|
||||
|
||||
char guild[32];
|
||||
JsonObjectGetString(hData, "guild", guild, sizeof(guild));
|
||||
|
||||
int limit = JsonObjectGetInt(hData, "limit");
|
||||
|
||||
char afterID[32];
|
||||
JsonObjectGetString(hData, "afterID", afterID, sizeof(afterID));
|
||||
|
||||
char url[256];
|
||||
if(StrEqual(afterID, "")) {
|
||||
FormatEx(url, sizeof(url), "https://discordapp.com/api/guilds/%s/members?limit=%i", guild, limit);
|
||||
}else {
|
||||
FormatEx(url, sizeof(url), "https://discordapp.com/api/guilds/%s/members?limit=%i&afterID=%s", guild, limit, afterID);
|
||||
}
|
||||
|
||||
char route[128];
|
||||
FormatEx(route, sizeof(route), "guild/%s/members", guild);
|
||||
|
||||
DiscordRequest request = new DiscordRequest(url, k_EHTTPMethodGET);
|
||||
if(request == null) {
|
||||
delete bot;
|
||||
CreateTimer(2.0, SendGetMembers, hData);
|
||||
return;
|
||||
}
|
||||
request.SetCallbacks(HTTPCompleted, MembersDataReceive);
|
||||
request.SetBot(bot);
|
||||
request.SetData(hData, route);
|
||||
|
||||
request.Send(route);
|
||||
|
||||
delete bot;
|
||||
}
|
||||
|
||||
public Action SendGetMembers(Handle timer, any data) {
|
||||
GetMembers(view_as<Handle>(data));
|
||||
}
|
||||
|
||||
|
||||
public MembersDataReceive(Handle request, bool failure, int offset, int statuscode, any dp) {
|
||||
if(failure || (statuscode != 200)) {
|
||||
if(statuscode == 400) {
|
||||
PrintToServer("BAD REQUEST");
|
||||
}
|
||||
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
GetMembers(dp);
|
||||
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't Send GetMembers - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
delete view_as<Handle>(dp);
|
||||
return;
|
||||
}
|
||||
SteamWorks_GetHTTPResponseBodyCallback(request, GetMembersData, dp);
|
||||
delete request;
|
||||
}
|
||||
|
||||
public int GetMembersData(const char[] data, any dp) {
|
||||
Handle hJson = json_load(data);
|
||||
Handle hData = view_as<Handle>(dp);
|
||||
DiscordBot bot = view_as<DiscordBot>(json_object_get(hData, "bot"));
|
||||
|
||||
Handle fwd = view_as<Handle>(JsonObjectGetInt(hData, "callback"));
|
||||
|
||||
char guild[32];
|
||||
JsonObjectGetString(hData, "guild", guild, sizeof(guild));
|
||||
|
||||
if(fwd != null) {
|
||||
Call_StartForward(fwd);
|
||||
Call_PushCell(bot);
|
||||
Call_PushString(guild);
|
||||
Call_PushCell(hJson);
|
||||
Call_Finish();
|
||||
}
|
||||
|
||||
delete bot;
|
||||
if(JsonObjectGetBool(hData, "autoPaginate")) {
|
||||
int size = json_array_size(hJson);
|
||||
int limit = JsonObjectGetInt(hData, "limit");
|
||||
if(limit == size) {
|
||||
Handle hLast = json_array_get(hJson, size - 1);
|
||||
char lastID[32];
|
||||
json_string_value(hLast, lastID, sizeof(lastID));
|
||||
delete hJson;
|
||||
delete hLast;
|
||||
|
||||
json_object_set_new(hData, "afterID", json_string(lastID));
|
||||
GetMembers(hData);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
delete hJson;
|
||||
delete hData;
|
||||
delete fwd;
|
||||
}
|
102
scripting/discord/GuildRole.sp
Normal file
102
scripting/discord/GuildRole.sp
Normal file
@@ -0,0 +1,102 @@
|
||||
public int Native_DiscordBot_GetGuildRoles(Handle plugin, int numParams) {
|
||||
DiscordBot bot = view_as<DiscordBot>(CloneHandle(GetNativeCell(1)));
|
||||
|
||||
char guild[32];
|
||||
GetNativeString(2, guild, sizeof(guild));
|
||||
|
||||
Function fCallback = GetNativeCell(3);
|
||||
|
||||
any data = GetNativeCell(4);
|
||||
|
||||
Handle hData = json_object();
|
||||
json_object_set_new(hData, "bot", bot);
|
||||
json_object_set_new(hData, "guild", json_string(guild));
|
||||
json_object_set_new(hData, "data1", json_integer(view_as<int>(data)));
|
||||
|
||||
Handle fwd = CreateForward(ET_Ignore, Param_Cell, Param_String, Param_Cell, Param_Cell);
|
||||
AddToForward(fwd, plugin, fCallback);
|
||||
json_object_set_new(hData, "callback", json_integer(view_as<int>(fwd)));
|
||||
|
||||
GetGuildRoles(hData);
|
||||
}
|
||||
|
||||
static void GetGuildRoles(Handle hData) {
|
||||
DiscordBot bot = view_as<DiscordBot>(json_object_get(hData, "bot"));
|
||||
|
||||
char guild[32];
|
||||
JsonObjectGetString(hData, "guild", guild, sizeof(guild));
|
||||
|
||||
|
||||
char url[256];
|
||||
FormatEx(url, sizeof(url), "https://discordapp.com/api/guilds/%s/roles", guild);
|
||||
|
||||
char route[128];
|
||||
FormatEx(route, sizeof(route), "guild/%s/roles", guild);
|
||||
|
||||
DiscordRequest request = new DiscordRequest(url, k_EHTTPMethodGET);
|
||||
if(request == null) {
|
||||
delete bot;
|
||||
CreateTimer(2.0, SendGetGuildRoles, hData);
|
||||
return;
|
||||
}
|
||||
request.SetCallbacks(HTTPCompleted, GetGuildRolesReceive);
|
||||
request.SetBot(bot);
|
||||
request.SetData(hData, route);
|
||||
|
||||
request.Send(route);
|
||||
|
||||
delete bot;
|
||||
}
|
||||
|
||||
public Action SendGetGuildRoles(Handle timer, any data) {
|
||||
GetGuildRoles(view_as<Handle>(data));
|
||||
}
|
||||
|
||||
|
||||
public GetGuildRolesReceive(Handle request, bool failure, int offset, int statuscode, any dp) {
|
||||
if(failure || (statuscode != 200)) {
|
||||
if(statuscode == 400) {
|
||||
PrintToServer("BAD REQUEST");
|
||||
}
|
||||
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
GetGuildRoles(dp);
|
||||
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't Send GetGuildRoles - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
delete view_as<Handle>(dp);
|
||||
return;
|
||||
}
|
||||
SteamWorks_GetHTTPResponseBodyCallback(request, GetRolesData, dp);
|
||||
delete request;
|
||||
}
|
||||
|
||||
public int GetRolesData(const char[] data, any dp) {
|
||||
Handle hJson = json_load(data);
|
||||
Handle hData = view_as<Handle>(dp);
|
||||
DiscordBot bot = view_as<DiscordBot>(json_object_get(hData, "bot"));
|
||||
|
||||
Handle fwd = view_as<Handle>(JsonObjectGetInt(hData, "callback"));
|
||||
|
||||
char guild[32];
|
||||
JsonObjectGetString(hData, "guild", guild, sizeof(guild));
|
||||
|
||||
any data1 = JsonObjectGetInt(hData, "data1");
|
||||
|
||||
if(fwd != null) {
|
||||
Call_StartForward(fwd);
|
||||
Call_PushCell(bot);
|
||||
Call_PushString(guild);
|
||||
Call_PushCell(view_as<RoleList>(hJson));
|
||||
Call_PushCell(data1);
|
||||
Call_Finish();
|
||||
}
|
||||
|
||||
delete bot;
|
||||
delete hJson;
|
||||
delete hData;
|
||||
delete fwd;
|
||||
}
|
145
scripting/discord/ListenToChannel.sp
Normal file
145
scripting/discord/ListenToChannel.sp
Normal file
@@ -0,0 +1,145 @@
|
||||
public int Native_DiscordBot_StartTimer(Handle plugin, int numParams) {
|
||||
DiscordBot bot = GetNativeCell(1);
|
||||
DiscordChannel channel = GetNativeCell(2);
|
||||
Function func = GetNativeCell(3);
|
||||
|
||||
Handle hObj = json_object();
|
||||
json_object_set(hObj, "bot", bot);
|
||||
json_object_set(hObj, "channel", channel);
|
||||
|
||||
Handle fwd = CreateForward(ET_Ignore, Param_Cell, Param_Cell, Param_Cell);
|
||||
AddToForward(fwd, plugin, func);
|
||||
|
||||
json_object_set_new(hObj, "callback", json_integer(view_as<int>(fwd)));
|
||||
|
||||
GetMessages(hObj);
|
||||
}
|
||||
|
||||
public void GetMessages(Handle hObject) {
|
||||
DiscordBot bot = view_as<DiscordBot>(json_object_get(hObject, "bot"));
|
||||
DiscordChannel channel = view_as<DiscordChannel>(json_object_get(hObject, "channel"));
|
||||
//Handle fwd = view_as<Handle>(json_object_get(hObject, "callback"));
|
||||
|
||||
char channelID[32];
|
||||
channel.GetID(channelID, sizeof(channelID));
|
||||
|
||||
char lastMessage[64];
|
||||
channel.GetLastMessageID(lastMessage, sizeof(lastMessage));
|
||||
|
||||
char url[256];
|
||||
FormatEx(url, sizeof(url), "channels/%s/messages?limit=%i&after=%s", channelID, 100, lastMessage);
|
||||
|
||||
Handle request = PrepareRequest(bot, url, _, null, OnGetMessage);
|
||||
if(request == null) {
|
||||
delete bot;
|
||||
delete channel;
|
||||
CreateTimer(2.0, GetMessagesDelayed, hObject);
|
||||
return;
|
||||
}
|
||||
|
||||
char route[128];
|
||||
FormatEx(route, sizeof(route), "channels/%s", channelID);
|
||||
|
||||
SteamWorks_SetHTTPRequestContextValue(request, hObject, UrlToDP(route));
|
||||
|
||||
delete bot;
|
||||
delete channel;
|
||||
|
||||
DiscordSendRequest(request, route);
|
||||
}
|
||||
|
||||
public Action GetMessagesDelayed(Handle timer, any data) {
|
||||
GetMessages(view_as<Handle>(data));
|
||||
}
|
||||
|
||||
public Action CheckMessageTimer(Handle timer, any dpt) {
|
||||
GetMessages(view_as<Handle>(dpt));
|
||||
}
|
||||
|
||||
public int OnGetMessage(Handle request, bool failure, int offset, int statuscode, any dp) {
|
||||
if(failure || statuscode != 200) {
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
GetMessages(view_as<Handle>(dp));
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't Retrieve Messages - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
Handle fwd = view_as<Handle>(JsonObjectGetInt(view_as<Handle>(dp), "callback"));
|
||||
if(fwd != null) delete fwd;
|
||||
delete view_as<Handle>(dp);
|
||||
return;
|
||||
}
|
||||
|
||||
SteamWorks_GetHTTPResponseBodyCallback(request, OnGetMessage_Data, dp);
|
||||
delete request;
|
||||
}
|
||||
|
||||
public int OnGetMessage_Data(const char[] data, any dpt) {
|
||||
Handle hObj = view_as<Handle>(dpt);
|
||||
|
||||
DiscordBot Bot = view_as<DiscordBot>(json_object_get(hObj, "bot"));
|
||||
DiscordChannel channel = view_as<DiscordChannel>(json_object_get(hObj, "channel"));
|
||||
Handle fwd = view_as<Handle>(JsonObjectGetInt(hObj, "callback"));
|
||||
|
||||
if(!Bot.IsListeningToChannel(channel) || GetForwardFunctionCount(fwd) == 0) {
|
||||
delete Bot;
|
||||
delete channel;
|
||||
delete hObj;
|
||||
delete fwd;
|
||||
return;
|
||||
}
|
||||
|
||||
Handle hJson = json_load(data);
|
||||
|
||||
if(json_is_array(hJson)) {
|
||||
for(int i = json_array_size(hJson) - 1; i >= 0; i--) {
|
||||
Handle hObject = json_array_get(hJson, i);
|
||||
|
||||
//The reason we find Channel for each message instead of global incase
|
||||
//Bot stops listening for the channel while we are still sending messages
|
||||
char channelID[32];
|
||||
JsonObjectGetString(hObject, "channel_id", channelID, sizeof(channelID));
|
||||
|
||||
//Find Channel corresponding to Channel id
|
||||
//DiscordChannel Channel = Bot.GetListeningChannelByID(channelID);
|
||||
if(!Bot.IsListeningToChannelID(channelID)) {
|
||||
//Channel is no longer listed to, remove any handles & stop
|
||||
delete hObject;
|
||||
delete hJson;
|
||||
|
||||
delete fwd;
|
||||
delete Bot;
|
||||
delete channel;
|
||||
delete hObj;
|
||||
return;
|
||||
}
|
||||
|
||||
char id[32];
|
||||
JsonObjectGetString(hObject, "id", id, sizeof(id));
|
||||
|
||||
if(i == 0) {
|
||||
channel.SetLastMessageID(id);
|
||||
}
|
||||
|
||||
//Get info and fire forward
|
||||
if(fwd != null) {
|
||||
Call_StartForward(fwd);
|
||||
Call_PushCell(Bot);
|
||||
Call_PushCell(channel);
|
||||
Call_PushCell(view_as<DiscordMessage>(hObject));
|
||||
Call_Finish();
|
||||
}
|
||||
|
||||
delete hObject;
|
||||
}
|
||||
}
|
||||
|
||||
CreateTimer(Bot.MessageCheckInterval, CheckMessageTimer, hObj);
|
||||
|
||||
delete Bot;
|
||||
delete channel;
|
||||
|
||||
|
||||
delete hJson;
|
||||
}
|
36
scripting/discord/MessageObject.sp
Normal file
36
scripting/discord/MessageObject.sp
Normal file
@@ -0,0 +1,36 @@
|
||||
public int Native_DiscordMessage_GetID(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
char buffer[64];
|
||||
JsonObjectGetString(hJson, "id", buffer, sizeof(buffer));
|
||||
SetNativeString(2, buffer, GetNativeCell(3));
|
||||
}
|
||||
|
||||
public int Native_DiscordMessage_IsPinned(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
return JsonObjectGetBool(hJson, "pinned");
|
||||
}
|
||||
|
||||
public int Native_DiscordMessage_GetAuthor(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
|
||||
Handle hAuthor = json_object_get(hJson, "author");
|
||||
|
||||
DiscordUser user = view_as<DiscordUser>(CloneHandle(hAuthor, plugin));
|
||||
delete hAuthor;
|
||||
|
||||
return _:user;
|
||||
}
|
||||
|
||||
public int Native_DiscordMessage_GetContent(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
static char buffer[2000];
|
||||
JsonObjectGetString(hJson, "content", buffer, sizeof(buffer));
|
||||
SetNativeString(2, buffer, GetNativeCell(3));
|
||||
}
|
||||
|
||||
public int Native_DiscordMessage_GetChannelID(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
char buffer[64];
|
||||
JsonObjectGetString(hJson, "channel_id", buffer, sizeof(buffer));
|
||||
SetNativeString(2, buffer, GetNativeCell(3));
|
||||
}
|
138
scripting/discord/SendMessage.sp
Normal file
138
scripting/discord/SendMessage.sp
Normal file
@@ -0,0 +1,138 @@
|
||||
public int Native_DiscordBot_SendMessageToChannel(Handle plugin, int numParams) {
|
||||
DiscordBot bot = GetNativeCell(1);
|
||||
char channel[32];
|
||||
static char message[2048];
|
||||
GetNativeString(2, channel, sizeof(channel));
|
||||
GetNativeString(3, message, sizeof(message));
|
||||
|
||||
Function fCallback = GetNativeCell(4);
|
||||
any data = GetNativeCell(5);
|
||||
Handle fForward = null;
|
||||
if(fCallback != INVALID_FUNCTION) {
|
||||
fForward = CreateForward(ET_Ignore, Param_Cell, Param_Cell, Param_Cell, Param_Cell);
|
||||
AddToForward(fForward, plugin, fCallback);
|
||||
}
|
||||
|
||||
SendMessage(bot, channel, message, fForward, data);
|
||||
}
|
||||
|
||||
public int Native_DiscordBot_SendMessage(Handle plugin, int numParams) {
|
||||
DiscordBot bot = GetNativeCell(1);
|
||||
|
||||
DiscordChannel Channel = GetNativeCell(2);
|
||||
char channelID[32];
|
||||
Channel.GetID(channelID, sizeof(channelID));
|
||||
|
||||
static char message[2048];
|
||||
GetNativeString(3, message, sizeof(message));
|
||||
|
||||
Function fCallback = GetNativeCell(4);
|
||||
any data = GetNativeCell(5);
|
||||
Handle fForward = null;
|
||||
if(fCallback != INVALID_FUNCTION) {
|
||||
fForward = CreateForward(ET_Ignore, Param_Cell, Param_Cell, Param_Cell, Param_Cell);
|
||||
AddToForward(fForward, plugin, fCallback);
|
||||
}
|
||||
|
||||
SendMessage(bot, channelID, message, fForward, data);
|
||||
}
|
||||
|
||||
public int Native_DiscordChannel_SendMessage(Handle plugin, int numParams) {
|
||||
DiscordChannel channel = view_as<DiscordChannel>(GetNativeCell(1));
|
||||
|
||||
char channelID[32];
|
||||
channel.GetID(channelID, sizeof(channelID));
|
||||
|
||||
DiscordBot bot = GetNativeCell(2);
|
||||
|
||||
static char message[2048];
|
||||
GetNativeString(3, message, sizeof(message));
|
||||
|
||||
Function fCallback = GetNativeCell(4);
|
||||
any data = GetNativeCell(5);
|
||||
Handle fForward = null;
|
||||
if(fCallback != INVALID_FUNCTION) {
|
||||
fForward = CreateForward(ET_Ignore, Param_Cell, Param_Cell, Param_Cell, Param_Cell);
|
||||
AddToForward(fForward, plugin, fCallback);
|
||||
}
|
||||
|
||||
SendMessage(bot, channelID, message, fForward, data);
|
||||
}
|
||||
|
||||
static void SendMessage(DiscordBot bot, char[] channel, char[] message, Handle fForward, any data) {
|
||||
Handle hJson = json_object();
|
||||
|
||||
json_object_set_new(hJson, "content", json_string(message));
|
||||
|
||||
char url[64];
|
||||
FormatEx(url, sizeof(url), "channels/%s/messages", channel);
|
||||
|
||||
DataPack dpSafety = new DataPack();
|
||||
WritePackCell(dpSafety, bot);
|
||||
WritePackString(dpSafety, channel);
|
||||
WritePackString(dpSafety, message);
|
||||
WritePackCell(dpSafety, fForward);
|
||||
WritePackCell(dpSafety, data);
|
||||
|
||||
Handle request = PrepareRequest(bot, url, k_EHTTPMethodPOST, hJson, GetSendMessageData);
|
||||
if(request == null) {
|
||||
delete hJson;
|
||||
CreateTimer(2.0, SendMessageDelayed, dpSafety);
|
||||
return;
|
||||
}
|
||||
|
||||
SteamWorks_SetHTTPRequestContextValue(request, dpSafety, UrlToDP(url));
|
||||
|
||||
DiscordSendRequest(request, url);
|
||||
}
|
||||
|
||||
public Action SendMessageDelayed(Handle timer, any data) {
|
||||
DataPack dp = view_as<DataPack>(data);
|
||||
ResetPack(dp);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
|
||||
char channel[32];
|
||||
ReadPackString(dp, channel, sizeof(channel));
|
||||
|
||||
char message[2048];
|
||||
ReadPackString(dp, message, sizeof(message));
|
||||
|
||||
Handle fForward = ReadPackCell(dp);
|
||||
any dataa = ReadPackCell(dp);
|
||||
|
||||
delete dp;
|
||||
|
||||
SendMessage(bot, channel, message, fForward, dataa);
|
||||
}
|
||||
|
||||
public int GetSendMessageData(Handle request, bool failure, int offset, int statuscode, any dp) {
|
||||
if(failure || statuscode != 200) {
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
ResetPack(dp);
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
|
||||
char channel[32];
|
||||
ReadPackString(dp, channel, sizeof(channel));
|
||||
|
||||
char message[2048];
|
||||
ReadPackString(dp, message, sizeof(message));
|
||||
|
||||
Handle fForward = ReadPackCell(dp);
|
||||
any data = ReadPackCell(dp);
|
||||
|
||||
delete view_as<Handle>(dp);
|
||||
|
||||
SendMessage(bot, channel, message, fForward, data);
|
||||
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't Send Message - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
delete view_as<Handle>(dp);
|
||||
return;
|
||||
}
|
||||
delete request;
|
||||
delete view_as<Handle>(dp);
|
||||
}
|
104
scripting/discord/SendWebHook.sp
Normal file
104
scripting/discord/SendWebHook.sp
Normal file
@@ -0,0 +1,104 @@
|
||||
public int Native_DiscordWebHook_Send(Handle plugin, int numParams) {
|
||||
DiscordWebHook hook = GetNativeCell(1);
|
||||
SendWebHook(view_as<DiscordWebHook>(hook));
|
||||
}
|
||||
|
||||
public void SendWebHook(DiscordWebHook hook) {
|
||||
if(!JsonObjectGetBool(hook, "__selfCopy", false)) {
|
||||
hook = view_as<DiscordWebHook>(json_deep_copy(hook));
|
||||
json_object_set_new(hook, "__selfCopy", json_true());
|
||||
}
|
||||
Handle hJson = hook.Data;
|
||||
|
||||
char url[256];
|
||||
hook.GetUrl(url, sizeof(url));
|
||||
|
||||
if(hook.SlackMode) {
|
||||
if(StrContains(url, "/slack") == -1) {
|
||||
Format(url, sizeof(url), "%s/slack", url);
|
||||
}
|
||||
|
||||
RenameJsonObject(hJson, "content", "text");
|
||||
RenameJsonObject(hJson, "embeds", "attachments");
|
||||
|
||||
Handle hAttachments = json_object_get(hJson, "attachments");
|
||||
if(hAttachments != null) {
|
||||
if(json_is_array(hAttachments)) {
|
||||
for(int i = 0; i < json_array_size(hAttachments); i++) {
|
||||
Handle hEmbed = json_array_get(hAttachments, i);
|
||||
|
||||
Handle hFields = json_object_get(hEmbed, "fields");
|
||||
if(hFields) {
|
||||
if(json_is_array(hFields)) {
|
||||
for(int j = 0; j < json_array_size(hFields); j++) {
|
||||
Handle hField = json_array_get(hFields, j);
|
||||
RenameJsonObject(hField, "name", "title");
|
||||
RenameJsonObject(hField, "inline", "short");
|
||||
//json_array_set_new(hFields, j, hField);
|
||||
delete hField;
|
||||
}
|
||||
}
|
||||
|
||||
//json_object_set_new(hEmbed, "fields", hFields);
|
||||
delete hFields;
|
||||
}
|
||||
|
||||
//json_array_set_new(hAttachments, i, hEmbed);
|
||||
delete hEmbed;
|
||||
}
|
||||
}
|
||||
|
||||
//json_object_set_new(hJson, "attachments", hAttachments);
|
||||
delete hAttachments;
|
||||
}
|
||||
}
|
||||
|
||||
//Send
|
||||
DiscordRequest request = new DiscordRequest(url, k_EHTTPMethodPOST);
|
||||
request.SetCallbacks(HTTPCompleted, SendWebHookReceiveData);
|
||||
request.SetJsonBodyEx(hJson);
|
||||
//Handle request = PrepareRequestRaw(null, url, k_EHTTPMethodPOST, hJson, SendWebHookReceiveData);
|
||||
if(request == null) {
|
||||
CreateTimer(2.0, SendWebHookDelayed, hJson);
|
||||
return;
|
||||
}
|
||||
|
||||
request.SetContextValue(hJson, UrlToDP(url));
|
||||
|
||||
//DiscordSendRequest(request, url);
|
||||
request.Send(url);
|
||||
}
|
||||
|
||||
public Action SendWebHookDelayed(Handle timer, any data) {
|
||||
SendWebHook(view_as<DiscordWebHook>(data));
|
||||
}
|
||||
|
||||
public SendWebHookReceiveData(Handle request, bool failure, int offset, int statuscode, any dp) {
|
||||
if(failure || (statuscode != 200 && statuscode != 204)) {
|
||||
if(statuscode == 400) {
|
||||
PrintToServer("BAD REQUEST");
|
||||
SteamWorks_GetHTTPResponseBodyCallback(request, WebHookData, dp);
|
||||
}
|
||||
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
SendWebHook(view_as<DiscordWebHook>(dp));
|
||||
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't Send Webhook - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
delete view_as<Handle>(dp);
|
||||
return;
|
||||
}
|
||||
delete request;
|
||||
delete view_as<Handle>(dp);
|
||||
}
|
||||
|
||||
public int WebHookData(const char[] data, any dp) {
|
||||
PrintToServer("DATA RECE: %s", data);
|
||||
static char stringJson[16384];
|
||||
stringJson[0] = '\0';
|
||||
json_dump(view_as<Handle>(dp), stringJson, sizeof(stringJson), 0, true);
|
||||
PrintToServer("DATA SENT: %s", stringJson);
|
||||
}
|
44
scripting/discord/UserObject.sp
Normal file
44
scripting/discord/UserObject.sp
Normal file
@@ -0,0 +1,44 @@
|
||||
public int Native_DiscordUser_GetID(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
char buffer[64];
|
||||
JsonObjectGetString(hJson, "id", buffer, sizeof(buffer));
|
||||
SetNativeString(2, buffer, GetNativeCell(3));
|
||||
}
|
||||
|
||||
public int Native_DiscordUser_GetUsername(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
char buffer[64];
|
||||
JsonObjectGetString(hJson, "username", buffer, sizeof(buffer));
|
||||
SetNativeString(2, buffer, GetNativeCell(3));
|
||||
}
|
||||
|
||||
public int Native_DiscordUser_GetDiscriminator(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
char buffer[16];
|
||||
JsonObjectGetString(hJson, "discriminator", buffer, sizeof(buffer));
|
||||
SetNativeString(2, buffer, GetNativeCell(3));
|
||||
}
|
||||
|
||||
public int Native_DiscordUser_GetAvatar(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
char buffer[256];
|
||||
JsonObjectGetString(hJson, "avatar", buffer, sizeof(buffer));
|
||||
SetNativeString(2, buffer, GetNativeCell(3));
|
||||
}
|
||||
|
||||
public int Native_DiscordUser_GetEmail(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
char buffer[64];
|
||||
JsonObjectGetString(hJson, "email", buffer, sizeof(buffer));
|
||||
SetNativeString(2, buffer, GetNativeCell(3));
|
||||
}
|
||||
|
||||
public int Native_DiscordUser_IsVerified(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
return JsonObjectGetBool(hJson, "verified");
|
||||
}
|
||||
|
||||
public int Native_DiscordUser_IsBot(Handle plugin, int numParams) {
|
||||
Handle hJson = GetNativeCell(1);
|
||||
return JsonObjectGetBool(hJson, "bot");
|
||||
}
|
129
scripting/discord/deletemessage.sp
Normal file
129
scripting/discord/deletemessage.sp
Normal file
@@ -0,0 +1,129 @@
|
||||
public int Native_DiscordBot_DeleteMessageID(Handle plugin, int numParams) {
|
||||
DiscordBot bot = GetNativeCell(1);
|
||||
|
||||
char channelid[64];
|
||||
GetNativeString(2, channelid, sizeof(channelid));
|
||||
|
||||
char msgid[64];
|
||||
GetNativeString(3, msgid, sizeof(msgid));
|
||||
|
||||
Function fCallback = GetNativeCell(4);
|
||||
any data = GetNativeCell(5);
|
||||
|
||||
DataPack dp = CreateDataPack();
|
||||
WritePackCell(dp, bot);
|
||||
WritePackString(dp, channelid);
|
||||
WritePackString(dp, msgid);
|
||||
WritePackCell(dp, plugin);
|
||||
WritePackFunction(dp, fCallback);
|
||||
WritePackCell(dp, data);
|
||||
|
||||
ThisDeleteMessage(bot, channelid, msgid, dp);
|
||||
}
|
||||
|
||||
public int Native_DiscordBot_DeleteMessage(Handle plugin, int numParams) {
|
||||
DiscordBot bot = GetNativeCell(1);
|
||||
|
||||
char channelid[64];
|
||||
DiscordChannel channel = GetNativeCell(2);
|
||||
channel.GetID(channelid, sizeof(channelid));
|
||||
|
||||
char msgid[64];
|
||||
DiscordMessage msg = GetNativeCell(3);
|
||||
msg.GetID(msgid, sizeof(msgid));
|
||||
|
||||
Function fCallback = GetNativeCell(4);
|
||||
any data = GetNativeCell(5);
|
||||
|
||||
DataPack dp = CreateDataPack();
|
||||
WritePackCell(dp, bot);
|
||||
WritePackString(dp, channelid);
|
||||
WritePackString(dp, msgid);
|
||||
WritePackCell(dp, plugin);
|
||||
WritePackFunction(dp, fCallback);
|
||||
WritePackCell(dp, data);
|
||||
|
||||
ThisDeleteMessage(bot, channelid, msgid, dp);
|
||||
}
|
||||
|
||||
static void ThisDeleteMessage(DiscordBot bot, char[] channelid, char[] msgid, DataPack dp) {
|
||||
char url[64];
|
||||
FormatEx(url, sizeof(url), "channels/%s/messages/%s", channelid, msgid);
|
||||
|
||||
Handle request = PrepareRequest(bot, url, k_EHTTPMethodDELETE, null, MessageDeletedResp);
|
||||
if(request == null) {
|
||||
CreateTimer(2.0, ThisDeleteMessageDelayed, dp);
|
||||
return;
|
||||
}
|
||||
|
||||
SteamWorks_SetHTTPRequestContextValue(request, dp, UrlToDP(url));
|
||||
|
||||
DiscordSendRequest(request, url);
|
||||
}
|
||||
|
||||
public Action ThisDeleteMessageDelayed(Handle timer, any data) {
|
||||
DataPack dp = view_as<DataPack>(data);
|
||||
ResetPack(dp);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
|
||||
char channelid[32];
|
||||
ReadPackString(dp, channelid, sizeof(channelid));
|
||||
|
||||
char msgid[32];
|
||||
ReadPackString(dp, msgid, sizeof(msgid));
|
||||
|
||||
ThisDeleteMessage(bot, channelid, msgid, dp);
|
||||
}
|
||||
|
||||
public int MessageDeletedResp(Handle request, bool failure, int offset, int statuscode, any dp) {
|
||||
if(failure || statuscode != 204) {
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
ResetPack(dp);
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
|
||||
char channelid[32];
|
||||
ReadPackString(dp, channelid, sizeof(channelid));
|
||||
|
||||
char msgid[32];
|
||||
ReadPackString(dp, msgid, sizeof(msgid));
|
||||
|
||||
ThisDeleteMessage(bot, channelid, msgid, view_as<DataPack>(dp));
|
||||
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't delete message - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
delete view_as<Handle>(dp);
|
||||
return;
|
||||
}
|
||||
|
||||
ResetPack(dp);
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
|
||||
char channelid[32];
|
||||
ReadPackString(dp, channelid, sizeof(channelid));
|
||||
|
||||
char msgid[32];
|
||||
ReadPackString(dp, msgid, sizeof(msgid));
|
||||
|
||||
Handle plugin = view_as<Handle>(ReadPackCell(dp));
|
||||
Function func = ReadPackFunction(dp);
|
||||
any pluginData = ReadPackCell(dp);
|
||||
|
||||
Handle fForward = INVALID_HANDLE;
|
||||
if(func != INVALID_FUNCTION) {
|
||||
fForward = CreateForward(ET_Ignore, Param_Cell, Param_Cell);
|
||||
AddToForward(fForward, plugin, func);
|
||||
|
||||
Call_StartForward(fForward);
|
||||
Call_PushCell(bot);
|
||||
Call_PushCell(pluginData);
|
||||
Call_Finish();
|
||||
delete fForward;
|
||||
}
|
||||
|
||||
delete view_as<Handle>(dp);
|
||||
delete request;
|
||||
}
|
328
scripting/discord/reactions.sp
Normal file
328
scripting/discord/reactions.sp
Normal file
@@ -0,0 +1,328 @@
|
||||
public int Native_DiscordBot_AddReaction(Handle plugin, int numParams) {
|
||||
DiscordBot bot = GetNativeCell(1);
|
||||
|
||||
char channel[32];
|
||||
GetNativeString(2, channel, sizeof(channel));
|
||||
|
||||
char msgid[64];
|
||||
GetNativeString(3, msgid, sizeof(msgid));
|
||||
|
||||
char emoji[128];
|
||||
GetNativeString(4, emoji, sizeof(emoji));
|
||||
|
||||
AddReaction(bot, channel, msgid, emoji);
|
||||
}
|
||||
|
||||
public int Native_DiscordBot_DeleteReaction(Handle plugin, int numParams) {
|
||||
DiscordBot bot = GetNativeCell(1);
|
||||
|
||||
char channel[32];
|
||||
GetNativeString(2, channel, sizeof(channel));
|
||||
|
||||
char msgid[64];
|
||||
GetNativeString(3, msgid, sizeof(msgid));
|
||||
|
||||
char emoji[128];
|
||||
GetNativeString(4, emoji, sizeof(emoji));
|
||||
|
||||
char user[128];
|
||||
GetNativeString(5, user, sizeof(user));
|
||||
|
||||
DeleteReaction(bot, channel, msgid, emoji, user);
|
||||
}
|
||||
|
||||
public int Native_DiscordBot_GetReaction(Handle plugin, int numParams) {
|
||||
DiscordBot bot = GetNativeCell(1);
|
||||
|
||||
char channel[32];
|
||||
GetNativeString(2, channel, sizeof(channel));
|
||||
|
||||
char msgid[64];
|
||||
GetNativeString(3, msgid, sizeof(msgid));
|
||||
|
||||
char emoji[128];
|
||||
GetNativeString(4, emoji, sizeof(emoji));
|
||||
|
||||
OnGetReactions fCallback = GetNativeCell(5);
|
||||
Handle fForward = null;
|
||||
if(fCallback != INVALID_FUNCTION) {
|
||||
fForward = CreateForward(ET_Ignore, Param_Cell, Param_Cell, Param_String, Param_String, Param_String, Param_String, Param_Cell);
|
||||
AddToForward(fForward, plugin, fCallback);
|
||||
}
|
||||
|
||||
any data = GetNativeCell(6);
|
||||
|
||||
GetReaction(bot, channel, msgid, emoji, fForward, data);
|
||||
}
|
||||
|
||||
///channels/{channel.id}/messages/{message.id}/reactions/{emoji}/@me
|
||||
public void AddReaction(DiscordBot bot, char[] channel, char[] messageid, char[] emoji) {
|
||||
char url[256];
|
||||
FormatEx(url, sizeof(url), "channels/%s/messages/%s/reactions/%s/@me", channel, messageid, emoji);
|
||||
|
||||
Handle request = PrepareRequest(bot, url, k_EHTTPMethodPUT, null, AddReactionReceiveData);
|
||||
|
||||
DataPack dp = new DataPack();
|
||||
WritePackCell(dp, bot);
|
||||
WritePackString(dp, channel);
|
||||
WritePackString(dp, messageid);
|
||||
WritePackString(dp, emoji);
|
||||
|
||||
if(request == dp) {
|
||||
CreateTimer(2.0, AddReactionDelayed, dp);
|
||||
return;
|
||||
}
|
||||
|
||||
char route[128];
|
||||
FormatEx(route, sizeof(route), "channels/%s/messages/msgid/reactions", channel);
|
||||
|
||||
SteamWorks_SetHTTPRequestContextValue(request, dp, UrlToDP(route));
|
||||
|
||||
DiscordSendRequest(request, url);
|
||||
}
|
||||
|
||||
public Action AddReactionDelayed(Handle timer, any data) {
|
||||
DataPack dp = view_as<DataPack>(data);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
char channel[64];
|
||||
char messageid[64];
|
||||
char emoji[64];
|
||||
ReadPackString(dp, channel, sizeof(channel));
|
||||
ReadPackString(dp, messageid, sizeof(messageid));
|
||||
ReadPackString(dp, emoji, sizeof(emoji));
|
||||
delete dp;
|
||||
|
||||
AddReaction(bot, channel, messageid, emoji);
|
||||
}
|
||||
|
||||
public AddReactionReceiveData(Handle request, bool failure, int offset, int statuscode, any data) {
|
||||
if(failure || statuscode != 204) {
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
|
||||
DataPack dp = view_as<DataPack>(data);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
char channel[64];
|
||||
char messageid[64];
|
||||
char emoji[64];
|
||||
ReadPackString(dp, channel, sizeof(channel));
|
||||
ReadPackString(dp, messageid, sizeof(messageid));
|
||||
ReadPackString(dp, emoji, sizeof(emoji));
|
||||
delete dp;
|
||||
|
||||
AddReaction(bot, channel, messageid, emoji);
|
||||
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't Add Reaction - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
delete view_as<Handle>(data);
|
||||
delete view_as<Handle>(data);
|
||||
return;
|
||||
}
|
||||
delete request;
|
||||
delete view_as<Handle>(data);
|
||||
}
|
||||
|
||||
///channels/{channel.id}/messages/{message.id}/reactions/{emoji}/{user.id}
|
||||
public void DeleteReaction(DiscordBot bot, char[] channel, char[] messageid, char[] emoji, char[] userid) {
|
||||
char url[256];
|
||||
|
||||
if(StrEqual(userid, "@all")) {
|
||||
FormatEx(url, sizeof(url), "channels/%s/messages/%s/reactions/%s", channel, messageid, emoji);
|
||||
}else {
|
||||
FormatEx(url, sizeof(url), "channels/%s/messages/%s/reactions/%s/%s", channel, messageid, emoji, userid);
|
||||
}
|
||||
|
||||
Handle request = PrepareRequest(bot, url, k_EHTTPMethodDELETE, null, DeleteReactionReceiveData);
|
||||
|
||||
DataPack dp = new DataPack();
|
||||
WritePackCell(dp, bot);
|
||||
WritePackString(dp, channel);
|
||||
WritePackString(dp, messageid);
|
||||
WritePackString(dp, emoji);
|
||||
WritePackString(dp, userid);
|
||||
|
||||
if(request == dp) {
|
||||
CreateTimer(2.0, DeleteReactionDelayed, dp);
|
||||
return;
|
||||
}
|
||||
|
||||
char route[128];
|
||||
FormatEx(route, sizeof(route), "channels/%s/messages/msgid/reactions", channel);
|
||||
|
||||
SteamWorks_SetHTTPRequestContextValue(request, dp, UrlToDP(route));
|
||||
|
||||
DiscordSendRequest(request, url);
|
||||
}
|
||||
|
||||
public Action DeleteReactionDelayed(Handle timer, any data) {
|
||||
DataPack dp = view_as<DataPack>(data);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
char channel[64];
|
||||
char messageid[64];
|
||||
char emoji[64];
|
||||
char userid[64];
|
||||
ReadPackString(dp, channel, sizeof(channel));
|
||||
ReadPackString(dp, messageid, sizeof(messageid));
|
||||
ReadPackString(dp, emoji, sizeof(emoji));
|
||||
ReadPackString(dp, userid, sizeof(userid));
|
||||
delete dp;
|
||||
|
||||
DeleteReaction(bot, channel, messageid, emoji, userid);
|
||||
}
|
||||
|
||||
public DeleteReactionReceiveData(Handle request, bool failure, int offset, int statuscode, any data) {
|
||||
if(failure || statuscode != 204) {
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
|
||||
DataPack dp = view_as<DataPack>(data);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
char channel[64];
|
||||
char messageid[64];
|
||||
char emoji[64];
|
||||
char userid[64];
|
||||
ReadPackString(dp, channel, sizeof(channel));
|
||||
ReadPackString(dp, messageid, sizeof(messageid));
|
||||
ReadPackString(dp, emoji, sizeof(emoji));
|
||||
ReadPackString(dp, userid, sizeof(userid));
|
||||
delete dp;
|
||||
|
||||
DeleteReaction(bot, channel, messageid, emoji, userid);
|
||||
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't Delete Reaction - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
delete view_as<Handle>(data);
|
||||
return;
|
||||
}
|
||||
delete request;
|
||||
delete view_as<Handle>(data);
|
||||
}
|
||||
|
||||
public void GetReaction(DiscordBot bot, char[] channel, char[] messageid, char[] emoji, Handle fForward, any data) {
|
||||
char url[256];
|
||||
FormatEx(url, sizeof(url), "channels/%s/messages/%s/reactions/%s", channel, messageid, emoji);
|
||||
|
||||
Handle request = PrepareRequest(bot, url, k_EHTTPMethodGET, null, GetReactionReceiveData);
|
||||
|
||||
DataPack dp = new DataPack();
|
||||
WritePackCell(dp, bot);
|
||||
WritePackString(dp, channel);
|
||||
WritePackString(dp, messageid);
|
||||
WritePackString(dp, emoji);
|
||||
WritePackCell(dp, fForward);
|
||||
WritePackCell(dp, data);
|
||||
|
||||
if(request == dp) {
|
||||
CreateTimer(2.0, GetReactionDelayed, dp);
|
||||
return;
|
||||
}
|
||||
|
||||
char route[128];
|
||||
FormatEx(route, sizeof(route), "channels/%s/messages/msgid/reactions", channel);
|
||||
|
||||
SteamWorks_SetHTTPRequestContextValue(request, dp, UrlToDP(route));
|
||||
|
||||
DiscordSendRequest(request, url);
|
||||
}
|
||||
|
||||
public Action GetReactionDelayed(Handle timer, any data) {
|
||||
DataPack dp = view_as<DataPack>(data);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
char channel[64];
|
||||
char messageid[64];
|
||||
char emoji[64];
|
||||
ReadPackString(dp, channel, sizeof(channel));
|
||||
ReadPackString(dp, messageid, sizeof(messageid));
|
||||
ReadPackString(dp, emoji, sizeof(emoji));
|
||||
Handle fForward = ReadPackCell(dp);
|
||||
any addData = ReadPackCell(dp);
|
||||
delete dp;
|
||||
|
||||
GetReaction(bot, channel, messageid, emoji, fForward, addData);
|
||||
}
|
||||
|
||||
public GetReactionReceiveData(Handle request, bool failure, int offset, int statuscode, any data) {
|
||||
if(failure || statuscode != 204) {
|
||||
if(statuscode == 429 || statuscode == 500) {
|
||||
|
||||
DataPack dp = view_as<DataPack>(data);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
char channel[64];
|
||||
char messageid[64];
|
||||
char emoji[64];
|
||||
ReadPackString(dp, channel, sizeof(channel));
|
||||
ReadPackString(dp, messageid, sizeof(messageid));
|
||||
ReadPackString(dp, emoji, sizeof(emoji));
|
||||
Handle fForward = ReadPackCell(dp);
|
||||
any addData = ReadPackCell(dp);
|
||||
delete dp;
|
||||
|
||||
GetReaction(bot, channel, messageid, emoji, fForward, addData);
|
||||
|
||||
delete request;
|
||||
return;
|
||||
}
|
||||
LogError("[DISCORD] Couldn't Delete Reaction - Fail %i %i", failure, statuscode);
|
||||
delete request;
|
||||
delete view_as<Handle>(data);
|
||||
return;
|
||||
}
|
||||
|
||||
SteamWorks_GetHTTPResponseBodyCallback(request, GetReactionsData, data);
|
||||
|
||||
delete request;
|
||||
}
|
||||
|
||||
public int GetReactionsData(const char[] data, any datapack) {
|
||||
DataPack dp = view_as<DataPack>(datapack);
|
||||
|
||||
DiscordBot bot = ReadPackCell(dp);
|
||||
char channel[64];
|
||||
char messageid[64];
|
||||
char emoji[64];
|
||||
ReadPackString(dp, channel, sizeof(channel));
|
||||
ReadPackString(dp, messageid, sizeof(messageid));
|
||||
ReadPackString(dp, emoji, sizeof(emoji));
|
||||
Handle fForward = ReadPackCell(dp);
|
||||
any addData = ReadPackCell(dp);
|
||||
delete dp;
|
||||
|
||||
Handle hJson = json_load(data);
|
||||
|
||||
ArrayList alUsers = new ArrayList();
|
||||
|
||||
if(json_is_array(hJson)) {
|
||||
for(int i = 0; i < json_array_size(hJson); i++) {
|
||||
DiscordUser user = view_as<DiscordUser>(json_array_get(hJson, i));
|
||||
alUsers.Push(user);
|
||||
}
|
||||
}
|
||||
delete hJson;
|
||||
|
||||
if(fForward != null) {
|
||||
Call_StartForward(fForward);
|
||||
Call_PushCell(bot);
|
||||
Call_PushCell(alUsers);
|
||||
Call_PushString(channel);
|
||||
Call_PushString(messageid);
|
||||
Call_PushString(emoji);
|
||||
Call_PushCell(addData);
|
||||
Call_Finish();
|
||||
}
|
||||
|
||||
for(int i = 0; i < alUsers.Length; i++) {
|
||||
DiscordUser user = alUsers.Get(i);
|
||||
delete user;
|
||||
}
|
||||
delete alUsers;
|
||||
}
|
Reference in New Issue
Block a user