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

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

  if ((message as any).saycHandled) return;

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

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

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

    if (parameters.length < 2) {
      await replyWithCleanup(
        message,
        "❌ Usage: `uma!say <channel_id> [message_id] <message>`\nExamples:\n- `uma!say 1234567890123456789 Hello everyone!`\n- `uma!say 1234567890123456789 9876543210987654321 Thanks for the info!`",
      );

      return;
    }

    const firstParameter = parameters[0];
    const secondParameter = parameters[1];
    const messageContent = parameters.slice(2).join(" ");
    let targetChannel: any;
    let targetMessage: any = null;
    const channelIdMatch = firstParameter.match(/^\d{17,19}$/);

    if (!channelIdMatch) {
      await replyWithCleanup(
        message,
        "❌ First parameter must be a channel ID (17-19 digits). Example: `1234567890123456789`",
      );

      return;
    }

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

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

      return;
    }

    const messageIdMatch = secondParameter?.match(/^\d{17,19}$/);

    if (messageIdMatch) {
      try {
        targetMessage = await targetChannel.messages.fetch(secondParameter);
      } catch {
        await replyWithCleanup(
          message,
          "❌ Message not found in the specified channel.",
        );

        return;
      }
    }

    if (!messageIdMatch && parameters.length < 2) {
      await replyWithCleanup(
        message,
        "❌ Usage: `uma!say <channel_id> [message_id] <message>`\nExamples:\n- `uma!say 1234567890123456789 Hello everyone!`\n- `uma!say 1234567890123456789 9876543210987654321 Thanks for the info!`",
      );

      return;
    }

    if (messageIdMatch && parameters.length < 3) {
      await replyWithCleanup(
        message,
        "❌ When providing a message ID, you need at least 3 parameters: channel_id, message_id, and message content.",
      );

      return;
    }

    try {
      await message.delete();

      const baseDuration = Math.max(1, messageContent.length / 20);
      const complexityMultiplier =
        (messageContent.match(/[.!?]/g) || []).length * 0.5;
      const wordCount = messageContent.split(" ").length;
      const wordComplexityMultiplier = Math.min(wordCount / 10, 2);
      const typingDuration = Math.min(
        baseDuration + complexityMultiplier + wordComplexityMultiplier,
        8,
      );

      await (targetChannel as any).sendTyping();
      await new Promise((resolve) =>
        setTimeout(resolve, typingDuration * 1000),
      );

      if (targetMessage) {
        await targetMessage.reply(messageContent);
      } else {
        await (targetChannel as any).send(messageContent);
      }
    } catch (error) {
      console.error("Error executing say command:", error);

      try {
        await replyWithCleanup(
          message,
          "❌ Failed to execute the say command. Please check permissions.",
        );
      } catch (replyError) {
        console.error("Failed to send error reply:", replyError);
      }
    }
  }
};