Hi,
using this plugin
https://forums.alliedmods.net/showthread.php?t=292448 and I wanted to add a way to retrieve the guild specific Nickname for a given user now there is
GetGuildMember in the Discord API but I'm not familiar enough with the plugin nor Discords API to know how I'm supposed to return a DiscordGuildUser object.
native is supposed to look something like
PHP Code:
native DiscordGuildUser GetGuildMemberByUserID(char[] userID);
What I tried is basically replicating what the existing code does but I don't fully know how to proceed.
Native is implemented as
PHP Code:
public int Native_DiscordBot_GetBuildMemberByUser(Handle plugin, int numParams)
{
DiscordBot bot = view_as<DiscordBot>(CloneHandle(GetNativeCell(1)));
char guild[32];
GetNativeString(2, guild, sizeof(guild));
char userID[32];
GetNativeString(3, userID, sizeof(userID));
Handle hData = json_object();
json_object_set_new(hData, "bot", bot);
json_object_set_new(hData, "guild", guild);
json_object_set_new(hData, "user", userID);
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)));
GetMember(hData);
return 1;
}
GetMember has this implementation:
PHP Code:
static void GetMember(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 userID[32];
JsonObjectGetString(hData, "userID", userID, sizeof(userID));
char url[256];
FormatEx(url, sizeof(url), "https://discord.com/api/guilds/%s/members/%s", guild, userID);
char route[128];
// I don't even know if this is correct or anything
FormatEx(route, sizeof(route), "guild/%s/members/%s", guild, userID);
DiscordRequest request = new DiscordRequest(url, k_EHTTPMethodGET);
if(request == null) {
delete bot;
CreateTimer(2.0, SendGetMember, hData);
return;
}
request.SetCallbacks(HTTPCompleted, MemberDataReceive);
request.SetBot(bot);
request.SetData(hData, route);
request.Send(route);
delete bot;
}
Member Data is declared as
PHP Code:
public MemberDataReceive(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 GetMember - Fail %i %i", failure, statuscode);
delete request;
delete view_as<Handle>(dp);
return;
}
SteamWorks_GetHTTPResponseBodyCallback(request, GetMemberData, dp);
delete request;
}
GetMemberData declaration:
PHP Code:
public int GetMemberData(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));
json_object
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 1;
}
}
delete hJson;
delete hData;
delete fwd;
return 0;
}
Doing it manually be retrieving all Guild Members does not work as I'm getting this error when attempt to retrieve the guild members list:
PHP Code:
L 07/20/2023 - 17:18:40: [SM] Exception reported: Not enough space on the heap
L 07/20/2023 - 17:18:40: [SM] Blaming: discord_api.smx
L 07/20/2023 - 17:18:40: [SM] Call stack trace:
L 07/20/2023 - 17:18:40: [SM] [0] SteamWorks_GetHTTPResponseBodyCallback
L 07/20/2023 - 17:18:40: [SM] [1] Line 117, discord\GuildMembers.sp::MembersDataReceive