From dca91be86e43473a886b8b2fe32ba7329b0583a9 Mon Sep 17 00:00:00 2001 From: socram03 Date: Sat, 25 Jun 2022 14:01:58 -0400 Subject: [PATCH] feat: fix and docs --- structures/GuildChannel.ts | 4 ++-- structures/NewsChannel.ts | 4 ++-- structures/TextChannel.ts | 32 ++++++++++++++++++++++---------- structures/ThreadChannel.ts | 4 ++-- structures/VoiceChannel.ts | 9 +++++---- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/structures/GuildChannel.ts b/structures/GuildChannel.ts index a4a941c..748fc0e 100644 --- a/structures/GuildChannel.ts +++ b/structures/GuildChannel.ts @@ -4,9 +4,9 @@ import { DiscordChannel, Routes, Session, Snowflake } from "../mod.ts"; export class GuildChannel extends Channel { - constructor(session: Session, data: DiscordChannel, guild: Guild) { + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { super(session, data); - this.guildId = guild.id; + this.guildId = guildId; this.position = data.position; data.topic ? this.topic = data.topic : null; data.parent_id ? this.parentId = data.parent_id : undefined; diff --git a/structures/NewsChannel.ts b/structures/NewsChannel.ts index ede8a7a..258cc93 100644 --- a/structures/NewsChannel.ts +++ b/structures/NewsChannel.ts @@ -3,8 +3,8 @@ import { TextChannel } from "./TextChannel.ts" import { Session, DiscordChannel } from "../mod.ts"; export class NewsChannel extends TextChannel { - constructor(session: Session, data: DiscordChannel , guild: Guild) { - super(session, data, guild); + constructor(session: Session, data: DiscordChannel , guildId: Guild["id"]) { + super(session, data, guildId); this.defaultAutoArchiveDuration = data.default_auto_archive_duration; } defaultAutoArchiveDuration?: number; diff --git a/structures/TextChannel.ts b/structures/TextChannel.ts index f901d57..dc17f23 100644 --- a/structures/TextChannel.ts +++ b/structures/TextChannel.ts @@ -12,6 +12,11 @@ import { } from "../mod.ts"; import { GetMessagesOptions } from "../util/Routes.ts" +/** + * Represents the options object to create an invitation + * https://discord.com/developers/docs/resources/channel#create-channel-invite-json-params + */ + export interface DiscordInvite { max_age?: number; max_uses?: number; @@ -20,6 +25,11 @@ export interface DiscordInvite { reason?: string; } +/** + * Represent the options object to create a Thread Channel + * https://discord.com/developers/docs/resources/channel#start-thread-without-message + */ + export interface ThreadCreateOptions { name: string; autoArchiveDuration: 60 | 1440 | 4320 | 10080; @@ -29,8 +39,8 @@ export interface ThreadCreateOptions { } export class TextChannel extends GuildChannel { - constructor(session: Session, data: DiscordChannel, guild: Guild) { - super(session, data, guild); + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { + super(session, data, guildId); data.last_message_id ? this.lastMessageId = data.last_message_id : undefined; data.last_pin_timestamp ? this.lastPinTimestamp = data.last_pin_timestamp : undefined; this.rateLimitPerUser = data.rate_limit_per_user ?? 0; @@ -42,15 +52,15 @@ export class TextChannel extends GuildChannel { rateLimitPerUser: number; nsfw: boolean; - async fetchPins(): Promise { - const messages = await this.session.rest.runMethod( + async fetchPins(): Promise { + const messages = await this.session.rest.runMethod( this.session.rest, "GET", Routes.CHANNEL_PINS(this.id), ); - return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : undefined; + return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : []; } - + // TODO return Invite Class createInvite(options?: DiscordInvite) { return this.session.rest.runMethod( this.session.rest, @@ -60,22 +70,24 @@ export class TextChannel extends GuildChannel { ); } - createThread(options: ThreadCreateOptions) { - this.session.rest.runMethod( + async createThread(options: ThreadCreateOptions): Promise { + const thread = await this.session.rest.runMethod( this.session.rest, "POST", Routes.CHANNEL_CREATE_THREAD(this.id), options, ); + return new ThreadChannel(this.session, thread, this.guildId); } - fetchMessages(options?: GetMessagesOptions) { + async fetchMessages(options?: GetMessagesOptions): Promise { if (options?.limit! > 100) throw Error("Values must be between 0-100") - return this.session.rest.runMethod( + const messages = await this.session.rest.runMethod( this.session.rest, "GET", Routes.CHANNEL_MESSAGES(this.id, options) ) + return messages[0] ? messages.map((x) => new Message(this.session, x)) : []; } sendTyping() { diff --git a/structures/ThreadChannel.ts b/structures/ThreadChannel.ts index 50daa18..dd03abb 100644 --- a/structures/ThreadChannel.ts +++ b/structures/ThreadChannel.ts @@ -3,8 +3,8 @@ import { Guild } from "./Guild.ts"; import { DiscordChannel, Session, Snowflake } from "../mod.ts"; export class ThreadChannel extends GuildChannel { - constructor(session: Session, data: DiscordChannel, guild: Guild) { - super(session, data, guild); + constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) { + super(session, data, guildId); this.archived = !!data.thread_metadata?.archived; this.archiveTimestamp = data.thread_metadata?.archive_timestamp; this.autoArchiveDuration = data.thread_metadata?.auto_archive_duration; diff --git a/structures/VoiceChannel.ts b/structures/VoiceChannel.ts index 8496cd7..b7872eb 100644 --- a/structures/VoiceChannel.ts +++ b/structures/VoiceChannel.ts @@ -1,19 +1,20 @@ import { GuildChannel } from "./GuildChannel.ts"; import { Guild } from "./Guild.ts"; -import { DiscordChannel, Session, VideoQualityModes } from "../mod.ts"; +import { DiscordChannel, Session, VideoQualityModes, Snowflake } from "../mod.ts"; export class VoiceChannel extends GuildChannel { constructor(session: Session, data: DiscordChannel, guild: Guild) { super(session, data, guild); this.bitRate = data.bitrate; - this.userLimit = data.user_limit; + this.userLimit = data.user_limit ?? 0; data.rtc_region ? this.rtcRegion = data.rtc_region : undefined; this.videoQuality = data.video_quality_mode; this.nsfw = !!data.nsfw; } bitRate?: number; - userLimit?: number; - rtcRegion?: string; + userLimit: number; + rtcRegion?: Snowflake; + videoQuality?: VideoQualityModes; nsfw?: boolean; }