Skip to content
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

Add support for Badges #412

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions libs/client/Resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions libs/containers/User.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that this method works differently compared to something like Message:hasFlag(), which checks for the presence of any of the provided flags. Might want to change it to how Message does it, or vise versa.

end

--[=[
@m getPrivateChannel
@t http
Expand Down Expand Up @@ -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.]=]
Copy link
Contributor

Choose a reason for hiding this comment

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

This should either say table or it should return an ArrayIterable instead of a table (I recommend this), Array on its own is not a valid type.

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
26 changes: 26 additions & 0 deletions libs/enums.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down