feat: better guild structure

This commit is contained in:
Yuzu 2022-06-23 20:56:15 -05:00
parent 998fd7cb73
commit 84b288d469
3 changed files with 78 additions and 10 deletions

View File

@ -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
}

40
structures/BaseGuild.ts Normal file
View File

@ -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;
}
}

View File

@ -5,14 +5,16 @@ import type { DiscordGuild, DiscordMember, MakeRequired } from "../vendor/extern
import { DefaultMessageNotificationLevels, ExplicitContentFilterLevels, VerificationLevels } from "../vendor/external.ts"; import { DefaultMessageNotificationLevels, ExplicitContentFilterLevels, VerificationLevels } from "../vendor/external.ts";
import { iconHashToBigInt, iconBigintToHash as _iconBigintToHash } from "../util/hash.ts"; import { iconHashToBigInt, iconBigintToHash as _iconBigintToHash } from "../util/hash.ts";
import { Member } from "./Member.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) { constructor(session: Session, data: DiscordGuild) {
this.session = session; super(session, data);
this.id = data.id;
this.name = data.name;
this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined;
this.splashHash = data.splash ? iconHashToBigInt(data.splash) : undefined; this.splashHash = data.splash ? iconHashToBigInt(data.splash) : undefined;
this.discoverySplashHash = data.discovery_splash ? iconHashToBigInt(data.discovery_splash) : undefined; this.discoverySplashHash = data.discovery_splash ? iconHashToBigInt(data.discovery_splash) : undefined;
this.ownerId = data.owner_id; 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<DiscordMember, "user">)) ?? []; this.members = data.members?.map((member) => new Member(session, member as MakeRequired<DiscordMember, "user">)) ?? [];
} }
readonly session: Session;
readonly id: Snowflake;
name: string;
iconHash?: bigint;
splashHash?: bigint; splashHash?: bigint;
discoverySplashHash?: bigint; discoverySplashHash?: bigint;
ownerId: Snowflake; ownerId: Snowflake;