harmony/src/managers/channels.ts

51 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-11-08 07:57:24 +00:00
import { Client } from '../models/client.ts'
import { Channel } from '../structures/channel.ts'
import { ChannelPayload } from '../types/channel.ts'
import { CHANNEL } from '../types/endpoint.ts'
import getChannelByType from '../utils/getChannelByType.ts'
import { BaseManager } from './base.ts'
export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
constructor(client: Client) {
super(client, "channels", Channel)
}
// Override get method as Generic
2020-11-03 05:19:57 +00:00
async get<T = Channel>(key: string): Promise<T | undefined> {
2020-11-03 09:21:29 +00:00
const data = await this._get(key)
2020-11-03 15:19:20 +00:00
if (data === undefined) return
let guild
2020-11-03 15:19:20 +00:00
if ((data as any).guild_id !== undefined) {
2020-11-03 09:21:29 +00:00
guild = await this.client.guilds.get((data as any).guild_id)
}
2020-11-03 09:21:29 +00:00
const res = getChannelByType(this.client, data, guild)
return res as any
}
2020-11-03 09:21:29 +00:00
async array(): Promise<undefined | Channel[]> {
const arr = await (this.client.cache.array(this.cacheName) as ChannelPayload[])
const result: any[] = []
2020-11-03 23:00:03 +00:00
for (const elem of arr) {
let guild
2020-11-03 15:19:20 +00:00
if ((elem as any).guild_id !== undefined) {
guild = await this.client.guilds.get((elem as any).guild_id)
}
2020-11-03 09:21:29 +00:00
result.push(getChannelByType(this.client, elem, guild))
}
return result
}
2020-11-03 09:21:29 +00:00
async fetch(id: string): Promise<Channel> {
return await new Promise((resolve, reject) => {
this.client.rest.get(CHANNEL(id)).then(async data => {
this.set(id, data as ChannelPayload)
let guild
2020-11-03 15:19:20 +00:00
if (data.guild_id !== undefined) {
2020-11-03 09:21:29 +00:00
guild = await this.client.guilds.get(data.guild_id)
}
2020-11-03 09:21:29 +00:00
resolve(getChannelByType(this.client, data as ChannelPayload, guild))
}).catch(e => reject(e))
})
}
}