From 06d71d846c33c2eb5db0e13eca9bfd958e34c5f9 Mon Sep 17 00:00:00 2001 From: ogn-server <61920219+coleminer0112@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:28:59 -0400 Subject: [PATCH 1/3] add 2 functions FindGuildForRole(roleId) RoleNameFromId(id, guild) --- fxmanifest.lua | 4 +++- server.lua | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/fxmanifest.lua b/fxmanifest.lua index 93e08aa..55cad56 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -40,5 +40,7 @@ server_exports { "RemoveRole", "ChangeDiscordVoice", "ClearCache", - "FetchRoleID" + "FetchRoleID", + "FindGuildForRole", + "RoleNameFromId" } diff --git a/server.lua b/server.lua index cc6f731..da1298b 100644 --- a/server.lua +++ b/server.lua @@ -1,4 +1,6 @@ local FormattedToken = "Bot " .. Config.Bot_Token +local roleGuilds = {} +local roleNames = {} local error_codes_defined = { [200] = 'OK - The request was completed successfully..!', @@ -399,6 +401,61 @@ function GetGuildRoleList(guild --[[optional]]) end recent_role_cache = {} +function FindGuildForRole(roleId) + local roleFound = false + local tempGuildList = Config.Guilds + tempGuildList["main"] = Config.Guild_ID + + if roleGuilds[tostring(roleId)] then + return roleGuilds[tostring(roleId)] + end + + for _,id in pairs(tempGuildList) do + local guild = DiscordRequest("GET", "guilds/"..id, {}) + if guild.code == 200 then + local data = json.decode(guild.data) + local roles = data.roles; + for i = 1, #roles do + if tostring(roles[i].id) == tostring(roleId) then + roleGuilds[tostring(roleId)] = id + roleFound = id + break + end + end + + if roleFound then + break + end + else + print("[Badger_Perms] An error occured, please check your config and ensure everything is correct. Error: "..(guild.data or guild.code)) + end + end + + return roleFound +end + +function RoleNameFromId(id, guild) + local guildRoles = false + local roleName = 'N/A' + + if roleNames[tostring(id)] then + return roleNames[tostring(id)] + end + + guildRoles = GetGuildRoleList(guild) + + if guildRoles then + for k, v in pairs(guildRoles) do + if tostring(v) == tostring(id) then + roleNames[tostring(id)] = k + roleName = k + break + end + end + end + + return roleName +end function ClearCache(discordId) if (discordId ~= nil) then From 199bb93e6e500d2774028713e35b3ae66e0f12a3 Mon Sep 17 00:00:00 2001 From: James Cole <61920219+coleminer0112@users.noreply.github.com> Date: Mon, 23 Oct 2023 18:48:46 -0700 Subject: [PATCH 2/3] FindGuildForRole: minor fixes for compatibility --- server.lua | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/server.lua b/server.lua index da1298b..e362740 100644 --- a/server.lua +++ b/server.lua @@ -402,12 +402,16 @@ end recent_role_cache = {} function FindGuildForRole(roleId) - local roleFound = false + local roleFound local tempGuildList = Config.Guilds - tempGuildList["main"] = Config.Guild_ID + tempGuildList["main_guild_abcd1010"] = Config.Guild_ID - if roleGuilds[tostring(roleId)] then - return roleGuilds[tostring(roleId)] + if not roleId then + sendDebugMessage("[Badger_Discord_API] ERROR: Invalid usage of FindGuildForRole. Requires `roleId` arg.") + end + roleId = tostring(roleId) + if roleGuilds[roleId] then + return roleGuilds[roleId] end for _,id in pairs(tempGuildList) do @@ -416,8 +420,8 @@ function FindGuildForRole(roleId) local data = json.decode(guild.data) local roles = data.roles; for i = 1, #roles do - if tostring(roles[i].id) == tostring(roleId) then - roleGuilds[tostring(roleId)] = id + if tostring(roles[i].id) == roleId then + roleGuilds[roleId] = id roleFound = id break end @@ -427,7 +431,7 @@ function FindGuildForRole(roleId) break end else - print("[Badger_Perms] An error occured, please check your config and ensure everything is correct. Error: "..(guild.data or guild.code)) + sendDebugMessage("[Badger_Discord_API] ERROR: please check your config and ensure everything is correct. Error: "..tostring(guild.code)) end end From 6c94b5ba18fb497516f93fef37ff531d43eabdd2 Mon Sep 17 00:00:00 2001 From: James Cole <61920219+coleminer0112@users.noreply.github.com> Date: Mon, 23 Oct 2023 18:49:11 -0700 Subject: [PATCH 3/3] RoleNameFromId: minor fixes for compatibility --- server.lua | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/server.lua b/server.lua index e362740..30e5436 100644 --- a/server.lua +++ b/server.lua @@ -440,24 +440,35 @@ end function RoleNameFromId(id, guild) local guildRoles = false - local roleName = 'N/A' + local roleName - if roleNames[tostring(id)] then - return roleNames[tostring(id)] + if not id then + sendDebugMessage("[Badger_Discord_API] ERROR: Invalid usage of RoleNameFromId. Requires `id` arg.") + return roleName + end + id = tostring(id) + if roleNames[id] then + return roleNames[id] end - + if not guild then guild = FindGuildForRole(id) end + if not guild then + sendDebugMessage("[Badger_Discord_API] ERROR: RoleNameFromId could not find guild for role ["..id.."]") + end + guild = tostring(guild) guildRoles = GetGuildRoleList(guild) if guildRoles then for k, v in pairs(guildRoles) do - if tostring(v) == tostring(id) then - roleNames[tostring(id)] = k + if tostring(v) == id then + roleNames[id] = k roleName = k break end end end - + if not roleName then + sendDebugMessage("[Badger_Discord_API] ERROR: Could not find name for role ["..id.."] in guild ["..guild.."]") + end return roleName end