feat: Channels structures

This commit is contained in:
socram03 2022-06-25 13:31:24 -04:00
parent 3246fd2b81
commit e2ceefdc0a
9 changed files with 190 additions and 36 deletions

View File

@ -1,20 +1,16 @@
import type { Model } from "./Base.ts"; import type { Model } from "./Base.ts";
import { Snowflake, Session, DiscordChannel, ChannelTypes } from "../mod.ts"; import { Snowflake, Session, DiscordChannel, ChannelTypes } from "../mod.ts";
export class Channel implements Model { export abstract class Channel implements Model {
constructor(session: Session, data: DiscordChannel) { constructor(session: Session, data: DiscordChannel) {
this.id = data.id; this.id = data.id;
this.data = data;
this.session = session; this.session = session;
this.name = this.data.name; this.name = data.name;
this.type = data.type;
} }
readonly data: DiscordChannel;
readonly id: Snowflake; readonly id: Snowflake;
readonly session: Session; readonly session: Session;
public readonly name: string | undefined; readonly name: string | undefined;
readonly type: ChannelTypes;
get type() {
return ChannelTypes[this.data.type];
}
} }

22
structures/DMChannel.ts Normal file
View File

@ -0,0 +1,22 @@
import { Channel } from "./Channel.ts";
//import { User } from "./User.ts";
import { Session, DiscordChannel, Snowflake, Routes } from "../mod.ts";
export class DMChannel extends Channel {
constructor(session: Session, data: DiscordChannel) {
super(session, data);
data.last_message_id ? this.lastMessageId = data.last_message_id : undefined;
// Waiting for implementation of botId in session
//this.user = new User(this.session, data.recipents!.find((r) => r.id !== this.session.botId));
}
lastMessageId?: Snowflake;
async close() {
const channel = await this.session.rest.runMethod<DiscordChannel>(
this.session.rest,
"DELETE",
Routes.CHANNEL(this.id)
)
return new DMChannel(this.session, channel);
}
}

View File

@ -1,21 +1,21 @@
import { Channel } from "./Channel.ts"; import { Channel } from "./Channel.ts";
import { DiscordChannel, Routes, Session } from "../mod.ts"; import { Guild } from "./Guild.ts";
import { DiscordChannel, Routes, Session, Snowflake } from "../mod.ts";
export class GuildChannel extends Channel { export class GuildChannel extends Channel {
constructor(session: Session, data: DiscordChannel) { constructor(session: Session, data: DiscordChannel, guild: Guild) {
super(session, data); super(session, data);
this.guild_id = data.guild_id; this.guildId = guild.id;
this.position = data.position;
data.topic ? this.topic = data.topic : null;
data.parent_id ? this.parentId = data.parent_id : undefined;
} }
guild_id?: string; guildId: Snowflake;
topic?: string;
get topic() { position?: number;
return this.data.topic; parentId?: Snowflake;
}
get guildId() {
return this.guild_id;
}
delete(reason?: string) { delete(reason?: string) {
return this.session.rest.runMethod<DiscordChannel>( return this.session.rest.runMethod<DiscordChannel>(

11
structures/NewsChannel.ts Normal file
View File

@ -0,0 +1,11 @@
import { Guild } from "./Guild.ts";
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);
this.defaultAutoArchiveDuration = data.default_auto_archive_duration;
}
defaultAutoArchiveDuration?: number;
}

View File

@ -1,27 +1,88 @@
import { GuildChannel } from "./GuildChannel.ts"; import { GuildChannel } from "./GuildChannel.ts";
import { DiscordChannel, Session } from "../mod.ts"; import { Guild } from "./Guild.ts";
import { ThreadChannel } from "./ThreadChannel.ts";
import { Message } from "./Message.ts";
import {
DiscordChannel,
DiscordInviteCreate,
Routes,
Session,
Snowflake,
DiscordMessage
} from "../mod.ts";
import { GetMessagesOptions } from "../util/Routes.ts"
export interface DiscordInvite {
max_age?: number;
max_uses?: number;
unique?: boolean;
temporary: boolean;
reason?: string;
}
export interface ThreadCreateOptions {
name: string;
autoArchiveDuration: 60 | 1440 | 4320 | 10080;
type: 10 | 11 | 12;
invitable?: boolean;
reason?: string;
}
export class TextChannel extends GuildChannel { export class TextChannel extends GuildChannel {
constructor(session: Session, data: DiscordChannel) { constructor(session: Session, data: DiscordChannel, guild: Guild) {
super(session, data); super(session, data, guild);
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;
this.nsfw = !!data.nsfw ?? false;
} }
get lastMessageId() { lastMessageId?: Snowflake;
return this.data.last_message_id; lastPinTimestamp?: string;
} rateLimitPerUser: number;
get rateLimitPerUser() { nsfw: boolean;
return this.data.rate_limit_per_user;
async fetchPins(): Promise<Message[] | undefined> {
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;
} }
get parentId() { createInvite(options?: DiscordInvite) {
return this.data.parent_id; return this.session.rest.runMethod<DiscordInviteCreate>(
this.session.rest,
"POST",
Routes.CHANNEL_INVITES(this.id),
options,
);
} }
get permissionOverwrites() { createThread(options: ThreadCreateOptions) {
return this.data.permission_overwrites; this.session.rest.runMethod<ThreadChannel>(
this.session.rest,
"POST",
Routes.CHANNEL_CREATE_THREAD(this.id),
options,
);
} }
get nsfw() { fetchMessages(options?: GetMessagesOptions) {
return this.data.nsfw; if (options?.limit! > 100) throw Error("Values must be between 0-100")
return this.session.rest.runMethod<Message[]>(
this.session.rest,
"GET",
Routes.CHANNEL_MESSAGES(this.id, options)
)
}
sendTyping() {
this.session.rest.runMethod<undefined>(
this.session.rest,
"POST",
Routes.CHANNEL_TYPING(this.id),
);
} }
} }

View File

@ -0,0 +1,23 @@
import { GuildChannel } from "./GuildChannel.ts";
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);
this.archived = !!data.thread_metadata?.archived;
this.archiveTimestamp = data.thread_metadata?.archive_timestamp;
this.autoArchiveDuration = data.thread_metadata?.auto_archive_duration;
this.locked = !!data.thread_metadata?.locked;
this.messageCount = data.message_count;
this.memberCount = data.member_count;
this.ownerId = data.owner_id;
}
archived?: boolean;
archiveTimestamp?: string;
autoArchiveDuration?: number;
locked?: boolean;
messageCount?: number;
memberCount?: number;
ownerId?: Snowflake;
}

View File

@ -0,0 +1,19 @@
import { GuildChannel } from "./GuildChannel.ts";
import { Guild } from "./Guild.ts";
import { DiscordChannel, Session, VideoQualityModes } 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;
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;
videoQuality?: VideoQualityModes;
nsfw?: boolean;
}

View File

@ -30,6 +30,26 @@ export function CHANNEL(channelId: Snowflake) {
return `/channels/${channelId}`; return `/channels/${channelId}`;
} }
export function CHANNEL_INVITES(channelId: Snowflake) {
return `/channels/${channelId}/invites`;
}
export function CHANNEL_TYPING(channelId: Snowflake) {
return `/channels/${channelId}/typing`;
}
export function CHANNEL_CREATE_THREAD(channelId: Snowflake) {
return `/channels/${channelId}/threads`;
}
export function MESSAGE_CREATE_THREAD(channelId: Snowflake, messageId: Snowflake) {
return `/channels/${channelId}/messages/${messageId}/threads`;
}
export function CHANNEL_PINS(channelId: Snowflake) {
return `/channels/${channelId}/pins`;
}
/** used to send messages */ /** used to send messages */
export function CHANNEL_MESSAGES(channelId: Snowflake, options?: GetMessagesOptions) { export function CHANNEL_MESSAGES(channelId: Snowflake, options?: GetMessagesOptions) {
let url = `/channels/${channelId}/messages?`; let url = `/channels/${channelId}/messages?`;

View File

@ -752,6 +752,8 @@ export interface DiscordChannel {
permissions?: string; permissions?: string;
/** When a thread is created this will be true on that channel payload for the thread. */ /** When a thread is created this will be true on that channel payload for the thread. */
newly_created?: boolean; newly_created?: boolean;
/** The recipients of the DM*/
recipents?: DiscordUser[];
} }
/** https://discord.com/developers/docs/topics/gateway#presence-update */ /** https://discord.com/developers/docs/topics/gateway#presence-update */