Skip to content

Commit

Permalink
Merge branch 'v6' into features
Browse files Browse the repository at this point in the history
  • Loading branch information
Faf4a authored Oct 6, 2024
2 parents 73995c5 + bc74f2f commit 072a1ae
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
62 changes: 62 additions & 0 deletions src/functions/ForEachObjectArray.js
Original file line number Diff line number Diff line change
@@ -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)
};
};
10 changes: 7 additions & 3 deletions src/functions/awaitData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
30 changes: 29 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,34 @@ export type Interpreter = (
sendMessage?: boolean
) => Promise<InterpreterReturn>;

/**
* Available custom function types that FunctionManager supports.
*/
export type CustomFunctionTypes = "aoi.js" | "djs";

/**
* Base custom function interface.
*/
export interface BaseCustomFunction<T extends CustomFunctionTypes> {
name: `$${string}`;
type: T;
}

/**
* Represents the structure for an aoi.js custom function type.
*/
export interface CustomAoiJSFunction<Type = "aoi.js"> extends BaseCustomFunction<Type> {
params?: string[];
code: string;
}

/**
* Represents the structure for a discord.js custom function type.
*/
export interface CustomDiscordJSFunction<Type = "djs"> extends BaseCustomFunction<Type> {
code(d: Data): Promise<Record<string, any>> | Record<string, any>
}

// FunctionManager
export declare class FunctionManager {
client: AoiClient;
Expand All @@ -395,7 +423,7 @@ export declare class FunctionManager {
interpreter: Interpreter;
constructor(client: AoiClient);
cacheFunctions(): void;
createFunction(data: Array<Record<string, any>>): void;
createFunction(...data: (CustomAoiJSFunction | CustomDiscordJSFunction)[]): void;
findFunctions(code: string): string[];
serializeCode(code: string): string[];
}
Expand Down

0 comments on commit 072a1ae

Please sign in to comment.