mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-01 20:46:08 +00:00
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import type { Model } from "./Base.ts";
|
|
import type { Snowflake } from "../util/Snowflake.ts";
|
|
import type { Session } from "../session/Session.ts";
|
|
import type { DiscordUser } from "../vendor/external.ts";
|
|
import type { ImageFormat, ImageSize } from "../util/shared/images.ts";
|
|
import { iconBigintToHash, iconHashToBigInt } from "../util/hash.ts";
|
|
import * as Routes from "../util/Routes.ts";
|
|
|
|
/**
|
|
* Represents a user
|
|
* @link https://discord.com/developers/docs/resources/user#user-object
|
|
*/
|
|
export class User implements Model {
|
|
constructor(session: Session, data: DiscordUser) {
|
|
this.session = session;
|
|
this.id = data.id;
|
|
|
|
this.username = data.username;
|
|
this.discriminator = data.discriminator;
|
|
this.avatarHash = data.avatar ? iconHashToBigInt(data.avatar) : undefined;
|
|
this.accentColor = data.accent_color;
|
|
this.bot = !!data.bot;
|
|
this.system = !!data.system;
|
|
this.banner = data.banner;
|
|
}
|
|
|
|
readonly session: Session;
|
|
readonly id: Snowflake;
|
|
|
|
username: string;
|
|
discriminator: string;
|
|
avatarHash?: bigint;
|
|
accentColor?: number;
|
|
bot: boolean;
|
|
system: boolean;
|
|
banner?: string;
|
|
|
|
/** gets the user's username#discriminator */
|
|
get tag() {
|
|
return `${this.username}#${this.discriminator}}`;
|
|
}
|
|
|
|
/** gets the user's avatar */
|
|
avatarUrl(options: { format?: ImageFormat; size?: ImageSize } = { size: 128 }) {
|
|
let url: string;
|
|
|
|
if (!this.avatarHash) {
|
|
url = Routes.USER_DEFAULT_AVATAR(Number(this.discriminator) % 5);
|
|
} else {
|
|
url = Routes.USER_AVATAR(this.id, iconBigintToHash(this.avatarHash));
|
|
}
|
|
|
|
return `${url}.${options.format ?? (url.includes("/a_") ? "gif" : "jpg")}?size=${options.size}`;
|
|
}
|
|
|
|
toString() {
|
|
return `<@${this.id}>`;
|
|
}
|
|
}
|
|
|
|
export default User;
|