1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-18 04:40:59 -05:00
Daggerbot-TS/src/commands/calculator.ts

45 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-09-17 06:23:47 -04:00
import Discord from 'discord.js';
import TClient from '../client.js';
import MessageTool from '../helpers/MessageTool.js';
2023-12-24 10:21:40 -05:00
import Formatters from '../helpers/Formatters.js';
2024-02-28 02:18:28 -05:00
import * as math from 'mathjs';// I hate this, but it doesn't provide a default export though :cimpCopium:
2023-09-17 06:23:47 -04:00
2023-12-24 10:21:40 -05:00
export default class Calculator {
static run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
2023-09-17 06:23:47 -04:00
const now = Date.now();
const exp = interaction.options.getString('expression', true).replace(/[^-()\d/*+.]/g, '');
try {
2024-02-28 02:18:28 -05:00
let result:math.EvalFunction;
2023-09-17 06:23:47 -04:00
switch (exp) {
case '1+1':
2024-02-28 02:18:28 -05:00
return interaction.reply('Wh-why are you doing this? I mean... **1+1** is **2**, but... *why?*');
2023-09-17 06:23:47 -04:00
case '2+2':
2024-02-28 02:18:28 -05:00
return interaction.reply('https://c.tenor.com/5-bc8MtSWCYAAAAC/tenor.gif');
case '2+2-1':
case '1-2+2':
return interaction.reply('https://tenor.com/bhI9r.gif');
2023-09-17 06:23:47 -04:00
case '0/0':
return interaction.reply(MessageTool.concatMessage(
'Imagine that you have zero cookies and you split them evenly among zero friends.',
'How many cookies does each person get? See? It doesn\'t make sense.',
'And Cookie Monster is sad that there are no cookies, and you are sad that you have no friends.',
'-- Siri, 2015'
));
2024-02-28 02:18:28 -05:00
default:
result = math.evaluate(exp);
break;
}
if (typeof result !== 'undefined') interaction.reply({embeds:[new client.embed().setColor(client.config.embedColor).addFields({name: 'Expression', value: `\`\`\`js\n${exp}\n\`\`\``},{name: 'Answer', value: `\`\`\`js\n${result}\n\`\`\``}).setFooter({text: `Time taken: ${Formatters.timeFormat(Date.now() - now, 3)}`})]})
2023-09-17 06:23:47 -04:00
} catch {
interaction.reply('The given expression is invalid.');
}
2023-12-24 10:21:40 -05:00
}
static data = new Discord.SlashCommandBuilder()
2023-09-17 06:23:47 -04:00
.setName('calculator')
.setDescription('Calculate a math expression or simple 2+2')
.addStringOption(x=>x
.setName('expression')
.setDescription('The expression to be calculated')
.setRequired(true))
}