Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ server_exports {
"RemoveRole",
"ChangeDiscordVoice",
"ClearCache",
"FetchRoleID"
"FetchRoleID",
"FindGuildForRole",
"RoleNameFromId"
}
72 changes: 72 additions & 0 deletions server.lua
Original file line number Diff line number Diff line change
@@ -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..!',
Expand Down Expand Up @@ -399,6 +401,76 @@ function GetGuildRoleList(guild --[[optional]])
end

recent_role_cache = {}
function FindGuildForRole(roleId)
local roleFound
local tempGuildList = Config.Guilds
tempGuildList["main_guild_abcd1010"] = Config.Guild_ID
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, what is the need for this arbitrary key in the table?

Copy link
Contributor Author

@coleminer0112 coleminer0112 Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly to make it unlikely that a user would overwrite that key in the config by 'injecting' the main guild ID in a nonsensical key


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
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) == roleId then
roleGuilds[roleId] = id
roleFound = id
break
end
end

if roleFound then
break
end
else
sendDebugMessage("[Badger_Discord_API] ERROR: please check your config and ensure everything is correct. Error: "..tostring(guild.code))
end
end

return roleFound
end

function RoleNameFromId(id, guild)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guild should be an optional parameter?

Copy link
Contributor Author

@coleminer0112 coleminer0112 Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is - looking below the case of guild == nil is handled by defaulting to searching for the guild. Providing the guild would just make it more efficient.

local guildRoles = false
local roleName

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) == 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

function ClearCache(discordId)
if (discordId ~= nil) then
Expand Down