summaryrefslogtreecommitdiff
path: root/packages/gateway/src/commands/delete.ts
blob: 20d088162e5b0c78f836eb356e535fdfdf0d55d2 (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
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]`\nExamples:\n- `uma!delete 1234567890123456789` (current channel)\n- `uma!delete 1234567890123456789 9876543210987654321` (specific channel)",
      );

      return;
    }

    const messageId = parameters[0];
    const channelId = parameters[1];

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

      return;
    }

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

      return;
    }

    try {
      let targetMessage = null;
      let targetChannel = null;

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

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

          return;
        }
      } else {
        targetChannel = message.channel;
      }

      try {
        targetMessage = await targetChannel.messages.fetch(messageId);
      } catch {
        await message.reply("❌ Message not found in the specified channel.");

        return;
      }

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