Skip to content
Permalink
Browse files

Add discord_api plugin

This should prevent errors like #440
  • Loading branch information
Bara committed Oct 19, 2019
1 parent f070b04 commit e14da1d88d3553ae2ecac174f77af7afaa95afd6
@@ -34,6 +34,8 @@ jobs:
run: |
echo -e "\nCompiling addons/sourcemod/scripting/block_messages.sp..."
addons/sourcemod/scripting/spcomp -E -w234 -O2 -v2 addons/sourcemod/scripting/block_messages.sp
echo -e "\nCompiling addons/sourcemod/scripting/discord_api.sp..."
addons/sourcemod/scripting/spcomp -E -w234 -O2 -v2 addons/sourcemod/scripting/discord_api.sp
echo -e "Compiling addons/sourcemod/scripting/ghostdm.sp..."
addons/sourcemod/scripting/spcomp -E -w234 -O2 -v2 addons/sourcemod/scripting/ghostdm.sp
echo -e "\nCompiling addons/sourcemod/scripting/no_weapon_fix.sp..."
@@ -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);
}
}
@@ -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;
}

0 comments on commit e14da1d

Please sign in to comment.
You can’t perform that action at this time.