2020-11-08 07:57:24 +00:00
|
|
|
import { Client } from '../models/client.ts'
|
|
|
|
import { Channel } from '../structures/channel.ts'
|
2020-11-25 11:53:40 +00:00
|
|
|
import { ChannelPayload, GuildChannelPayload } from '../types/channel.ts'
|
2020-11-08 07:57:24 +00:00
|
|
|
import { CHANNEL } from '../types/endpoint.ts'
|
|
|
|
import getChannelByType from '../utils/getChannelByType.ts'
|
|
|
|
import { BaseManager } from './base.ts'
|
2020-10-31 11:45:33 +00:00
|
|
|
|
|
|
|
export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
|
2020-12-02 12:29:52 +00:00
|
|
|
constructor(client: Client) {
|
2020-11-25 11:53:40 +00:00
|
|
|
super(client, 'channels', Channel)
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override get method as Generic
|
2020-12-02 12:29:52 +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
|
2020-11-02 12:08:38 +00:00
|
|
|
let guild
|
2020-11-25 11:53:40 +00:00
|
|
|
if ('guild_id' in data) {
|
|
|
|
guild = await this.client.guilds.get(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
|
|
(data as GuildChannelPayload).guild_id
|
|
|
|
)
|
2020-11-02 12:08:38 +00:00
|
|
|
}
|
2020-11-03 09:21:29 +00:00
|
|
|
const res = getChannelByType(this.client, data, guild)
|
2020-11-02 12:08:38 +00:00
|
|
|
return res as any
|
|
|
|
}
|
|
|
|
|
2020-12-19 07:58:40 +00:00
|
|
|
async array(): Promise<Channel[]> {
|
2020-11-25 11:53:40 +00:00
|
|
|
const arr = await (this.client.cache.array(
|
|
|
|
this.cacheName
|
|
|
|
) as ChannelPayload[])
|
2020-11-03 09:21:29 +00:00
|
|
|
const result: any[] = []
|
2020-11-03 23:00:03 +00:00
|
|
|
for (const elem of arr) {
|
2020-11-02 12:08:38 +00:00
|
|
|
let guild
|
2020-11-25 11:53:40 +00:00
|
|
|
if ('guild_id' in elem) {
|
|
|
|
guild = await this.client.guilds.get(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
|
|
(elem as GuildChannelPayload).guild_id
|
|
|
|
)
|
2020-11-02 12:08:38 +00:00
|
|
|
}
|
2020-11-03 09:21:29 +00:00
|
|
|
result.push(getChannelByType(this.client, elem, guild))
|
2020-11-02 12:08:38 +00:00
|
|
|
}
|
|
|
|
return result
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 02:39:37 +00:00
|
|
|
/** Fetches a Channel by ID, cache it, resolve it */
|
2020-12-02 12:29:52 +00:00
|
|
|
async fetch<T = Channel>(id: string): Promise<T> {
|
2020-11-03 09:21:29 +00:00
|
|
|
return await new Promise((resolve, reject) => {
|
2020-11-25 11:53:40 +00:00
|
|
|
this.client.rest
|
|
|
|
.get(CHANNEL(id))
|
2020-12-02 12:29:52 +00:00
|
|
|
.then(async (data) => {
|
2020-11-25 11:53:40 +00:00
|
|
|
this.set(id, data as ChannelPayload)
|
|
|
|
let guild
|
|
|
|
if (data.guild_id !== undefined) {
|
|
|
|
guild = await this.client.guilds.get(data.guild_id)
|
|
|
|
}
|
2020-12-02 12:29:52 +00:00
|
|
|
resolve(
|
|
|
|
(getChannelByType(
|
|
|
|
this.client,
|
|
|
|
data as ChannelPayload,
|
|
|
|
guild
|
|
|
|
) as unknown) as T
|
|
|
|
)
|
2020-11-25 11:53:40 +00:00
|
|
|
})
|
2020-12-02 12:29:52 +00:00
|
|
|
.catch((e) => reject(e))
|
2020-10-31 11:45:33 +00:00
|
|
|
})
|
|
|
|
}
|
2020-11-02 06:58:23 +00:00
|
|
|
}
|