summaryrefslogtreecommitdiff
path: root/packages/gateway/src/commands/sayd.ts
blob: 8bb64f2838a9e18d31f53dfb7190a5a39c7c667d (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
import { Message } from "discord.js";
import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities";
import { parseCommandDurationToMilliseconds } from "./parseCommandDuration";

export const handleSaydCommand = async (message: Message): Promise<boolean> => {
  if (message.author.bot) return false;

  const content = message.content.trim();
  const commandMatch = content.match(/^uma!sayd\s+(\d+)\s+(\S+)\s+(.+)$/s);

  if (!commandMatch) return false;

  const [, channelId, timeout, messageContent] = commandMatch;
  const timeoutMilliseconds = parseCommandDurationToMilliseconds(timeout);

  if (!timeoutMilliseconds) {
    await replyWithCleanup(
      message,
      "❌ Invalid timeout format. Use: `<number><s|m|h|d>`\nExamples: `10s`, `5m`, `1h`",
    );

    return true;
  }

  if (!messageContent.trim()) {
    await replyWithCleanup(
      message,
      "❌ You need to provide a message to send.",
    );

    return true;
  }

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

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

      return true;
    }

    const sentMessage = await targetChannel.send(messageContent);

    setTimeout(async () => {
      try {
        await sentMessage.delete();
      } catch (error) {
        logUnexpectedDiscordAPIError(error);
      }
    }, timeoutMilliseconds);

    await replyWithCleanup(
      message,
      `✅ Message sent to <#${channelId}> and will be deleted in ${timeout}.`,
    );

    return true;
  } catch (error) {
    logUnexpectedDiscordAPIError(error);
    await replyWithCleanup(
      message,
      "❌ Failed to send message to the specified channel.",
    );

    return true;
  }
};