summaryrefslogtreecommitdiff
path: root/packages/gateway/src/commands/say.ts
blob: 46965744d2c2dbdf18a5a59d12fc1e32667f12bb (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
import { Client, Events, Message } from "discord.js";
import { GUILD_ID } from "../constants";

export const handleSayCommand = (client: Client) => {
  client.on(Events.MessageCreate, async (message: Message) => {
    if (message.author.bot) return;

    if (message.content.toLowerCase().startsWith("uma!say")) {
      const application = await 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 message.reply(
          "❌ Usage: `uma!say <channel_mention_or_message_id> <message>`\nExamples:\n- `uma!say #general Hello everyone!`\n- `uma!say 1234567890123456789 Thanks for the info!`",
        );

        return;
      }

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

      if (messageIdMatch) {
        try {
          const guild = client.guilds.cache.get(GUILD_ID);

          if (!guild) {
            await message.reply("❌ Guild not found.");

            return;
          }

          let foundMessage = null;

          for (const channel of guild.channels.cache.values()) {
            if (channel.isTextBased()) {
              try {
                foundMessage = await channel.messages.fetch(firstParameter);

                if (foundMessage) {
                  targetChannel = channel;
                  targetMessage = foundMessage;

                  break;
                }
              } catch {
                continue;
              }
            }
          }

          if (!foundMessage) {
            await message.reply("❌ Message not found.");

            return;
          }
        } catch {
          await message.reply("❌ Error finding message.");

          return;
        }
      } else {
        const channelMatch = firstParameter.match(/<#(\d+)>/);

        if (!channelMatch) {
          await message.reply(
            "❌ Please mention a channel or provide a message ID. Example: `#general` or `1234567890123456789`",
          );

          return;
        }

        const channelId = channelMatch[1];

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

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

          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 message.reply(
            "❌ Failed to execute the say command. Please check permissions.",
          );
        } catch (replyError) {
          console.error("Failed to send error reply:", replyError);
        }
      }
    }
  });
};