2020-11-02 06:58:23 +00:00
|
|
|
import { Client } from '../models/client.ts'
|
|
|
|
import { Emoji } from '../structures/emoji.ts'
|
|
|
|
import { EmojiPayload } from '../types/emoji.ts'
|
2020-11-08 07:58:24 +00:00
|
|
|
import { GUILD_EMOJI } from '../types/endpoint.ts'
|
2020-11-04 12:38:00 +00:00
|
|
|
import { BaseManager } from './base.ts'
|
2020-10-31 11:45:33 +00:00
|
|
|
|
|
|
|
export class EmojisManager extends BaseManager<EmojiPayload, Emoji> {
|
2020-12-02 12:29:52 +00:00
|
|
|
constructor(client: Client) {
|
2020-11-08 08:47:01 +00:00
|
|
|
super(client, `emojis`, Emoji)
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 12:29:52 +00:00
|
|
|
async get(key: string): Promise<Emoji | undefined> {
|
2020-11-08 11:10:33 +00:00
|
|
|
const raw = await this._get(key)
|
|
|
|
if (raw === undefined) return
|
|
|
|
const emoji = new this.DataType(this.client, raw)
|
2020-11-08 11:36:30 +00:00
|
|
|
if ((raw as any).guild_id !== undefined) {
|
2020-11-08 11:10:33 +00:00
|
|
|
const guild = await this.client.guilds.get((raw as any).guild_id)
|
2020-11-08 11:36:30 +00:00
|
|
|
if (guild !== undefined) emoji.guild = guild
|
2020-11-08 11:10:33 +00:00
|
|
|
}
|
|
|
|
return emoji
|
|
|
|
}
|
|
|
|
|
2020-12-05 02:24:08 +00:00
|
|
|
/** Fetchs an Emoji by Guild ID and Emoji ID, cache it and resolve it */
|
2020-12-02 12:29:52 +00:00
|
|
|
async fetch(guildID: string, id: string): Promise<Emoji> {
|
2020-11-02 06:58:23 +00:00
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
this.client.rest
|
2020-11-08 08:47:01 +00:00
|
|
|
.get(GUILD_EMOJI(guildID, id))
|
2020-12-02 12:29:52 +00:00
|
|
|
.then(async (data) => {
|
2020-11-08 08:47:01 +00:00
|
|
|
await this.set(id, data as EmojiPayload)
|
2020-11-02 06:58:23 +00:00
|
|
|
resolve(new Emoji(this.client, data as EmojiPayload))
|
|
|
|
})
|
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
|
|
|
}
|