-
-
Notifications
You must be signed in to change notification settings - Fork 52
Add 2 new exports: get Guild by role ID and Role name from role ID #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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..!', | ||
|
|
@@ -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 | ||
|
|
||
| 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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. guild should be an optional parameter?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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