Skip to content

Commit ab9c8e4

Browse files
committed
Migrate deleteInlineRepliesFromBot
1 parent 87f0140 commit ab9c8e4

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

src/handler/messageDeleteHandler.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1-
import type { ClientUser, Message } from "discord.js";
1+
import type { Message } from "discord.js";
22

33
import type { BotContext } from "#context.ts";
44

55
import * as pollService from "#service/poll.ts";
6+
import * as botReplyService from "#service/botReply.ts";
67

78
import log from "#log";
89

9-
const deleteInlineRepliesFromBot = (messageRef: Message<true>, botUser: ClientUser) =>
10-
Promise.allSettled(
11-
messageRef.channel.messages.cache
12-
.filter(m => m.author.id === botUser.id && m.reference?.messageId === messageRef.id)
13-
.map(m => m.delete()),
14-
);
10+
async function deleteInlineRepliesFromBot(message: Message<true>) {
11+
const botReplies = await botReplyService.getBotRepliesForMessage(message);
12+
if (botReplies.length === 0) {
13+
return;
14+
}
15+
16+
const deletePromises = botReplies.map(async reply => {
17+
try {
18+
const botReplyMessage = await message.channel.messages.fetch(reply.botReplyMessageId);
19+
await botReplyMessage.delete();
20+
} catch (error) {
21+
log.warn({ error, replyId: reply.id }, "Failed to delete bot reply message");
22+
}
23+
});
24+
25+
await Promise.allSettled(deletePromises);
26+
27+
await botReplyService.deleteBotRepliesForMessage(message);
28+
}
1529

1630
export default async function (message: Message<true>, context: BotContext) {
1731
if (message.author.id === context.client.user.id) {
@@ -23,13 +37,7 @@ export default async function (message: Message<true>, context: BotContext) {
2337

2438
await handlePollDeletion(message);
2539

26-
const isNormalCommand =
27-
message.content.startsWith(context.prefix.command) ||
28-
message.content.startsWith(context.prefix.modCommand);
29-
30-
if (isNormalCommand) {
31-
await deleteInlineRepliesFromBot(message, context.client.user);
32-
}
40+
await deleteInlineRepliesFromBot(message);
3341
}
3442

3543
async function handlePollDeletion(message: Message<true>) {

0 commit comments

Comments
 (0)