From 88f1614dc300f429885a1e7e2b84ad45c8ff7c9f Mon Sep 17 00:00:00 2001 From: ckohen Date: Mon, 1 Aug 2022 01:56:15 -0700 Subject: [PATCH] feat: initial User structure, for comments --- packages/structures/package.json | 2 + packages/structures/src/index.ts | 1 + packages/structures/src/users/Connection.ts | 76 ++++++++++ packages/structures/src/users/User.ts | 149 ++++++++++++++++++++ packages/structures/src/users/index.ts | 2 + yarn.lock | 2 + 6 files changed, 232 insertions(+) create mode 100644 packages/structures/src/users/Connection.ts create mode 100644 packages/structures/src/users/User.ts create mode 100644 packages/structures/src/users/index.ts diff --git a/packages/structures/package.json b/packages/structures/package.json index 87cad5dc01b66..998842a5b9dd6 100644 --- a/packages/structures/package.json +++ b/packages/structures/package.json @@ -52,7 +52,9 @@ }, "homepage": "https://discord.js.org", "dependencies": { + "@sapphire/snowflake": "^3.2.2", "discord-api-types": "^0.36.3", + "ts-mixer": "^6.0.1", "tslib": "^2.4.0" }, "devDependencies": { diff --git a/packages/structures/src/index.ts b/packages/structures/src/index.ts index e69de29bb2d1d..b18bf4caef965 100644 --- a/packages/structures/src/index.ts +++ b/packages/structures/src/index.ts @@ -0,0 +1 @@ +export * from './users'; diff --git a/packages/structures/src/users/Connection.ts b/packages/structures/src/users/Connection.ts new file mode 100644 index 0000000000000..703d0c736d815 --- /dev/null +++ b/packages/structures/src/users/Connection.ts @@ -0,0 +1,76 @@ +import type { APIConnection } from 'discord-api-types/v10'; + +/** + * Represents a user's connection on Discord. + */ +export class Connection { + public constructor( + /** + * The raw data received from the API for the connection + */ + protected raw: APIConnection, + ) {} + + /** + * The id of the connection account + */ + public get id() { + return this.raw.id; + } + + /** + * The username of the connection account + */ + public get name() { + return this.raw.name; + } + + /** + * The type of service this connection is for + */ + public get type() { + return this.raw.type; + } + + /** + * Whether the connection is revoked + */ + public get revoked() { + return this.raw.revoked ?? false; + } + + /** + * Any integrations associated with this connection + */ + public get integrations() { + return this.raw.integrations ?? null; + } + + /** + * Whether the connection is verified + */ + public get verified() { + return this.raw.verified; + } + + /** + * Whether friend sync is enabled for this connection + */ + public get friendSync() { + return this.raw.friend_sync; + } + + /** + * Whether activities related to this connection are shown in the users presence + */ + public get showActivity() { + return this.raw.show_activity; + } + + /** + * The visibilty state for this connection + */ + public get visibility() { + return this.raw.visibility; + } +} diff --git a/packages/structures/src/users/User.ts b/packages/structures/src/users/User.ts new file mode 100644 index 0000000000000..eb0bad767ba6b --- /dev/null +++ b/packages/structures/src/users/User.ts @@ -0,0 +1,149 @@ +import { DiscordSnowflake } from '@sapphire/snowflake'; +import type { APIUser, Snowflake } from 'discord-api-types/v10'; + +/** + * Represents any user on Discord. + */ +export class User { + public constructor( + /** + * The raw data received from the API for the user + */ + protected raw: APIUser, + ) {} + + /** + * The user's id + */ + public get id(): Snowflake { + return this.raw.id; + } + + /** + * The username of the user + */ + public get username() { + return this.raw.username; + } + + /** + * The user's 4 digit tag, in combination with the username can uniquely identify the user + */ + public get discriminator() { + return this.raw.discriminator; + } + + /** + * The user avatar's hash + */ + public get avatar() { + return this.raw.avatar; + } + + /** + * Whether the user is a bot + */ + public get bot() { + return this.raw.bot ?? false; + } + + /** + * Whether the user is an Official Discord System user + */ + public get system() { + return this.raw.system ?? false; + } + + /** + * Whether the user has mfa enabled + * This property is only set when the user was fetched with an OAuth2 token and the `identify` scope + */ + public get mfaEnabled() { + return this.raw.mfa_enabled; + } + + /** + * The user's banner hash + * This property is only set when the user was manually fetched + */ + public get banner() { + return this.raw.banner; + } + + /** + * The base 10 accent color of the user's banner + * This property is only set when the user was manually fetched + */ + public get accentColor() { + return this.raw.accent_color; + } + + /** + * The user's primary discord language + * This property is only set when the user was fetched with an Oauth2 token and the `identify` scope + */ + public get locale() { + return this.raw.locale; + } + + /** + * Whether the email on the user's account has been verified + * This property is only set when the user was fetched with an OAuth2 token and the `email` scope + */ + public get verified() { + return this.raw.verified; + } + + /** + * The user's email + * This property is only set when the user was fetched with an OAuth2 token and the `email` scope + */ + public get email() { + return this.raw.email; + } + + /** + * The type of nitro subscription on the user's account + * This property is only set when the user was fetched with an OAuth2 token and the `identify` scope + */ + public get premiumType() { + return this.raw.premium_type; + } + + /** + * The flags for the user + */ + public get flags() { + return this.raw.public_flags; + } + + /** + * The timestamp the user was created at + */ + public get createdTimestamp() { + return DiscordSnowflake.timestampFrom(this.id); + } + + /** + * The time the user was created at + */ + public get createdAt() { + return new Date(this.createdTimestamp); + } + + /** + * The hexadecimal version of the user accent color, with a leading hash + * This property is only set when the user was manually fetched + */ + public get hexAccentColor() { + if (typeof this.accentColor !== 'number') return this.accentColor; + return `#${this.accentColor.toString(16).padStart(6, '0')}`; + } + + /** + * The Discord "tag" (e.g. `hydrabolt#0001`) for this user + */ + public get tag() { + return `${this.username}#${this.discriminator}`; + } +} diff --git a/packages/structures/src/users/index.ts b/packages/structures/src/users/index.ts new file mode 100644 index 0000000000000..9e5646ccd73c8 --- /dev/null +++ b/packages/structures/src/users/index.ts @@ -0,0 +1,2 @@ +export * from './Connection'; +export * from './User'; diff --git a/yarn.lock b/yarn.lock index f323a534732f8..1c9c649927304 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1893,12 +1893,14 @@ __metadata: "@discordjs/docgen": "workspace:^" "@favware/cliff-jumper": ^1.8.5 "@microsoft/api-extractor": ^7.28.6 + "@sapphire/snowflake": ^3.2.2 "@types/node": ^16.11.46 c8: ^7.12.0 discord-api-types: ^0.36.3 eslint: ^8.20.0 prettier: ^2.7.1 rollup-plugin-typescript2: 0.32.1 + ts-mixer: ^6.0.1 tslib: ^2.4.0 typescript: ^4.7.4 unbuild: ^0.7.6