summaryrefslogtreecommitdiff
path: root/packages/gateway/src/commands/delete.ts
blob: bdbe5859b7195e9fa7ff5c3e409325ca254d34a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Message } from "discord.js";
import { replyWithCleanup } from "../utilities";

export const handleDeleteCommand = async (message: Message) => {
  if (message.author.bot) return;

  if (message.content.toLowerCase().startsWith("uma!delete")) {
    const application = await message.client.application?.fetch();
    const ownerId = application?.owner?.id;

    if (message.author.id !== ownerId) {
      await replyWithCleanup(
        message,
        "❌ Only the server owner can use this command.",
      );

      return;
    }

    const parameters = message.content.split(" ").slice(1);

    if (parameters.length < 1) {
      await replyWithCleanup(
        message,
        "❌ Usage: `uma!delete <message_id> [channel_id] [message_id2] [message_id3] ...`\nExamples:\n- `uma!delete 1234567890123456789` (current channel)\n- `uma!delete 9876543210987654321 1234567890123456789 1111111111111111111` (specific channel, multiple messages)",
      );

      return;
    }

    let targetChannel = null;
    let messageIds: string[] = [];

    if (parameters.length === 1) {
      messageIds = [parameters[0]];
      targetChannel = message.channel;
    } else {
      const channelId = parameters[0];

      messageIds = parameters.slice(1);

      if (!/^\d{17,19}$/.test(channelId)) {
        await message.reply(
          "❌ Invalid channel ID format. Please provide a valid Discord channel ID.",
        );

        return;
      }

      targetChannel = message.client.channels.cache.get(channelId);

      if (!targetChannel || !targetChannel.isTextBased()) {
        await message.reply("❌ Channel not found or is not a text channel.");

        return;
      }
    }

    for (const messageId of messageIds)
      if (!/^\d{17,19}$/.test(messageId)) {
        await message.reply(
          "❌ Invalid message ID format. Please provide valid Discord message IDs.",
        );

        return;
      }

    try {
      if (messageIds.length > 1) {
        try {
          const messagesToDelete = [];
          let failedFetchCount = 0;

          for (const messageId of messageIds)
            try {
              const targetMessage =
                await targetChannel.messages.fetch(messageId);

              messagesToDelete.push(targetMessage);
            } catch {
              failedFetchCount += 1;
            }

          if (messagesToDelete.length === 0) {
            await message.reply("❌ No valid messages found to delete.");

            return;
          }

          if ("bulkDelete" in targetChannel && messagesToDelete.length > 1) {
            try {
              await targetChannel.bulkDelete(messagesToDelete);
              await message.delete();

              if (failedFetchCount > 0)
                console.warn(
                  `Failed to fetch ${failedFetchCount} out of ${messageIds.length} messages for bulk delete`,
                );
            } catch (bulkError) {
              console.warn(
                "Bulk delete failed, falling back to individual deletion:",
                bulkError,
              );

              let failedDeleteCount = 0;

              for (const message of messagesToDelete)
                try {
                  await message.delete();
                } catch {
                  failedDeleteCount += 1;
                }

              await message.delete();

              if (failedDeleteCount > 0 || failedFetchCount > 0)
                console.warn(
                  `Failed to delete ${failedDeleteCount} messages and fetch ${failedFetchCount} messages`,
                );
            }
          } else {
            let failedDeleteCount = 0;

            for (const message of messagesToDelete)
              try {
                await message.delete();
              } catch {
                failedDeleteCount += 1;
              }

            await message.delete();

            if (failedDeleteCount > 0 || failedFetchCount > 0)
              console.warn(
                `Failed to delete ${failedDeleteCount} messages and fetch ${failedFetchCount} messages`,
              );
          }
        } catch (error) {
          console.error("Error in bulk delete process:", error);
          await message.reply(
            "❌ Failed to delete messages. Check bot permissions and try again.",
          );
        }
      } else {
        try {
          const targetMessage = await targetChannel.messages.fetch(
            messageIds[0],
          );

          await targetMessage.delete();
          await message.delete();
        } catch {
          await message.reply(
            "❌ Failed to delete message. Check bot permissions and try again.",
          );
        }
      }
    } catch (error) {
      console.error("Error deleting messages:", error);
      await message.reply(
        "❌ Failed to delete messages. Check bot permissions and try again.",
      );
    }
  }
};