aboutsummaryrefslogtreecommitdiff
path: root/src/api/Commands/commandHelpers.ts
diff options
context:
space:
mode:
authorVendicated <vendicated@riseup.net>2022-10-12 22:22:21 +0200
committerVendicated <vendicated@riseup.net>2022-10-12 22:22:21 +0200
commit267b2b1a0703313311da01addafcee28100347ea (patch)
tree8e074ea220ba10d168f69b92ebc40ea608476ae7 /src/api/Commands/commandHelpers.ts
parent83d480a68cfed2adee18b9a6b74634644cefaacc (diff)
downloadVencord-267b2b1a0703313311da01addafcee28100347ea.tar.gz
Vencord-267b2b1a0703313311da01addafcee28100347ea.tar.bz2
Vencord-267b2b1a0703313311da01addafcee28100347ea.zip
Commands: basic error handling
Diffstat (limited to 'src/api/Commands/commandHelpers.ts')
-rw-r--r--src/api/Commands/commandHelpers.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/api/Commands/commandHelpers.ts b/src/api/Commands/commandHelpers.ts
new file mode 100644
index 0000000..544445b
--- /dev/null
+++ b/src/api/Commands/commandHelpers.ts
@@ -0,0 +1,42 @@
+import { filters, waitFor } from "../../webpack";
+import type { PartialDeep } from "type-fest";
+import { Message } from "discord-types/general";
+import { lazyWebpack, mergeDefaults } from "../../utils/misc";
+import { Argument } from "./types";
+
+const createBotMessage = lazyWebpack(filters.byCode('username:"Clyde"'));
+const MessageSender = lazyWebpack(filters.byProps(["receiveMessage"]));
+
+let SnowflakeUtils: any;
+waitFor("fromTimestamp", m => SnowflakeUtils = m);
+
+export function generateId() {
+ return `-${SnowflakeUtils.fromTimestamp(Date.now())}`;
+}
+
+/**
+ * Send a message as Clyde
+ * @param {string} channelId ID of channel to send message to
+ * @param {Message} message Message to send
+ * @returns {Message}
+ */
+export function sendBotMessage(channelId: string, message: PartialDeep<Message>): Message {
+ const botMessage = createBotMessage({ channelId, content: "", embeds: [] });
+
+ MessageSender.receiveMessage(channelId, mergeDefaults(message, botMessage));
+
+ return message as Message;
+}
+
+/**
+ * Get the value of an option by name
+ * @param args Arguments array (first argument passed to execute)
+ * @param name Name of the argument
+ * @param fallbackValue Fallback value in case this option wasn't passed
+ * @returns Value
+ */
+export function findOption<T>(args: Argument[], name: string): T & {} | undefined;
+export function findOption<T>(args: Argument[], name: string, fallbackValue: T): T & {};
+export function findOption(args: Argument[], name: string, fallbackValue?: any) {
+ return (args.find(a => a.name === name)?.value || fallbackValue) as any;
+}