diff --git a/structures/AnonymousGuild.ts b/structures/AnonymousGuild.ts index cf3898e..f675669 100644 --- a/structures/AnonymousGuild.ts +++ b/structures/AnonymousGuild.ts @@ -4,7 +4,8 @@ import type { DiscordGuild, GuildNsfwLevel, VerificationLevels } from "../vendor import { iconHashToBigInt } from "../util/hash.ts"; import { BaseGuild } from "./BaseGuild.ts"; -export abstract class AnonymousGuild extends BaseGuild implements Model { +export class AnonymousGuild extends BaseGuild implements Model { + constructor(session: Session, data: Partial); // TODO: Improve this type (name and id are required) constructor(session: Session, data: DiscordGuild) { super(session, data); diff --git a/structures/Invite.ts b/structures/Invite.ts new file mode 100644 index 0000000..f83be87 --- /dev/null +++ b/structures/Invite.ts @@ -0,0 +1,67 @@ +import type { Session } from "../session/Session.ts"; +import type { DiscordInvite } from "../vendor/external.ts"; +import { TargetTypes } from "../vendor/external.ts"; +import InviteGuild from "./InviteGuild.ts"; +import User from "./User.ts"; + +/** + * @link https://discord.com/developers/docs/resources/invite#invite-object + */ +export class Invite { + constructor(session: Session, data: DiscordInvite) { + this.session = session; + + if (data.guild) { + this.guild = new InviteGuild(session, data.guild); + } + + if (data.approximate_member_count) { + this.approximateMemberCount = data.approximate_member_count; + } + + if (data.approximate_presence_count) { + this.approximatePresenceCount = data.approximate_presence_count; + } + + // TODO: fix this + // this.channel = data.channel; + this.code = data.code; + + if (data.expires_at) { + this.expiresAt = Number.parseInt(data.expires_at); + } + + // TODO: fix this + // this.xd = data.guild_scheduled_event + + if (data.inviter) { + this.inviter = new User(session, data.inviter); + } + + if (data.target_user) { + this.targetUser = new User(session, data.target_user); + } + + // TODO: fix this + // this.stageInstance = data.stage_instance + + // TODO: fix this + // this.targetApplication = data.target_application + + if (data.target_type) { + this.targetType = data.target_type; + } + } + + readonly session: Session; + guild?: InviteGuild; + approximateMemberCount?: number; + approximatePresenceCount?: number; + code: string; + expiresAt?: number; + inviter?: User; + targetUser?: User; + targeType?: TargetTypes; +} + +export default Invite; diff --git a/structures/InviteGuild.ts b/structures/InviteGuild.ts new file mode 100644 index 0000000..d120c93 --- /dev/null +++ b/structures/InviteGuild.ts @@ -0,0 +1,18 @@ +import type { Session } from "../session/Session.ts"; +import type { DiscordGuild } from "../vendor/external.ts"; +import AnonymousGuild from "./AnonymousGuild.ts"; +import WelcomeScreen from "./WelcomeScreen.ts"; + +export class InviteGuild extends AnonymousGuild { + constructor(session: Session, data: Partial) { + super(session, data); + + if (data.welcome_screen) { + this.welcomeScreen = new WelcomeScreen(session, data.welcome_screen); + } + } + + welcomeScreen?: WelcomeScreen; +} + +export default InviteGuild; diff --git a/structures/TextChannel.ts b/structures/TextChannel.ts index 0fa640a..63cfda1 100644 --- a/structures/TextChannel.ts +++ b/structures/TextChannel.ts @@ -1,11 +1,12 @@ import type { Session } from "../session/Session.ts"; import type { Snowflake } from "../util/Snowflake.ts"; import type { GetMessagesOptions } from "../util/Routes.ts"; -import type { DiscordChannel, DiscordInviteCreate, DiscordMessage } from "../vendor/external.ts"; +import type { DiscordChannel, DiscordInvite, DiscordMessage, TargetTypes } from "../vendor/external.ts"; import { GuildChannel } from "./GuildChannel.ts"; import { Guild } from "./Guild.ts"; import { ThreadChannel } from "./ThreadChannel.ts"; import { Message } from "./Message.ts"; +import { Invite } from "./Invite.ts"; import { Routes } from "../util/mod.ts"; /** @@ -13,11 +14,14 @@ import { Routes } from "../util/mod.ts"; * @link https://discord.com/developers/docs/resources/channel#create-channel-invite-json-params */ export interface DiscordInviteOptions { - max_age?: number; - max_uses?: number; + maxAge?: number; + maxUses?: number; unique?: boolean; temporary: boolean; reason?: string; + targetType?: TargetTypes; + targetUserId?: Snowflake; + targetApplicationId?: Snowflake; } /** @@ -54,14 +58,26 @@ export class TextChannel extends GuildChannel { ); return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : []; } - // TODO return Invite Class - createInvite(options?: DiscordInviteOptions) { - return this.session.rest.runMethod( + + async createInvite(options?: DiscordInviteOptions) { + const invite = await this.session.rest.runMethod( this.session.rest, "POST", Routes.CHANNEL_INVITES(this.id), - options, + options + ? { + max_age: options.maxAge, + max_uses: options.maxUses, + temporary: options.temporary, + unique: options.unique, + target_type: options.targetType, + target_user_id: options.targetUserId, + target_application_id: options.targetApplicationId, + } + : {}, ); + + return new Invite(this.session, invite); } async createThread(options: ThreadCreateOptions): Promise { diff --git a/structures/WelcomeChannel.ts b/structures/WelcomeChannel.ts new file mode 100644 index 0000000..e540207 --- /dev/null +++ b/structures/WelcomeChannel.ts @@ -0,0 +1,32 @@ +import type { Model } from "./Base.ts"; +import type { Snowflake } from "../util/Snowflake.ts"; +import type { Session } from "../session/Session.ts"; +import type { DiscordWelcomeScreenChannel } from "../vendor/external.ts"; +import Emoji from "./Emoji.ts"; + +/** + * @link https://discord.com/developers/docs/resources/guild#welcome-screen-object-welcome-screen-channel-structure + */ +export class WelcomeChannel implements Model { + constructor(session: Session, data: DiscordWelcomeScreenChannel) { + this.session = session; + this.channelId = data.channel_id; + this.description = data.description; + this.emoji = new Emoji(session, { + name: data.emoji_name ? data.emoji_name : undefined, + id: data.emoji_id ? data.emoji_id : undefined, + }); + } + + session: Session; + channelId: Snowflake; + description: string; + emoji: Emoji; + + /** alias for WelcomeScreenChannel.channelId */ + get id() { + return this.channelId; + } +} + +export default WelcomeChannel; diff --git a/structures/WelcomeScreen.ts b/structures/WelcomeScreen.ts new file mode 100644 index 0000000..af4b85a --- /dev/null +++ b/structures/WelcomeScreen.ts @@ -0,0 +1,26 @@ +import type { Session } from "../session/Session.ts"; +import type { DiscordWelcomeScreen } from "../vendor/external.ts"; +import WelcomeChannel from "./WelcomeChannel.ts"; + +/** + * @link https://discord.com/developers/docs/resources/guild#welcome-screen-object + */ +export class WelcomeScreen { + constructor(session: Session, data: DiscordWelcomeScreen) { + this.session = session; + this.welcomeChannels = data.welcome_channels.map((welcomeChannel) => + new WelcomeChannel(session, welcomeChannel) + ); + + if (data.description) { + this.description = data.description; + } + } + + readonly session: Session; + + description?: string; + welcomeChannels: WelcomeChannel[]; +} + +export default WelcomeScreen;