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 {
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;

View File

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

View File

@ -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<Message[] | undefined> {
const messages = await this.session.rest.runMethod(
async fetchPins(): Promise<Message[] | []> {
const messages = await this.session.rest.runMethod<DiscordMessage[]>(
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<DiscordInviteCreate>(
this.session.rest,
@ -60,22 +70,24 @@ export class TextChannel extends GuildChannel {
);
}
createThread(options: ThreadCreateOptions) {
this.session.rest.runMethod<ThreadChannel>(
async createThread(options: ThreadCreateOptions): Promise<ThreadChannel> {
const thread = await this.session.rest.runMethod<DiscordChannel>(
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<Message[] | []> {
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,
"GET",
Routes.CHANNEL_MESSAGES(this.id, options)
)
return messages[0] ? messages.map((x) => new Message(this.session, x)) : [];
}
sendTyping() {

View File

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

View File

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