diff --git a/backend/package-lock.json b/backend/package-lock.json index b1d6d1891..4ca2b90b3 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 2ff53482a..50bf6a30c 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/docs.ts b/backend/src/plugins/Tags/docs.ts index bc1608bbd..0cf2ed7e1 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})` : ""} **${def.description}**\n __Usage__: \`{${def.name}${usage}}\`\n ${examples ? `__Examples__:\n${examples}` : ""}\n\n diff --git a/backend/src/plugins/Tags/templateFunctions.ts b/backend/src/plugins/Tags/templateFunctions.ts index 67a490690..d30657e88 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,27 @@ 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: "abs", + description: "Returns the absolute of a number", + returnValue: "number", + arguments: ["number"], + examples: ["abs(-1.2345)"], + }, { name: "add", description: "Adds two or more numbers", @@ -149,6 +184,120 @@ 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", + returnValue: "number", + 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", @@ -163,4 +312,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: "trim_text", + description: "Trims all non-numeric characters from a string", + returnValue: "string", + 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", + 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 3a7eb2b32..433ac09e9 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(); diff --git a/backend/src/plugins/Tags/util/renderTagBody.ts b/backend/src/plugins/Tags/util/renderTagBody.ts index e75a12cfc..e2b8b4033 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 ""; diff --git a/backend/src/templateFormatter.ts b/backend/src/templateFormatter.ts index 11c179251..a4a11b1df 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"; @@ -395,6 +396,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; @@ -419,8 +424,21 @@ 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) { + if (isNaN(arg)) return 0; + return Math.floor(parseFloat(arg)); + }, + ceil(arg) { + 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; @@ -448,6 +466,89 @@ 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)); + }, + 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) { + 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]; + } + }, cases(mod, ...cases) { if (cases.length === 0) return ""; if (isNaN(mod)) return ""; @@ -458,6 +559,23 @@ const baseValues = { const mod = Math.floor(Math.random() * cases.length) + 1; return baseValues.cases(mod, ...cases); }, + trim_text(str) { + 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 { + if (typeof value === "number") value = value.toString(); + return convertBase(value, from, to); + } catch (_) { + return ""; + } + }, }; export async function renderTemplate(