From 2e5a6476d5752299bd6637474fb635264aa0d783 Mon Sep 17 00:00:00 2001 From: Carter <45381083+Fyko@users.noreply.github.com> Date: Thu, 16 Apr 2020 02:32:15 -0600 Subject: [PATCH] feat: User#flags (#4060) * feat: user flags * fix: unnecessary negated statement * fix: wording for description * fix: an vs. a * feat: add verified bot and dev flags Co-Authored-By: Vlad Frangu * typings :verified bot and dev flags Co-Authored-By: Vlad Frangu * feat: mon's suggestion, async fetchFlags & jsdoc * feat: added to index.js * fix: typo * style: leveled flags * typings: update leveled flags Co-authored-by: Vlad Frangu --- src/index.js | 1 + src/structures/User.js | 18 +++++++++++++++ src/util/UserFlags.js | 51 ++++++++++++++++++++++++++++++++++++++++++ typings/index.d.ts | 21 +++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 src/util/UserFlags.js diff --git a/src/index.js b/src/index.js index a8b3969d872c..edce6e8d49ea 100644 --- a/src/index.js +++ b/src/index.js @@ -28,6 +28,7 @@ module.exports = { SnowflakeUtil: require('./util/Snowflake'), Structures: require('./util/Structures'), SystemChannelFlags: require('./util/SystemChannelFlags'), + UserFlags: require('./util/UserFlags'), Util: Util, version: require('../package.json').version, diff --git a/src/structures/User.js b/src/structures/User.js index e34a9e953eb8..b2a6500f4bd0 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -5,6 +5,7 @@ const { Presence } = require('./Presence'); const TextBasedChannel = require('./interfaces/TextBasedChannel'); const { Error } = require('../errors'); const Snowflake = require('../util/Snowflake'); +const UserFlags = require('../util/UserFlags'); /** * Represents a user on Discord. @@ -73,6 +74,12 @@ class User extends Base { */ if (data.locale) this.locale = data.locale; + /** + * The flags for this user + * @type {?UserFlags} + */ + if (typeof data.public_flags !== 'undefined') this.flags = new UserFlags(data.public_flags); + /** * The ID of the last message sent by the user, if one was sent * @type {?Snowflake} @@ -255,6 +262,17 @@ class User extends Base { return equal; } + /** + * Fetches this user's flags. + * @returns {Promise} + */ + async fetchFlags() { + if (this.flags) return this.flags; + const data = await this.client.api.users(this.id).get(); + this._patch(data); + return this.flags; + } + /** * Fetches this user. * @returns {Promise} diff --git a/src/util/UserFlags.js b/src/util/UserFlags.js new file mode 100644 index 000000000000..120cb4f16df8 --- /dev/null +++ b/src/util/UserFlags.js @@ -0,0 +1,51 @@ +'use strict'; +const BitField = require('./BitField'); + +/** + * Data structure that makes it easy to interact with a {@link User#flags} bitfield. + * @extends {BitField} + */ +class UserFlags extends BitField {} + +/** + * @name UserFlags + * @kind constructor + * @memberof UserFlags + * @param {BitFieldResolvable} [bits=0] Bit(s) to read from + */ + +/** + * Numeric user flags. All available properties: + * * `DISCORD_EMPLOYEE` + * * `DISCORD_PARTNER` + * * `HYPESQUAD_EVENTS` + * * `BUGHUNTER_LEVEL_1` + * * `HOUSE_BRAVERY` + * * `HOUSE_BRILLIANCE` + * * `HOUSE_BALANCE` + * * `EARLY_SUPPORTER` + * * `TEAM_USER` + * * `SYSTEM` + * * `BUGHUNTER_LEVEL_2` + * * `VERIFIED_BOT` + * * `VERIFIED_DEVELOPER` + * @type {Object} + * @see {@link https://discordapp.com/developers/docs/resources/user#user-object-user-flags} + */ +UserFlags.FLAGS = { + DISCORD_EMPLOYEE: 1 << 0, + DISCORD_PARTNER: 1 << 1, + HYPESQUAD_EVENTS: 1 << 2, + BUGHUNTER_LEVEL_1: 1 << 3, + HOUSE_BRAVERY: 1 << 6, + HOUSE_BRILLIANCE: 1 << 7, + HOUSE_BALANCE: 1 << 8, + EARLY_SUPPORTER: 1 << 9, + TEAM_USER: 1 << 10, + SYSTEM: 1 << 12, + BUGHUNTER_LEVEL_2: 1 << 14, + VERIFIED_BOT: 1 << 16, + VERIFIED_DEVELOPER: 1 << 17, +}; + +module.exports = UserFlags; diff --git a/typings/index.d.ts b/typings/index.d.ts index 3e62b4966e1e..f6d4be86d5bf 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1469,6 +1469,7 @@ declare module 'discord.js' { public discriminator: string; public readonly defaultAvatarURL: string; public readonly dmChannel: DMChannel; + public flags: Readonly; public id: Snowflake; public lastMessageID: Snowflake | null; public locale: string; @@ -1489,6 +1490,11 @@ declare module 'discord.js' { public typingSinceIn(channel: ChannelResolvable): Date; } + export class UserFlags extends BitField { + public static FLAGS: Record; + public static resolve(bit?: BitFieldResolvable): number; + } + export class Util { public static basename(path: string, ext?: string): string; public static binaryToID(num: string): Snowflake; @@ -2978,6 +2984,21 @@ declare module 'discord.js' { timeout: NodeJS.Timeout; } + type UserFlagsString = + | 'DISCORD_EMPLOYEE' + | 'DISCORD_PARTNER' + | 'HYPESQUAD_EVENTS' + | 'BUGHUNTER_LEVEL_1' + | 'HOUSE_BRAVERY' + | 'HOUSE_BRILLIANCE' + | 'HOUSE_BALANCE' + | 'EARLY_SUPPORTER' + | 'TEAM_USER' + | 'SYSTEM' + | 'BUGHUNTER_LEVEL_2' + | 'VERIFIED_BOT' + | 'VERIFIED_DEVELOPER'; + type UserResolvable = User | Snowflake | Message | GuildMember; type VerificationLevel = 'NONE' | 'LOW' | 'MEDIUM' | 'HIGH' | 'VERY_HIGH';