2020-10-31 11:45:33 +00:00
|
|
|
import { Client } from "../models/client.ts";
|
|
|
|
import { Channel } from "../structures/channel.ts";
|
|
|
|
import { User } from "../structures/user.ts";
|
2020-11-01 12:05:22 +00:00
|
|
|
import { ChannelPayload } from "../types/channel.ts";
|
2020-10-31 11:45:33 +00:00
|
|
|
import { CHANNEL } from "../types/endpoint.ts";
|
2020-11-02 12:08:38 +00:00
|
|
|
import getChannelByType from "../utils/getChannelByType.ts";
|
2020-10-31 11:45:33 +00:00
|
|
|
import { BaseManager } from "./BaseManager.ts";
|
|
|
|
|
|
|
|
export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
|
|
|
|
constructor(client: Client) {
|
2020-11-02 12:08:38 +00:00
|
|
|
super(client, "channels", Channel)
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override get method as Generic
|
2020-11-02 12:08:38 +00:00
|
|
|
async get<T = Channel>(key: string): Promise<T | void> {
|
|
|
|
let data = await this._get(key) as any
|
|
|
|
if(!data) return
|
|
|
|
let guild
|
|
|
|
if(data.guild_id) {
|
|
|
|
guild = await this.client.guilds.get(data.guild_id)
|
|
|
|
}
|
|
|
|
let res = getChannelByType(this.client, data as ChannelPayload, guild || undefined)
|
|
|
|
return res as any
|
|
|
|
}
|
|
|
|
|
|
|
|
async array(): Promise<void | Channel[]> {
|
|
|
|
let arr = await (this.client.cache.array(this.cacheName) as ChannelPayload[])
|
|
|
|
let result: any[] = []
|
|
|
|
for(let elem of arr) {
|
|
|
|
let guild
|
|
|
|
if((elem as any).guild_id) {
|
|
|
|
guild = await this.client.guilds.get((elem as any).guild_id)
|
|
|
|
}
|
|
|
|
result.push(getChannelByType(this.client, elem as ChannelPayload, guild || undefined))
|
|
|
|
}
|
|
|
|
return result
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 11:22:09 +00:00
|
|
|
fetch(id: string): Promise<Channel> {
|
2020-10-31 11:45:33 +00:00
|
|
|
return new Promise((res, rej) => {
|
2020-11-02 12:08:38 +00:00
|
|
|
this.client.rest.get(CHANNEL(id)).then(async data => {
|
2020-10-31 11:45:33 +00:00
|
|
|
this.set(id, data as ChannelPayload)
|
2020-11-02 12:08:38 +00:00
|
|
|
let guild
|
|
|
|
if((data as any).guild_id) {
|
|
|
|
guild = await this.client.guilds.get((data as any).guild_id)
|
|
|
|
}
|
|
|
|
res(getChannelByType(this.client, data as ChannelPayload, guild || undefined))
|
2020-10-31 11:45:33 +00:00
|
|
|
}).catch(e => rej(e))
|
|
|
|
})
|
|
|
|
}
|
2020-11-02 06:58:23 +00:00
|
|
|
}
|