2020-11-02 06:58:23 +00:00
|
|
|
import { Message } from '../../structures/message.ts'
|
|
|
|
import { TextChannel } from '../../structures/textChannel.ts'
|
|
|
|
import { User } from '../../structures/user.ts'
|
|
|
|
import { MessagePayload } from '../../types/channel.ts'
|
2020-11-01 06:38:45 +00:00
|
|
|
import { Gateway, GatewayEventHandler } from '../index.ts'
|
|
|
|
|
2020-11-02 06:58:23 +00:00
|
|
|
export const messageCreate: GatewayEventHandler = async (
|
2020-11-01 06:38:45 +00:00
|
|
|
gateway: Gateway,
|
|
|
|
d: MessagePayload
|
|
|
|
) => {
|
2020-11-02 12:08:38 +00:00
|
|
|
let channel = await gateway.client.channels.get<TextChannel>(d.channel_id)
|
2020-11-01 06:38:45 +00:00
|
|
|
// Fetch the channel if not cached
|
2020-11-02 06:58:23 +00:00
|
|
|
if (channel === undefined)
|
2020-11-04 12:38:00 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
|
|
channel = (await gateway.client.channels.fetch(d.channel_id)) as TextChannel
|
2020-11-15 09:16:36 +00:00
|
|
|
await channel.messages.set(d.id, d)
|
2020-11-02 06:58:23 +00:00
|
|
|
const user = new User(gateway.client, d.author)
|
2020-11-01 11:22:09 +00:00
|
|
|
await gateway.client.users.set(d.author.id, d.author)
|
2020-11-02 12:08:38 +00:00
|
|
|
let guild
|
2020-11-07 13:05:37 +00:00
|
|
|
let member
|
2020-11-07 02:32:14 +00:00
|
|
|
if (d.guild_id !== undefined) {
|
2020-11-02 12:08:38 +00:00
|
|
|
guild = await gateway.client.guilds.get(d.guild_id)
|
|
|
|
}
|
2020-11-07 13:05:37 +00:00
|
|
|
if (guild !== undefined && d.member !== undefined) {
|
|
|
|
d.member.user = d.author
|
|
|
|
await guild.members.set(d.author.id, d.member)
|
|
|
|
member = await guild.members.get(d.author.id)
|
|
|
|
}
|
2020-11-25 11:53:40 +00:00
|
|
|
const message = new Message(gateway.client, d, channel, user)
|
2020-11-03 15:19:20 +00:00
|
|
|
if (guild !== undefined) message.guild = guild
|
2020-11-09 10:29:16 +00:00
|
|
|
await message.mentions.fromPayload(d)
|
|
|
|
message.member = member
|
2020-11-07 13:05:37 +00:00
|
|
|
if (message.member !== undefined) {
|
2020-11-08 07:57:24 +00:00
|
|
|
if (message.member.user === undefined) {
|
2020-11-07 13:05:37 +00:00
|
|
|
const user = await gateway.client.users.get(message.member.id)
|
2020-11-08 07:57:24 +00:00
|
|
|
if (user !== undefined) {
|
2020-11-07 13:05:37 +00:00
|
|
|
message.member.user = user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 06:38:45 +00:00
|
|
|
gateway.client.emit('messageCreate', message)
|
|
|
|
}
|