Calculate advanced or simple math equations. Can calculate simple algebra. Use * to times and ** or ^ for 'to power of'. This snippet requires a slash command with a string input for the equation
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
var calc = require('ez-calculator');
function replaceAll(str, find, replace) {
var escapedFind = find.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
return str.replace(new RegExp(escapedFind, 'g'), replace);
}
let problem = context.params.event.data.options[0].value;
let initialProblem = problem; //create initial problem so the response problem includes original syntax
console.log(problem);
//create exponents properly
problem = replaceAll(problem, `^`, `**`);
console.log(problem);
var result = calc.calculate(problem);
//prevents bold and italics in initial problem
initialProblem = replaceAll(initialProblem, `*`, `\\*`);
console.log(initialProblem);
await lib.discord.interactions['@1.0.1'].responses.create({
token: `${context.params.event.token}`,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
content: '',
embeds: [
{
type: 'rich',
title: `Calculator 🧮`,
description: `${initialProblem} = \`${result}\``,
color: 0x0051ff, // dark blue
footer: {
text: result ? `` : `Remember to use * to multiply. X is a pronumeral`,
},
},
],
});