import type { Model } from "./Base.ts"; import type { Snowflake } from "../util/Snowflake.ts"; import type { Session } from "../session/Session.ts"; import type { DiscordGuild, DiscordRole } from "../vendor/external.ts"; import { DefaultMessageNotificationLevels, ExplicitContentFilterLevels, VerificationLevels } from "../vendor/external.ts"; import { iconHashToBigInt, iconBigintToHash } from "../util/hash.ts"; import { Member } from "./Member.ts"; import { BaseGuild } from "./BaseGuild.ts"; import { Role } from "./Role.ts"; import { Routes } from "../util/mod.ts"; export interface CreateRole { name?: string; color?: number; iconHash?: string | bigint; unicodeEmoji?: string; hoist?: boolean; mentionable?: boolean; } /** * 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) { super(session, data); this.splashHash = data.splash ? iconHashToBigInt(data.splash) : undefined; this.discoverySplashHash = data.discovery_splash ? iconHashToBigInt(data.discovery_splash) : undefined; this.ownerId = data.owner_id; this.widgetEnabled = !!data.widget_enabled; this.widgetChannelId = data.widget_channel_id ? data.widget_channel_id : undefined; this.vefificationLevel = data.verification_level; this.defaultMessageNotificationLevel = data.default_message_notifications; this.explicitContentFilterLevel = data.explicit_content_filter; this.members = data.members?.map((member) => new Member(session, { ...member, user: member.user! })) ?? []; this.roles = data.roles.map((role) => new Role(session, data.id, role)); } splashHash?: bigint; discoverySplashHash?: bigint; ownerId: Snowflake; widgetEnabled: boolean; widgetChannelId?: Snowflake; vefificationLevel: VerificationLevels; defaultMessageNotificationLevel: DefaultMessageNotificationLevels; explicitContentFilterLevel: ExplicitContentFilterLevels; members: Member[]; roles: Role[]; async createRole(options: CreateRole) { let icon: string | undefined; if (options.iconHash) { if (typeof options.iconHash === "string") { icon = options.iconHash; } else { icon = iconBigintToHash(options.iconHash); } } const role = await this.session.rest.runMethod( this.session.rest, "PUT", Routes.GUILD_ROLES(this.id), { name: options.name, color: options.color, icon, unicode_emoji: options.unicodeEmoji, hoist: options.hoist, mentionable: options.mentionable, } ); return new Role(this.session, this, role); } async deleteRole(roleId: Snowflake): Promise { await this.session.rest.runMethod(this.session.rest, "DELETE", Routes.GUILD_ROLE(this.id, roleId)); } }