Skip to content

Commit

Permalink
feat: User#flags (discordjs#4060)
Browse files Browse the repository at this point in the history
* 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 <kingdgrizzle@gmail.com>

* typings :verified bot and dev flags

Co-Authored-By: Vlad Frangu <kingdgrizzle@gmail.com>

* 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 <kingdgrizzle@gmail.com>
  • Loading branch information
Fyko and vladfrangu authored Apr 16, 2020
1 parent 72a33cb commit 2e5a647
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
18 changes: 18 additions & 0 deletions src/structures/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -255,6 +262,17 @@ class User extends Base {
return equal;
}

/**
* Fetches this user's flags.
* @returns {Promise<UserFlags>}
*/
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<User>}
Expand Down
51 changes: 51 additions & 0 deletions src/util/UserFlags.js
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 21 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,7 @@ declare module 'discord.js' {
public discriminator: string;
public readonly defaultAvatarURL: string;
public readonly dmChannel: DMChannel;
public flags: Readonly<UserFlags>;
public id: Snowflake;
public lastMessageID: Snowflake | null;
public locale: string;
Expand All @@ -1489,6 +1490,11 @@ declare module 'discord.js' {
public typingSinceIn(channel: ChannelResolvable): Date;
}

export class UserFlags extends BitField<UserFlagsString> {
public static FLAGS: Record<UserFlagsString, number>;
public static resolve(bit?: BitFieldResolvable<UserFlagsString>): number;
}

export class Util {
public static basename(path: string, ext?: string): string;
public static binaryToID(num: string): Snowflake;
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 2e5a647

Please sign in to comment.