diff --git a/structures/AnonymousGuild.ts b/structures/AnonymousGuild.ts new file mode 100644 index 0000000..649809b --- /dev/null +++ b/structures/AnonymousGuild.ts @@ -0,0 +1,31 @@ +import type { Model } from "./Base.ts"; +import type { Session } from "../session/Session.ts"; +import type { DiscordGuild, GuildNsfwLevel, VerificationLevels } from "../vendor/external.ts"; +import { iconHashToBigInt } from "../util/hash.ts"; +import { BaseGuild } from "./BaseGuild.ts"; + +export abstract class AnonymousGuild extends BaseGuild implements Model { + constructor(session: Session, data: DiscordGuild) { + super(session, data); + + this.splashHash = data.splash ? iconHashToBigInt(data.splash) : undefined; + this.bannerHash = data.banner ? iconHashToBigInt(data.banner) : undefined; + + this.verificationLevel = data.verification_level; + this.vanityUrlCode = data.vanity_url_code ? data.vanity_url_code : undefined; + this.nsfwLevel = data.nsfw_level; + this.description = data.description ? data.description : undefined; + this.premiumSubscriptionCount = data.premium_subscription_count; + } + + splashHash?: bigint; + bannerHash?: bigint; + + verificationLevel: VerificationLevels; + vanityUrlCode?: string; + nsfwLevel: GuildNsfwLevel; + description?: string; + premiumSubscriptionCount?: number; + + // TODO: bannerUrl and splashUrl +} diff --git a/structures/BaseGuild.ts b/structures/BaseGuild.ts new file mode 100644 index 0000000..4c1d24b --- /dev/null +++ b/structures/BaseGuild.ts @@ -0,0 +1,40 @@ +import type { Model } from "./Base.ts"; +import type { Session } from "../session/Session.ts"; +import type { DiscordGuild, GuildFeatures } from "../vendor/external.ts"; +import { Snowflake } from "../util/Snowflake.ts"; +import { iconHashToBigInt } from "../util/hash.ts"; + +/** + * Class for {@link Guild} and {@link AnonymousGuild} + * */ +export abstract class BaseGuild implements Model { + constructor(session: Session, data: DiscordGuild) { + this.session = session; + this.id = data.id; + + this.name = data.name; + this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined; + + this.features = data.features; + } + + readonly session: Session; + readonly id: Snowflake; + + name: string; + iconHash?: bigint; + features: GuildFeatures[]; + + get createdTimestamp() { + return Snowflake.snowflakeToTimestamp(this.id); + } + + get createdAt() { + return new Date(this.createdTimestamp); + } + + toString() { + return this.name; + } +} + diff --git a/structures/Guild.ts b/structures/Guild.ts index 803a69d..98de437 100644 --- a/structures/Guild.ts +++ b/structures/Guild.ts @@ -5,14 +5,16 @@ import type { DiscordGuild, DiscordMember, MakeRequired } from "../vendor/extern import { DefaultMessageNotificationLevels, ExplicitContentFilterLevels, VerificationLevels } from "../vendor/external.ts"; import { iconHashToBigInt, iconBigintToHash as _iconBigintToHash } from "../util/hash.ts"; import { Member } from "./Member.ts"; +import { BaseGuild } from "./BaseGuild.ts"; -export class Guild implements Model { +/** + * Represents a guild + * @link https://discord.com/developers/docs/resources/guild#guild-object + * */ +export class Guild extends BaseGuild implements Model { constructor(session: Session, data: DiscordGuild) { - this.session = session; - this.id = data.id; + super(session, data); - this.name = data.name; - this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined; this.splashHash = data.splash ? iconHashToBigInt(data.splash) : undefined; this.discoverySplashHash = data.discovery_splash ? iconHashToBigInt(data.discovery_splash) : undefined; this.ownerId = data.owner_id; @@ -24,11 +26,6 @@ export class Guild implements Model { this.members = data.members?.map((member) => new Member(session, member as MakeRequired)) ?? []; } - readonly session: Session; - readonly id: Snowflake; - - name: string; - iconHash?: bigint; splashHash?: bigint; discoverySplashHash?: bigint; ownerId: Snowflake;