2020-10-31 13:00:33 +00:00
|
|
|
import { Client } from "../models/client.ts";
|
|
|
|
import { Message } from "../structures/message.ts";
|
2020-11-01 11:22:09 +00:00
|
|
|
import { MessageMentions } from "../structures/MessageMentions.ts";
|
2020-11-02 12:08:38 +00:00
|
|
|
import { TextChannel } from "../structures/textChannel.ts";
|
2020-11-01 11:22:09 +00:00
|
|
|
import { User } from "../structures/user.ts";
|
2020-11-01 12:05:22 +00:00
|
|
|
import { MessagePayload } from "../types/channel.ts";
|
2020-10-31 13:00:33 +00:00
|
|
|
import { CHANNEL_MESSAGE } from "../types/endpoint.ts";
|
|
|
|
import { BaseManager } from "./BaseManager.ts";
|
|
|
|
|
|
|
|
export class MessagesManager extends BaseManager<MessagePayload, Message> {
|
2020-11-02 06:58:23 +00:00
|
|
|
constructor (client: Client) {
|
|
|
|
super(client, 'messages', Message)
|
2020-10-31 13:00:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 05:19:57 +00:00
|
|
|
async get(key: string): Promise<Message | undefined> {
|
2020-11-01 13:42:00 +00:00
|
|
|
const raw = await this._get(key)
|
2020-11-03 15:19:20 +00:00
|
|
|
if (raw === undefined) return
|
2020-11-01 13:42:00 +00:00
|
|
|
let channel = await this.client.channels.get(raw.channel_id)
|
2020-11-03 15:19:20 +00:00
|
|
|
if (channel === undefined) channel = await this.client.channels.fetch(raw.channel_id)
|
|
|
|
if (channel === undefined) return
|
2020-11-03 09:21:29 +00:00
|
|
|
const author = new User(this.client, raw.author)
|
|
|
|
const mentions = new MessageMentions()
|
2020-11-03 05:19:57 +00:00
|
|
|
return new this.DataType(this.client, raw, channel, author, mentions) as any
|
2020-11-01 13:42:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:21:29 +00:00
|
|
|
async fetch(channelID: string, id: string): Promise<Message> {
|
|
|
|
return await new Promise((resolve, reject) => {
|
2020-11-01 11:22:09 +00:00
|
|
|
this.client.rest.get(CHANNEL_MESSAGE(channelID, id)).then(async data => {
|
2020-10-31 13:00:33 +00:00
|
|
|
this.set(id, data as MessagePayload)
|
2020-11-03 09:21:29 +00:00
|
|
|
let channel: any = await this.client.channels.get<TextChannel>(channelID)
|
2020-11-03 15:19:20 +00:00
|
|
|
if (channel === undefined) channel = await this.client.channels.fetch(channelID)
|
2020-11-03 09:21:29 +00:00
|
|
|
const author = new User(this.client, (data as MessagePayload).author)
|
2020-11-01 11:22:09 +00:00
|
|
|
await this.client.users.set(author.id, (data as MessagePayload).author)
|
|
|
|
// TODO: Make this thing work (MessageMentions)
|
2020-11-03 09:21:29 +00:00
|
|
|
const mentions = new MessageMentions()
|
|
|
|
resolve(new Message(this.client, data as MessagePayload, channel as TextChannel, author, mentions))
|
|
|
|
}).catch(e => reject(e))
|
2020-10-31 13:00:33 +00:00
|
|
|
})
|
|
|
|
}
|
2020-11-02 06:58:23 +00:00
|
|
|
}
|