diff --git a/libs/client/Resolver.lua b/libs/client/Resolver.lua index efe9544e..a17d9204 100644 --- a/libs/client/Resolver.lua +++ b/libs/client/Resolver.lua @@ -8,6 +8,7 @@ local permission = assert(enums.permission) local gatewayIntent = assert(enums.gatewayIntent) local actionType = assert(enums.actionType) local messageFlag = assert(enums.messageFlag) +local userFlag = assert(enums.userFlag) local base64 = ssl.base64 local readFileSync = fs.readFileSync local classes = class.classes @@ -211,6 +212,17 @@ function Resolver.messageFlag(obj) return n end +function Resolver.userFlag(obj) + local t = type(obj) + local n = nil + if t == 'string' then + n = userFlag[obj] + elseif t == 'number' then + n = userFlag(obj) and obj + end + return n +end + function Resolver.base64(obj) if type(obj) == 'string' then if obj:find('data:.*;base64,') == 1 then diff --git a/libs/containers/User.lua b/libs/containers/User.lua index a0ba9795..617f158e 100644 --- a/libs/containers/User.lua +++ b/libs/containers/User.lua @@ -7,8 +7,13 @@ specific guild's context. local Snowflake = require('containers/abstract/Snowflake') local FilteredIterable = require('iterables/FilteredIterable') local constants = require('constants') +local enums = require('enums') +local bit = require('bit') +local band = bit.band local format = string.format +local insert = table.insert + local DEFAULT_AVATARS = constants.DEFAULT_AVATARS local User, get = require('class')('User', Snowflake) @@ -57,6 +62,18 @@ function User:getDefaultAvatarURL(size) end end +--[=[ +@m hasBadge +@t mem +@p badge User-Flag-Resolvable +@r boolean +@d Indicates whether the user has the badge given. +]=] +function User:hasBadge(badge) + badge = Resolver.userFlag(badge) + return band(self._public_flags or 0, badge) == badge +end + --[=[ @m getPrivateChannel @t http @@ -202,4 +219,18 @@ function get.mutualGuilds(self) return self._mutual_guilds end +--[=[@p badges Array An array of all badges the user has, represented by the badge's name.]=] +function get.badges(self) + local badges = {} + local publicflags = self._public_flags + + for badge, flag in pairs(enums.userFlag) do + if band(publicflags, flag) == flag then + insert(badges, badge) + end + end + + return badges +end + return User diff --git a/libs/enums.lua b/libs/enums.lua index b148b561..3b2c6dd2 100644 --- a/libs/enums.lua +++ b/libs/enums.lua @@ -235,6 +235,32 @@ enums.messageFlag = enum { isVoiceMessage = flag(13), } +enums.userFlag = enum { + staff = flag(0), + partner = flag(1), + hypesquad = flag(2), + bugHunterLevel1 = flag(3), + -- unused = flag(4), + -- unused = flag(5), + houseBravery = flag(6), + houseBrilliance = flag(7), + houseBalance = flag(8), + earlyNitroSupporter = flag(9), + teamPseudoUser = flag(10), + -- unused = flag(11), + -- unused = flag(12), + -- unused = flag(13), + bugHunterLevel2 = flag(14), + -- unused = flag(15), + verifiedBot = flag(16), + verifiedDeveloper = flag(17), + certifiedModerator = flag(18), + botHttpInteractions = flag(19), + -- unused = flag(20), + -- unused = flag(21), + activeDeveloper = flag(22) +} + enums.gatewayIntent = enum { guilds = flag(0), guildMembers = flag(1), -- privileged