harmony/src/gateway/handlers/messageDeleteBulk.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-12-02 12:29:52 +00:00
import { Message } from '../../structures/message.ts'
import { GuildTextChannel } from '../../structures/textChannel.ts'
import { MessageDeleteBulkPayload } from '../../types/gateway.ts'
import { Collection } from '../../utils/collection.ts'
import { Gateway, GatewayEventHandler } from '../index.ts'
export const messageDeleteBulk: GatewayEventHandler = async (
gateway: Gateway,
d: MessageDeleteBulkPayload
) => {
let channel = await gateway.client.channels.get<GuildTextChannel>(
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 GuildTextChannel
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)
}