Skip to content

Commit

Permalink
feat: initial User structure, for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ckohen committed Aug 3, 2022
1 parent dd9e475 commit 88f1614
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/structures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions packages/structures/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './users';
76 changes: 76 additions & 0 deletions packages/structures/src/users/Connection.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
149 changes: 149 additions & 0 deletions packages/structures/src/users/User.ts
Original file line number Diff line number Diff line change
@@ -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
* <info>This property is only set when the user was fetched with an OAuth2 token and the `identify` scope</info>
*/
public get mfaEnabled() {
return this.raw.mfa_enabled;
}

/**
* The user's banner hash
* <info>This property is only set when the user was manually fetched</info>
*/
public get banner() {
return this.raw.banner;
}

/**
* The base 10 accent color of the user's banner
* <info>This property is only set when the user was manually fetched</info>
*/
public get accentColor() {
return this.raw.accent_color;
}

/**
* The user's primary discord language
* <info>This property is only set when the user was fetched with an Oauth2 token and the `identify` scope</info>
*/
public get locale() {
return this.raw.locale;
}

/**
* Whether the email on the user's account has been verified
* <info>This property is only set when the user was fetched with an OAuth2 token and the `email` scope</info>
*/
public get verified() {
return this.raw.verified;
}

/**
* The user's email
* <info>This property is only set when the user was fetched with an OAuth2 token and the `email` scope</info>
*/
public get email() {
return this.raw.email;
}

/**
* The type of nitro subscription on the user's account
* <info>This property is only set when the user was fetched with an OAuth2 token and the `identify` scope</info>
*/
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
* <info>This property is only set when the user was manually fetched</info>
*/
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}`;
}
}
2 changes: 2 additions & 0 deletions packages/structures/src/users/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Connection';
export * from './User';
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 88f1614

Please sign in to comment.