From bc74f2f8473678507ed7315c357b983b08736398 Mon Sep 17 00:00:00 2001 From: supremesupreme <91563714+supremesupreme@users.noreply.github.com> Date: Sat, 5 Oct 2024 18:27:49 +0100 Subject: [PATCH] $forEachObjectArray function (#661) --- src/functions/ForEachObjectArray.js | 62 +++++++++++++++++++++++++++++ src/functions/awaitData.js | 10 +++-- 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/functions/ForEachObjectArray.js diff --git a/src/functions/ForEachObjectArray.js b/src/functions/ForEachObjectArray.js new file mode 100644 index 000000000..dc090c50d --- /dev/null +++ b/src/functions/ForEachObjectArray.js @@ -0,0 +1,62 @@ +const Interpreter = require("../core/interpreter.js"); +/** + * @param {import("..").Data} d + */ +module.exports = async (d) => { + const data = d.util.aoiFunc(d); + if (data.err) return d.error(data.err); + + const [name, property, awaitedCmd, endCmd] = data.inside.splits; + + if (!d.data.objects?.[name]) { + return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Object With Name '" + name + "' Does Not Exist."); + } + + if (!property) { + return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Property not found. "); + } + + let cmd = d.client.cmd.awaited.find((c) => c.name.toLowerCase() === awaitedCmd.toLowerCase()); + + if (!cmd) { + return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Awaited Command With Name '" + awaitedCmd + "' Does Not Exist."); + } + + let el = {}; + let i = 0; + for (const key in d.data.objects[name]) { + if (d.data.objects[name].hasOwnProperty(key)) { + + el[key] = d.data.objects[name][key]; + const c = { ...cmd }; + c.code = c.code.replaceAll("{value}", el); + + if(!Array.isArray(el[key])) continue; + el[key] = d.data.objects[name][key] + let parsedResult = JSON.stringify(el[property][i]) + await Interpreter( + d.client, + d.message, + d.args, + c, + d.client.db, + true, + undefined, + { ...d.data, awaitData: parsedResult, index: i } + ); + i++; + } +} + +if (endCmd.trim() !== "") { + const cmd = d.client.cmd.awaited.find((x) => x.name.toLowerCase() === endCmd.addBrackets().toLowerCase()); + if (!cmd) return; + await d.interpreter(d.client, d.message, d.args, cmd, d.client.db, false, undefined, { + index: i-1 + }); +} + + return { + code: d.util.setCode(data) + }; +}; \ No newline at end of file diff --git a/src/functions/awaitData.js b/src/functions/awaitData.js index 25b7c3991..7d98fcf1e 100644 --- a/src/functions/awaitData.js +++ b/src/functions/awaitData.js @@ -7,9 +7,13 @@ module.exports = (d) => { const [option] = data.inside.splits; try { - data.result = eval(`d.data.awaitData?.${option.addBrackets()}`); - } catch (e) { - data.result = undefined; + if(option){ + data.result = eval(`d.data.awaitData?.${option}`); + } else { + data.result = eval(`d.data.awaitData`); + } + } catch (e){ + return d.aoiError.fnError(d, "custom", {}, e); } return { code: d.util.setCode(data),