summaryrefslogtreecommitdiff
path: root/packages/gateway/src/commands/sayd.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gateway/src/commands/sayd.ts')
-rw-r--r--packages/gateway/src/commands/sayd.ts75
1 files changed, 75 insertions, 0 deletions
diff --git a/packages/gateway/src/commands/sayd.ts b/packages/gateway/src/commands/sayd.ts
new file mode 100644
index 0000000..8bb64f2
--- /dev/null
+++ b/packages/gateway/src/commands/sayd.ts
@@ -0,0 +1,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;
+ }
+};