From c0f3ac11f0b2a29ae62b7532ba5f13bf23f08cfd Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 09:44:10 +0000 Subject: [PATCH 01/15] floor/ceil Signed-off-by: GitHub --- backend/src/templateFormatter.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index 11c17925..9e5e7856 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -421,6 +421,14 @@ const baseValues = { if (isNaN(arg)) return 0; return decimals === 0 ? Math.round(arg) : arg.toFixed(decimals); }, + floor(arg) { + if (isNaN(arg)) return 0; + return Math.floor(parseFloat(arg)); + }, + ceil(arg) { + if (isNaN(arg)) return 0; + return Math.ceil(parseFloat(arg)); + }, add(...args) { return args.reduce((result, arg) => { if (isNaN(arg)) return result; From bf8900b6a5e3a156f95db64e184447aa48ac0c3c Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 09:46:26 +0000 Subject: [PATCH 02/15] get_user Signed-off-by: GitHub --- backend/src/plugins/Tags/util/renderTagBody.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backend/src/plugins/Tags/util/renderTagBody.ts b/backend/src/plugins/Tags/util/renderTagBody.ts index e75a12cf..e2b8b403 100644 --- a/backend/src/plugins/Tags/util/renderTagBody.ts +++ b/backend/src/plugins/Tags/util/renderTagBody.ts @@ -1,6 +1,7 @@ import { ExtendedMatchParams, GuildPluginData } from "knub"; import { TemplateSafeValue, TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter"; -import { StrictMessageContent, renderRecursively } from "../../../utils"; +import { StrictMessageContent, UnknownUser, renderRecursively, resolveUser } from "../../../utils"; +import { userToTemplateSafeUser } from "../../../utils/templateSafeObjects.js"; import { TTag, TagsPluginType } from "../types"; import { findTagByName } from "./findTagByName"; @@ -39,6 +40,12 @@ export async function renderTagBody( if (emptyObject[name]) return; return !Object.hasOwn(dynamicVars, name) || dynamicVars[name] == null ? "" : dynamicVars[name]; }, + async get_user(str) { + if (!str || typeof str !== "string") return ""; + const resolved = await resolveUser(pluginData.client, str); + if (resolved instanceof UnknownUser) return ""; + return userToTemplateSafeUser(resolved); + }, tag: async (name, ...subTagArgs) => { if (++tagFnCallsObj.calls > MAX_TAG_FN_CALLS) return ""; if (typeof name !== "string") return ""; From 54d2c00d17b1a95086de46e437a6e999a7f776bd Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 09:47:36 +0000 Subject: [PATCH 03/15] round parse floats Signed-off-by: GitHub --- backend/src/templateFormatter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index 9e5e7856..142576f4 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -419,6 +419,7 @@ const baseValues = { }, round(arg, decimals = 0) { if (isNaN(arg)) return 0; + if (typeof arg !== "number") arg = parseFloat(arg); // should be safe since we check above if it's not a number return decimals === 0 ? Math.round(arg) : arg.toFixed(decimals); }, floor(arg) { From e2d23aa4afb944532fcb16152f4959a5dea7146e Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 09:48:14 +0000 Subject: [PATCH 04/15] exponential Signed-off-by: GitHub --- backend/src/templateFormatter.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index 142576f4..6382ae48 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -457,6 +457,10 @@ const baseValues = { return result / parseFloat(arg); }, args[0]); }, + exp(base, power) { + if (isNaN(base) || isNaN(power)) return 0; + return Math.pow(parseFloat(base), parseFloat(power)); + }, cases(mod, ...cases) { if (cases.length === 0) return ""; if (isNaN(mod)) return ""; From ae49782aa41cc10100f35a8e7bdb74200d6e2dda Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 09:48:36 +0000 Subject: [PATCH 05/15] arrlen Signed-off-by: GitHub --- backend/src/templateFormatter.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index 6382ae48..ea9175e7 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -395,6 +395,10 @@ const baseValues = { ucfirst(arg) { return baseValues.upperFirst(arg); }, + arrlen(arg) { + if (!Array.isArray(arg)) return 0; + return arg.length; + }, strlen(arg) { if (typeof arg !== "string") return 0; return [...arg].length; From c44d6b97fe56bb7a87455a9ecb8639441aa5d385 Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 09:50:23 +0000 Subject: [PATCH 06/15] docs work Signed-off-by: GitHub --- backend/src/plugins/Tags/templateFunctions.ts | 168 ++++++++++++++++++ backend/src/plugins/Tags/types.ts | 1 + 2 files changed, 169 insertions(+) diff --git a/backend/src/plugins/Tags/templateFunctions.ts b/backend/src/plugins/Tags/templateFunctions.ts index 67a49069..e0f20ca4 100644 --- a/backend/src/plugins/Tags/templateFunctions.ts +++ b/backend/src/plugins/Tags/templateFunctions.ts @@ -107,6 +107,20 @@ export const TemplateFunctions: TemplateFunction[] = [ arguments: ["string"], examples: ['upperFirst("hello World")'], }, + { + name: "strlen", + description: "Returns the length of a string argument", + returnValue: "number", + arguments: ["string"], + examples: ['strlen("Hello World")'], + }, + { + name: "arrlen", + description: "Returns the length of an array argument", + returnValue: "number", + arguments: ["array"], + examples: ['arrlen(["Hello", "World"])'], + }, { name: "rand", description: "Returns a random number between from and to, optionally using seed", @@ -121,6 +135,20 @@ export const TemplateFunctions: TemplateFunction[] = [ arguments: ["number", "decimalPlaces"], examples: ["round(1.2345, 2)"], }, + { + name: "ceil", + description: "Rounds a number up to the next integer", + returnValue: "number", + arguments: ["number"], + examples: ["ceil(1.2345)"], + }, + { + name: "floor", + description: "Rounds a number down to the next integer", + returnValue: "number", + arguments: ["number"], + examples: ["floor(1.2345)"], + }, { name: "add", description: "Adds two or more numbers", @@ -149,6 +177,13 @@ export const TemplateFunctions: TemplateFunction[] = [ arguments: ["number1", "number2", "..."], examples: ["div(6, 2)"], }, + { + name: "exp", + description: "Raises a number to the power of another number", + returnValue: "number", + arguments: ["base", "power"], + examples: ["exp(2, 3)"], + }, { name: "cases", description: "Returns the argument at position", @@ -163,4 +198,137 @@ export const TemplateFunctions: TemplateFunction[] = [ arguments: ["argument1", "argument2", "..."], examples: ['choose("Hello", "World", "!")'], }, + { + name: "map", + description: "Returns the value of the key of object, array or single value", + returnValue: "any", + arguments: ["object | array", "key"], + examples: ['map(user, "id")'], + }, + { + name: "find_i", + description: "Returns the index of the first argument in the array", + returnValue: "number", + arguments: ["array", "value"], + examples: ['find_i(["Hello", "World"], "World")'], + }, + { + name: "get_snowflake", + description: "Trims all non-numeric characters from a string", + returnValue: "string", + arguments: ["string"], + examples: ['get_snowflake("<@!344837487526412300>")'], + }, + { + name: "tag", + description: "Gets the value of another defined tag", + returnValue: "string", + arguments: ["tagName"], + examples: ['tag("tagName")'], + plugin: "tags", + }, + { + name: "get", + description: "Gets the value of a saved variable", + returnValue: "any", + arguments: ["variable"], + examples: ['get("variable")'], + plugin: "tags", + }, + { + name: "set", + description: "Sets the value of a saved variable", + returnValue: "none", + arguments: ["variableName", "value"], + examples: ['set("variableName", "value")'], + plugin: "tags", + }, + { + name: "setr", + description: "Sets the value of a saved variable and returns it", + returnValue: "any", + arguments: ["variableName", "value"], + examples: ['setr("variableName", "value")'], + plugin: "tags", + }, + { + name: "parseDateTime", + description: "Parses a date string/unix timestamp into a formated Date string", + returnValue: "string", + arguments: ["date"], + examples: ["parseDateTime(1643411583656)", 'parseDateTime("2020-01-01T00:00:00.000Z")'], + plugin: "tags", + }, + { + name: "countdown", + description: "Returns a countdown string to target timestamp", + returnValue: "string", + arguments: ["timestamp"], + examples: ["countdown(1577886400000)"], + plugin: "tags", + }, + { + name: "now", + description: "Returns the current timestamp", + returnValue: "number", + arguments: [], + examples: ["now()"], + plugin: "tags", + }, + { + name: "timeAdd", + description: "Adds a delay to a timestamp", + returnValue: "number", + arguments: ["timestamp", "delay"], + examples: ['timeAdd(1577886400000, "1h")', 'timeAdd("1h")'], + plugin: "tags", + }, + { + name: "timeSub", + description: "Subtracts a delay from a timestamp", + returnValue: "number", + arguments: ["timestamp", "delay"], + examples: ['timeSub(1577886400000, "1h")', 'timeSub("1h")'], + plugin: "tags", + }, + { + name: "timeAgo", + description: "Alias for timeSub", + returnValue: "number", + arguments: ["delay"], + examples: ['timeAgo("2h")'], + plugin: "tags", + }, + { + name: "formatTime", + description: "Formats a timestamp into a human readable string", + returnValue: "string", + arguments: ["timestamp", "formatStyle"], + examples: ['formatTime(now(), "YYYY-MM-DD HH")', 'formatTime(1577886400000, "YYYY-MM-DD")'], + plugin: "tags", + }, + { + name: "mention", + description: "Converts a snowflake to a mention", + returnValue: "string", + arguments: ["snowflake"], + examples: ["mention('344837487526412300')"], + plugin: "tags", + }, + { + name: "isMention", + description: "Checks if a string is a mention", + returnValue: "boolean", + arguments: ["string"], + examples: ['isMention("<@!344837487526412300>")'], + plugin: "tags", + }, + { + name: "get_user", + description: "Tries to resolve a user from ID or mention", + returnValue: 'ResolvedUser || ""', + arguments: ["string"], + examples: ['get_user("<@!344837487526412300>")', "get_user(get_snowflake(args.0))"], + plugin: "tags", + }, ]; diff --git a/backend/src/plugins/Tags/types.ts b/backend/src/plugins/Tags/types.ts index 3a7eb2b3..433ac09e 100644 --- a/backend/src/plugins/Tags/types.ts +++ b/backend/src/plugins/Tags/types.ts @@ -66,6 +66,7 @@ export interface TemplateFunction { returnValue: string; signature?: string; examples?: string[]; + plugin?: string; } export const tagsCmd = guildPluginMessageCommand(); From c6f838f3337f704c8b5a4406d1fda6a7221c014c Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 09:54:36 +0000 Subject: [PATCH 07/15] map + get_snowflake Signed-off-by: GitHub --- backend/src/templateFormatter.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index ea9175e7..7ff24761 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -465,6 +465,12 @@ const baseValues = { if (isNaN(base) || isNaN(power)) return 0; return Math.pow(parseFloat(base), parseFloat(power)); }, + map(obj, key) { + if (Array.isArray(obj)) { + return obj.map((tobj) => tobj[key]); + } + return obj[key]; + }, cases(mod, ...cases) { if (cases.length === 0) return ""; if (isNaN(mod)) return ""; @@ -475,6 +481,10 @@ const baseValues = { const mod = Math.floor(Math.random() * cases.length) + 1; return baseValues.cases(mod, ...cases); }, + get_snowflake(str) { + if (!str || typeof str !== "string") return ""; + return str.replaceAll(/[^\d]+/g, ""); + }, }; export async function renderTemplate( From 77f414925f83b648a108497e8b54b16dc6068a4f Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 09:55:55 +0000 Subject: [PATCH 08/15] rename get_snowflake Signed-off-by: GitHub --- backend/src/plugins/Tags/templateFunctions.ts | 11 ++--------- backend/src/templateFormatter.ts | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/backend/src/plugins/Tags/templateFunctions.ts b/backend/src/plugins/Tags/templateFunctions.ts index e0f20ca4..ebf1415a 100644 --- a/backend/src/plugins/Tags/templateFunctions.ts +++ b/backend/src/plugins/Tags/templateFunctions.ts @@ -206,18 +206,11 @@ export const TemplateFunctions: TemplateFunction[] = [ examples: ['map(user, "id")'], }, { - name: "find_i", - description: "Returns the index of the first argument in the array", - returnValue: "number", - arguments: ["array", "value"], - examples: ['find_i(["Hello", "World"], "World")'], - }, - { - name: "get_snowflake", + name: "trim_text", description: "Trims all non-numeric characters from a string", returnValue: "string", arguments: ["string"], - examples: ['get_snowflake("<@!344837487526412300>")'], + examples: ['trim_text("<@!344837487526412300>")'], }, { name: "tag", diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index 7ff24761..a69640a6 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -481,7 +481,7 @@ const baseValues = { const mod = Math.floor(Math.random() * cases.length) + 1; return baseValues.cases(mod, ...cases); }, - get_snowflake(str) { + trim_text(str) { if (!str || typeof str !== "string") return ""; return str.replaceAll(/[^\d]+/g, ""); }, From e1996e35f6de28a21228d9f80648ea398f87d09a Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 09:58:40 +0000 Subject: [PATCH 09/15] improve docs Signed-off-by: GitHub --- backend/src/plugins/Tags/docs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/plugins/Tags/docs.ts b/backend/src/plugins/Tags/docs.ts index bc1608bb..c528d1b0 100644 --- a/backend/src/plugins/Tags/docs.ts +++ b/backend/src/plugins/Tags/docs.ts @@ -7,7 +7,7 @@ export function generateTemplateMarkdown(definitions: TemplateFunction[]): strin const usage = def.signature ?? `(${def.arguments.join(", ")})`; const examples = def.examples?.map((ex) => `> \`{${ex}}\``).join("\n") ?? null; return trimPluginDescription(` - ## ${def.name} + ## ${def.name}${def.plugin ? ` (${def.plugin} only)` : ""} **${def.description}**\n __Usage__: \`{${def.name}${usage}}\`\n ${examples ? `__Examples__:\n${examples}` : ""}\n\n From 13565e2a7af572a988d0618a26aa8c9ab946af30 Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 10:37:03 +0000 Subject: [PATCH 10/15] add one or two funcs Signed-off-by: GitHub --- backend/package-lock.json | 14 ++ backend/package.json | 1 + backend/src/plugins/Tags/templateFunctions.ts | 121 ++++++++++++++++++ backend/src/templateFormatter.ts | 80 ++++++++++++ 4 files changed, 216 insertions(+) diff --git a/backend/package-lock.json b/backend/package-lock.json index b1d6d189..4ca2b90b 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.1", "dependencies": { "@silvia-odwyer/photon-node": "^0.3.1", + "baseroo": "^1.2.0", "bufferutil": "^4.0.3", "clinic": "^13.0.0", "cors": "^2.8.5", @@ -1528,6 +1529,14 @@ "node": ">=6.0.0" } }, + "node_modules/baseroo": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/baseroo/-/baseroo-1.2.0.tgz", + "integrity": "sha512-sRLKZGqz42S+BB5uX1OGLwFB3r7a1GlL09qwd+xtb0jAiiSOHPLf/IHu/CL8NcboizJx79SOs9X7K4c8qbfXlw==", + "dependencies": { + "make-error": "^1.3.6" + } + }, "node_modules/bcrypt-pbkdf": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", @@ -5799,6 +5808,11 @@ "semver": "bin/semver.js" } }, + "node_modules/make-error": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", + "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==" + }, "node_modules/manage-path": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/manage-path/-/manage-path-2.0.0.tgz", diff --git a/backend/package.json b/backend/package.json index 2ff53482..50bf6a30 100644 --- a/backend/package.json +++ b/backend/package.json @@ -30,6 +30,7 @@ }, "dependencies": { "@silvia-odwyer/photon-node": "^0.3.1", + "baseroo": "^1.2.0", "bufferutil": "^4.0.3", "clinic": "^13.0.0", "cors": "^2.8.5", diff --git a/backend/src/plugins/Tags/templateFunctions.ts b/backend/src/plugins/Tags/templateFunctions.ts index ebf1415a..d30657e8 100644 --- a/backend/src/plugins/Tags/templateFunctions.ts +++ b/backend/src/plugins/Tags/templateFunctions.ts @@ -149,6 +149,13 @@ export const TemplateFunctions: TemplateFunction[] = [ arguments: ["number"], examples: ["floor(1.2345)"], }, + { + name: "abs", + description: "Returns the absolute of a number", + returnValue: "number", + arguments: ["number"], + examples: ["abs(-1.2345)"], + }, { name: "add", description: "Adds two or more numbers", @@ -177,6 +184,20 @@ export const TemplateFunctions: TemplateFunction[] = [ arguments: ["number1", "number2", "..."], examples: ["div(6, 2)"], }, + { + name: "sqrt", + description: "Calculates the square root of a number", + returnValue: "number", + arguments: ["number"], + examples: ["sqrt(5)"], + }, + { + name: "cbrt", + description: "Calculates the cubic root of a number", + returnValue: "number", + arguments: ["number"], + examples: ["cbrt(50)"], + }, { name: "exp", description: "Raises a number to the power of another number", @@ -184,6 +205,99 @@ export const TemplateFunctions: TemplateFunction[] = [ arguments: ["base", "power"], examples: ["exp(2, 3)"], }, + { + name: "sin", + description: "Returns the sine of a number in radians", + returnValue: "number", + arguments: ["radians"], + examples: ["sin(2)"], + }, + { + name: "sinh", + description: "Returns the hyperbolic sine of a number", + returnValue: "number", + arguments: ["number"], + examples: ["sinh(1)"], + }, + { + name: "tan", + description: "Returns the tangent of a number in radians", + returnValue: "number", + arguments: ["radians"], + examples: ["tan(1.5)"], + }, + { + name: "tanh", + description: "Returns the hyperbolic tangent of a number in radians", + returnValue: "number", + arguments: ["radians"], + examples: ["tanh(1.5)"], + }, + { + name: "cos", + description: "Returns the cosine of a number in radians", + returnValue: "number", + arguments: ["radians"], + examples: ["cos(1.5)"], + }, + { + name: "cosh", + description: "Returns the hyperbolic cosine of a number in radians", + returnValue: "number", + arguments: ["radians"], + examples: ["cosh(1.5)"], + }, + { + name: "hypot", + description: "Returns the square root of the sum of squares of it's arguments", + returnValue: "number", + arguments: ["number1", "number2", "..."], + examples: ["hypot(3, 4, 5, 6)"], + }, + { + name: "log", + description: "Returns the base e logarithm of a number", + returnValue: "number", + arguments: ["number"], + examples: ["log(3)"], + }, + { + name: "log2", + description: "Returns the base 2 logarithm of a number", + returnValue: "number", + arguments: ["number"], + examples: ["log2(3)"], + }, + { + name: "log10", + description: "Returns the base 10 logarithm of a number", + returnValue: "number", + arguments: ["number"], + examples: ["log10(3)"], + }, + { + name: "log1p", + description: "Returns the base e logarithm of a 1 + number", + returnValue: "number", + arguments: ["number"], + examples: ["log1p(3)"], + }, + { + name: "const", + description: "Get value of math constants", + returnValue: "number", + arguments: ["constant_name"], + examples: [ + "const(pi)", + "const(e)", + "const(sqrt2)", + "const(sqrt0.5)", + "const(ln10)", + "const(ln2)", + "const(log10e)", + "const(log2e)", + ], + }, { name: "cases", description: "Returns the argument at position", @@ -212,6 +326,13 @@ export const TemplateFunctions: TemplateFunction[] = [ arguments: ["string"], examples: ['trim_text("<@!344837487526412300>")'], }, + { + name: "convert_base", + description: "Converts a value from base to base", + returnValue: "string", + arguments: ["value", "origin", "dest"], + examples: ['convert_base("256", "10", "2")'], + }, { name: "tag", description: "Gets the value of another defined tag", diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index a69640a6..46070649 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -1,3 +1,4 @@ +import { convertBase } from "baseroo"; import seedrandom from "seedrandom"; import { get, has } from "./utils"; @@ -434,6 +435,10 @@ const baseValues = { if (isNaN(arg)) return 0; return Math.ceil(parseFloat(arg)); }, + abs(arg) { + if (isNaN(arg)) return 0; + return Math.abs(parseFloat(arg)); + }, add(...args) { return args.reduce((result, arg) => { if (isNaN(arg)) return result; @@ -465,6 +470,73 @@ const baseValues = { if (isNaN(base) || isNaN(power)) return 0; return Math.pow(parseFloat(base), parseFloat(power)); }, + sqrt(arg) { + if (isNaN(arg)) return 0; + return Math.sqrt(parseFloat(arg)); + }, + cbrt(arg) { + if (isNaN(arg)) return 0; + return Math.cbrt(parseFloat(arg)); + }, + sin(radians) { + if (isNaN(radians)) return 0; + return Math.sin(parseFloat(radians)); + }, + sinh(arg) { + if (isNaN(arg)) return 0; + return Math.sinh(parseFloat(arg)); + }, + tan(arg) { + if (isNaN(arg)) return 0; + return Math.tan(parseFloat(arg)); + }, + tanh(arg) { + if (isNaN(arg)) return 0; + return Math.tanh(parseFloat(arg)); + }, + log(arg) { + if (isNaN(arg)) return 0; + return Math.log(parseFloat(arg)); + }, + log2(arg) { + if (isNaN(arg)) return 0; + return Math.log2(parseFloat(arg)); + }, + log10(arg) { + if (isNaN(arg)) return 0; + return Math.log10(parseFloat(arg)); + }, + log1p(arg) { + if (isNaN(arg)) return 0; + return Math.log1p(parseFloat(arg)); + }, + hypot(...args) { + if (!args.every((e) => !isNaN(e))) return ""; // TODO: Improve validation + return Math.hypot(...args.map((e) => parseFloat(e))); + }, + cos(arg) { + if (isNaN(arg)) return 0; + return Math.cos(parseFloat(arg)); + }, + cosh(arg) { + if (isNaN(arg)) return 0; + return Math.cosh(parseFloat(arg)); + }, + const(str) { + // math constants lmao :joy: + const math_constants = { + pi: Math.PI, + e: Math.E, + sqrt2: Math.SQRT2, + "sqrt0.5": Math.SQRT1_2, + ln10: Math.LN10, + ln2: Math.LN2, + log10e: Math.LOG10E, + log2e: Math.LOG2E, + }; + if (typeof str !== "string") return ""; + return math_constants[str.toLowerCase()] ?? ""; + }, map(obj, key) { if (Array.isArray(obj)) { return obj.map((tobj) => tobj[key]); @@ -485,6 +557,14 @@ const baseValues = { if (!str || typeof str !== "string") return ""; return str.replaceAll(/[^\d]+/g, ""); }, + convert_base(value, from, to) { + try { + // :joy: + return convertBase(value, from, to); + } catch (_) { + return ""; + } + }, }; export async function renderTemplate( From 032e552f6c81f2b6200469daa9fe2522a7c4b4f4 Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 12:01:32 +0000 Subject: [PATCH 11/15] alias get_snowflake Signed-off-by: GitHub --- backend/src/templateFormatter.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index 46070649..f5b57b15 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -557,6 +557,11 @@ const baseValues = { if (!str || typeof str !== "string") return ""; return str.replaceAll(/[^\d]+/g, ""); }, + get_snowflake(str) { + // couldn't find a better way of aliasing :( + if (!str || typeof str !== "string") return ""; + return str.replaceAll(/[^\d]+/g, ""); + }, convert_base(value, from, to) { try { // :joy: From c428bb14de7cca5734c78bb8c8716a9a60ea8027 Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 12:01:44 +0000 Subject: [PATCH 12/15] convert to string for convert_base Signed-off-by: GitHub --- backend/src/templateFormatter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index f5b57b15..31b71774 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -564,7 +564,7 @@ const baseValues = { }, convert_base(value, from, to) { try { - // :joy: + if (typeof value === "number") value = value.toString(); return convertBase(value, from, to); } catch (_) { return ""; From 65483793696a35a3861b5e25983ef73e17e274dc Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 12:02:08 +0000 Subject: [PATCH 13/15] docs: plugin definition type Signed-off-by: GitHub --- backend/src/plugins/Tags/docs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/plugins/Tags/docs.ts b/backend/src/plugins/Tags/docs.ts index c528d1b0..0cf2ed7e 100644 --- a/backend/src/plugins/Tags/docs.ts +++ b/backend/src/plugins/Tags/docs.ts @@ -7,7 +7,7 @@ export function generateTemplateMarkdown(definitions: TemplateFunction[]): strin const usage = def.signature ?? `(${def.arguments.join(", ")})`; const examples = def.examples?.map((ex) => `> \`{${ex}}\``).join("\n") ?? null; return trimPluginDescription(` - ## ${def.name}${def.plugin ? ` (${def.plugin} only)` : ""} + ## ${def.name}${def.plugin ? ` (${def.plugin})` : ""} **${def.description}**\n __Usage__: \`{${def.name}${usage}}\`\n ${examples ? `__Examples__:\n${examples}` : ""}\n\n From 12d59bc238f08e42e652b775533430ad2a0bef2b Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 13:17:48 +0000 Subject: [PATCH 14/15] fix crash with map Signed-off-by: GitHub --- backend/src/templateFormatter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index 31b71774..3f04fc76 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -538,6 +538,7 @@ const baseValues = { return math_constants[str.toLowerCase()] ?? ""; }, map(obj, key) { + if (typeof obj !== "object" || typeof key !== "string" || typeof obj === "function" || !obj) return ""; if (Array.isArray(obj)) { return obj.map((tobj) => tobj[key]); } From 7e6fc1f1962608827688c8139cb6f00803c4f3ff Mon Sep 17 00:00:00 2001 From: Tiago R Date: Mon, 9 Oct 2023 13:56:49 +0000 Subject: [PATCH 15/15] fuck i owe almeida a donut now Signed-off-by: GitHub --- backend/src/templateFormatter.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index 3f04fc76..a4a11b1d 100644 --- a/backend/src/templateFormatter.ts +++ b/backend/src/templateFormatter.ts @@ -538,11 +538,16 @@ const baseValues = { return math_constants[str.toLowerCase()] ?? ""; }, map(obj, key) { - if (typeof obj !== "object" || typeof key !== "string" || typeof obj === "function" || !obj) return ""; - if (Array.isArray(obj)) { - return obj.map((tobj) => tobj[key]); + return actualMap(obj, key); + + function actualMap(obj, key, depth = 0) { + if (depth > 5) return ""; + if (!obj || !key || typeof obj !== "object" || typeof key !== "string") return ""; + if (Array.isArray(obj)) { + return obj.map((tobj) => actualMap(tobj, key, depth + 1)); + } + return obj[key]; } - return obj[key]; }, cases(mod, ...cases) { if (cases.length === 0) return "";