2020-11-08 07:58:24 +00:00
|
|
|
import { Client } from '../models/client.ts'
|
|
|
|
import { Message } from '../structures/message.ts'
|
|
|
|
import { MessageMentions } from '../structures/messageMentions.ts'
|
|
|
|
import { TextChannel } from '../structures/textChannel.ts'
|
|
|
|
import { User } from '../structures/user.ts'
|
|
|
|
import { MessagePayload } from '../types/channel.ts'
|
|
|
|
import { CHANNEL_MESSAGE } from '../types/endpoint.ts'
|
|
|
|
import { BaseManager } from './base.ts'
|
2020-10-31 13:00:33 +00:00
|
|
|
|
|
|
|
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-08 07:58:24 +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-08 07:58:24 +00:00
|
|
|
|
2020-11-01 13:42:00 +00:00
|
|
|
let channel = await this.client.channels.get(raw.channel_id)
|
2020-11-08 07:58:24 +00:00
|
|
|
if (channel === undefined)
|
|
|
|
channel = await this.client.channels.fetch(raw.channel_id)
|
|
|
|
|
2020-11-03 09:21:29 +00:00
|
|
|
const author = new User(this.client, raw.author)
|
|
|
|
const mentions = new MessageMentions()
|
2020-11-08 07:58:24 +00:00
|
|
|
|
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-08 07:58:24 +00:00
|
|
|
async fetch (channelID: string, id: string): Promise<Message> {
|
2020-11-03 09:21:29 +00:00
|
|
|
return await new Promise((resolve, reject) => {
|
2020-11-08 07:58:24 +00:00
|
|
|
this.client.rest
|
|
|
|
.get(CHANNEL_MESSAGE(channelID, id))
|
|
|
|
.then(async data => {
|
|
|
|
this.set(id, data as MessagePayload)
|
|
|
|
|
|
|
|
let channel: any = await this.client.channels.get<TextChannel>(
|
|
|
|
channelID
|
|
|
|
)
|
|
|
|
if (channel === undefined)
|
|
|
|
channel = await this.client.channels.fetch(channelID)
|
|
|
|
|
|
|
|
const author = new User(this.client, (data as MessagePayload).author)
|
|
|
|
await this.client.users.set(
|
|
|
|
author.id,
|
|
|
|
(data as MessagePayload).author
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: Make this thing work (MessageMentions)
|
|
|
|
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
|
|
|
}
|