summaryrefslogtreecommitdiff
path: root/packages/gateway/src/commands/pin.ts
blob: 1667c50cb737289a9c4d56d952cb2685ad64ff18 (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
import { Message } from "discord.js";
import { replyWithCleanup } from "../utilities";

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

  if (!message.content.startsWith("uma!pin")) return;

  const application = await message.client.application?.fetch();
  const ownerId = application?.owner?.id;

  if (message.author.id !== ownerId) return;

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

  if (parameters.length < 2) {
    await replyWithCleanup(
      message,
      "❌ Usage: `uma!pin <message_id> [channel_id]`",
    );

    return;
  }

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

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

    return;
  }

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

    return;
  }

  try {
    let targetChannel = message.channel;

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

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

        return;
      }

      targetChannel = specifiedChannel;
    }

    const targetMessage = await targetChannel.messages.fetch(messageId);

    if (!targetMessage) {
      await replyWithCleanup(
        message,
        "❌ Message not found in the specified channel.",
      );

      return;
    }

    if (targetMessage.pinned) {
      await replyWithCleanup(message, "❌ Message is already pinned.");

      return;
    }

    await message.delete();
    await targetMessage.pin();
  } catch (error) {
    console.error("Error pinning message:", error);

    if (
      error instanceof Error &&
      error.message.includes("Missing Permissions")
    ) {
      await replyWithCleanup(
        message,
        "❌ Missing permissions to pin messages in this channel.",
      );
    } else if (
      error instanceof Error &&
      error.message.includes("Maximum number of pins")
    ) {
      await replyWithCleanup(
        message,
        "❌ Channel has reached maximum number of pinned messages (50). Unpin another message first.",
      );
    } else {
      await replyWithCleanup(
        message,
        "❌ Failed to pin the message. Message ID may be invalid.",
      );
    }
  }
};