harmony/src/gateway/handlers/messageReactionAdd.ts

53 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-04-04 05:42:15 +00:00
import type { Gateway, GatewayEventHandler } from '../mod.ts'
2020-12-02 12:29:52 +00:00
import { MessageReactionAddPayload } from '../../types/gateway.ts'
import { TextChannel } from '../../structures/textChannel.ts'
import { MessageReaction } from '../../structures/messageReaction.ts'
import { UserPayload } from '../../types/user.ts'
export const messageReactionAdd: GatewayEventHandler = async (
gateway: Gateway,
d: MessageReactionAddPayload
) => {
let channel = await gateway.client.channels.get<TextChannel>(d.channel_id)
if (channel === undefined)
channel = await gateway.client.channels.fetch<TextChannel>(d.channel_id)
if (channel === undefined) return
let message = await channel.messages.get(d.message_id)
if (message === undefined) {
if (gateway.client.fetchUncachedReactions === true) {
message = await channel.messages.fetch(d.message_id)
if (message === undefined) return
} else return
}
let user = await gateway.client.users.get(d.user_id)
if (user === undefined) {
if (gateway.client.fetchUncachedReactions === true) {
user = await gateway.client.users.fetch(d.user_id)
if (user === undefined) return
} else return
}
2020-12-26 06:49:52 +00:00
const emojiID = (d.emoji.id !== null ? d.emoji.id : d.emoji.name) as string
2020-12-24 00:59:32 +00:00
let reaction = await message.reactions.get(emojiID)
2020-12-02 12:29:52 +00:00
if (reaction === undefined) {
2020-12-24 00:59:32 +00:00
await message.reactions.set(emojiID, {
2020-12-02 12:29:52 +00:00
count: 1,
emoji: d.emoji,
me: d.user_id === gateway.client.user?.id
})
reaction = ((await message.reactions.get(
2020-12-24 00:59:32 +00:00
emojiID
2020-12-02 12:29:52 +00:00
)) as unknown) as MessageReaction
}
const rawUser = ((await gateway.client.users.get(
d.user_id
)) as unknown) as UserPayload
reaction.users.set(rawUser.id, rawUser)
gateway.client.emit('messageReactionAdd', reaction, user)
}