harmony/src/gateway/handlers/messageDeleteBulk.ts

34 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-04-04 09:29:56 +00:00
import type { Message } from '../../structures/message.ts'
import type { GuildTextBasedChannel } from '../../structures/guildTextChannel.ts'
import type { MessageDeleteBulkPayload } from '../../types/gateway.ts'
2020-12-02 12:29:52 +00:00
import { Collection } from '../../utils/collection.ts'
2021-04-04 05:42:15 +00:00
import type { Gateway, GatewayEventHandler } from '../mod.ts'
2020-12-02 12:29:52 +00:00
export const messageDeleteBulk: GatewayEventHandler = async (
gateway: Gateway,
d: MessageDeleteBulkPayload
) => {
let channel = await gateway.client.channels.get<GuildTextBasedChannel>(
2020-12-02 12:29:52 +00:00
d.channel_id
)
// Fetch the channel if not cached
if (channel === undefined)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
channel = (await gateway.client.channels.fetch(
d.channel_id
)) as GuildTextBasedChannel
2020-12-02 12:29:52 +00:00
const messages = new Collection<string, Message>()
const uncached = new Set<string>()
for (const id of d.ids) {
const message = await channel.messages.get(id)
if (message === undefined) uncached.add(id)
else {
messages.set(id, message)
await channel.messages._delete(id)
2020-12-02 12:29:52 +00:00
}
}
gateway.client.emit('messageDeleteBulk', channel, messages, uncached)
}