feat: fix and docs

This commit is contained in:
socram03 2022-06-25 14:01:58 -04:00
parent e2ceefdc0a
commit dca91be86e
5 changed files with 33 additions and 20 deletions

View File

@ -4,9 +4,9 @@ import { DiscordChannel, Routes, Session, Snowflake } from "../mod.ts";
export class GuildChannel extends Channel { export class GuildChannel extends Channel {
constructor(session: Session, data: DiscordChannel, guild: Guild) { constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) {
super(session, data); super(session, data);
this.guildId = guild.id; this.guildId = guildId;
this.position = data.position; this.position = data.position;
data.topic ? this.topic = data.topic : null; data.topic ? this.topic = data.topic : null;
data.parent_id ? this.parentId = data.parent_id : undefined; data.parent_id ? this.parentId = data.parent_id : undefined;

View File

@ -3,8 +3,8 @@ import { TextChannel } from "./TextChannel.ts"
import { Session, DiscordChannel } from "../mod.ts"; import { Session, DiscordChannel } from "../mod.ts";
export class NewsChannel extends TextChannel { export class NewsChannel extends TextChannel {
constructor(session: Session, data: DiscordChannel , guild: Guild) { constructor(session: Session, data: DiscordChannel , guildId: Guild["id"]) {
super(session, data, guild); super(session, data, guildId);
this.defaultAutoArchiveDuration = data.default_auto_archive_duration; this.defaultAutoArchiveDuration = data.default_auto_archive_duration;
} }
defaultAutoArchiveDuration?: number; defaultAutoArchiveDuration?: number;

View File

@ -12,6 +12,11 @@ import {
} from "../mod.ts"; } from "../mod.ts";
import { GetMessagesOptions } from "../util/Routes.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 { export interface DiscordInvite {
max_age?: number; max_age?: number;
max_uses?: number; max_uses?: number;
@ -20,6 +25,11 @@ export interface DiscordInvite {
reason?: string; 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 { export interface ThreadCreateOptions {
name: string; name: string;
autoArchiveDuration: 60 | 1440 | 4320 | 10080; autoArchiveDuration: 60 | 1440 | 4320 | 10080;
@ -29,8 +39,8 @@ export interface ThreadCreateOptions {
} }
export class TextChannel extends GuildChannel { export class TextChannel extends GuildChannel {
constructor(session: Session, data: DiscordChannel, guild: Guild) { constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) {
super(session, data, guild); super(session, data, guildId);
data.last_message_id ? this.lastMessageId = data.last_message_id : undefined; data.last_message_id ? this.lastMessageId = data.last_message_id : undefined;
data.last_pin_timestamp ? this.lastPinTimestamp = data.last_pin_timestamp : undefined; data.last_pin_timestamp ? this.lastPinTimestamp = data.last_pin_timestamp : undefined;
this.rateLimitPerUser = data.rate_limit_per_user ?? 0; this.rateLimitPerUser = data.rate_limit_per_user ?? 0;
@ -42,15 +52,15 @@ export class TextChannel extends GuildChannel {
rateLimitPerUser: number; rateLimitPerUser: number;
nsfw: boolean; nsfw: boolean;
async fetchPins(): Promise<Message[] | undefined> { async fetchPins(): Promise<Message[] | []> {
const messages = await this.session.rest.runMethod( const messages = await this.session.rest.runMethod<DiscordMessage[]>(
this.session.rest, this.session.rest,
"GET", "GET",
Routes.CHANNEL_PINS(this.id), 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) { createInvite(options?: DiscordInvite) {
return this.session.rest.runMethod<DiscordInviteCreate>( return this.session.rest.runMethod<DiscordInviteCreate>(
this.session.rest, this.session.rest,
@ -60,22 +70,24 @@ export class TextChannel extends GuildChannel {
); );
} }
createThread(options: ThreadCreateOptions) { async createThread(options: ThreadCreateOptions): Promise<ThreadChannel> {
this.session.rest.runMethod<ThreadChannel>( const thread = await this.session.rest.runMethod<DiscordChannel>(
this.session.rest, this.session.rest,
"POST", "POST",
Routes.CHANNEL_CREATE_THREAD(this.id), Routes.CHANNEL_CREATE_THREAD(this.id),
options, options,
); );
return new ThreadChannel(this.session, thread, this.guildId);
} }
fetchMessages(options?: GetMessagesOptions) { async fetchMessages(options?: GetMessagesOptions): Promise<Message[] | []> {
if (options?.limit! > 100) throw Error("Values must be between 0-100") if (options?.limit! > 100) throw Error("Values must be between 0-100")
return this.session.rest.runMethod<Message[]>( const messages = await this.session.rest.runMethod<DiscordMessage[]>(
this.session.rest, this.session.rest,
"GET", "GET",
Routes.CHANNEL_MESSAGES(this.id, options) Routes.CHANNEL_MESSAGES(this.id, options)
) )
return messages[0] ? messages.map((x) => new Message(this.session, x)) : [];
} }
sendTyping() { sendTyping() {

View File

@ -3,8 +3,8 @@ import { Guild } from "./Guild.ts";
import { DiscordChannel, Session, Snowflake } from "../mod.ts"; import { DiscordChannel, Session, Snowflake } from "../mod.ts";
export class ThreadChannel extends GuildChannel { export class ThreadChannel extends GuildChannel {
constructor(session: Session, data: DiscordChannel, guild: Guild) { constructor(session: Session, data: DiscordChannel, guildId: Guild["id"]) {
super(session, data, guild); super(session, data, guildId);
this.archived = !!data.thread_metadata?.archived; this.archived = !!data.thread_metadata?.archived;
this.archiveTimestamp = data.thread_metadata?.archive_timestamp; this.archiveTimestamp = data.thread_metadata?.archive_timestamp;
this.autoArchiveDuration = data.thread_metadata?.auto_archive_duration; this.autoArchiveDuration = data.thread_metadata?.auto_archive_duration;

View File

@ -1,19 +1,20 @@
import { GuildChannel } from "./GuildChannel.ts"; import { GuildChannel } from "./GuildChannel.ts";
import { Guild } from "./Guild.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 { export class VoiceChannel extends GuildChannel {
constructor(session: Session, data: DiscordChannel, guild: Guild) { constructor(session: Session, data: DiscordChannel, guild: Guild) {
super(session, data, guild); super(session, data, guild);
this.bitRate = data.bitrate; this.bitRate = data.bitrate;
this.userLimit = data.user_limit; this.userLimit = data.user_limit ?? 0;
data.rtc_region ? this.rtcRegion = data.rtc_region : undefined; data.rtc_region ? this.rtcRegion = data.rtc_region : undefined;
this.videoQuality = data.video_quality_mode; this.videoQuality = data.video_quality_mode;
this.nsfw = !!data.nsfw; this.nsfw = !!data.nsfw;
} }
bitRate?: number; bitRate?: number;
userLimit?: number; userLimit: number;
rtcRegion?: string; rtcRegion?: Snowflake;
videoQuality?: VideoQualityModes; videoQuality?: VideoQualityModes;
nsfw?: boolean; nsfw?: boolean;
} }