From 9c479f5386123788f408bf494fb754e8eb912305 Mon Sep 17 00:00:00 2001 From: CGX <92705245+Cyberghxst@users.noreply.github.com> Date: Sat, 12 Oct 2024 05:18:06 -0600 Subject: [PATCH] feat: $switch function (#663) --- src/functions/switch.js | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/functions/switch.js diff --git a/src/functions/switch.js b/src/functions/switch.js new file mode 100644 index 000000000..b08780844 --- /dev/null +++ b/src/functions/switch.js @@ -0,0 +1,46 @@ +const PLACEHOLDER_PATTERN = /\{.*?\}/g + +/** + * @param {import("..").Data} d + */ +module.exports = async d => { + const data = d.util.aoiFunc(d) + const [value, caseString] = data.inside.splits + + const cases = (caseString.match(PLACEHOLDER_PATTERN) ?? []) + .map((v) => { + const [type, value, awaitedName] = v.slice(1, -1).split(":") + return type === "case" ? { type, value, awaitedName } : { type, awaitedName: value } + }) + + if (cases.length < 1) { + return d.aoiError.fnError(d, "custom", {}, "You must provide cases to evaluate!") + } + + const foundCase = cases.find((c) => c.type === "case" && c.value === value) + const defaultCase = cases.find((c) => c.type === "default") + + if (foundCase) { + const awaitedCommand = d.client.cmd.awaited.find((cmd) => cmd.name.toLowerCase() === foundCase.awaitedName.toLowerCase()) + if (!awaitedCommand) { + return d.aoiError.fnError(d, "custom", {}, `Invalid awaited command name "${foundCase.awaitedName}" provided.`) + } + + const result = await d.interpreter(d.client, d.message, d.args, awaitedCommand, d.client.db, true, undefined, d.data) + data.result = result.code + } + + if (!foundCase && defaultCase) { + const awaitedCommand = d.client.cmd.awaited.find((cmd) => cmd.name.toLowerCase() === defaultCase.awaitedName.toLowerCase()) + if (!awaitedCommand) { + return d.aoiError.fnError(d, "custom", {}, `Invalid awaited command name "${defaultCase.awaitedName}" provided.`) + } + + const result = await d.interpreter(d.client, d.message, d.args, awaitedCommand, d.client.db, true, undefined, d.data) + data.result = result.code + } + + return { + code: d.util.setCode(data) + } +} \ No newline at end of file