From 09762316d893043c9ab1803f913f49d588a2c384 Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Sun, 17 Sep 2023 20:23:47 +1000 Subject: [PATCH] Concerned about eval but k --- .pnp.cjs | 102 ++++++++++--------------------------- src/commands/calculator.ts | 50 ++++++++++++++++++ yarn.lock | 99 +++++++++++------------------------ 3 files changed, 106 insertions(+), 145 deletions(-) create mode 100644 src/commands/calculator.ts diff --git a/.pnp.cjs b/.pnp.cjs index 850c749..903ebcc 100644 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -52,10 +52,10 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { }]\ ]],\ ["@discord-player/equalizer", [\ - ["npm:0.2.2", {\ - "packageLocation": "./.yarn/cache/@discord-player-equalizer-npm-0.2.2-4485821a15-33b9991a84.zip/node_modules/@discord-player/equalizer/",\ + ["npm:0.2.3", {\ + "packageLocation": "./.yarn/cache/@discord-player-equalizer-npm-0.2.3-e5859f85ea-6f60c41b48.zip/node_modules/@discord-player/equalizer/",\ "packageDependencies": [\ - ["@discord-player/equalizer", "npm:0.2.2"]\ + ["@discord-player/equalizer", "npm:0.2.3"]\ ],\ "linkType": "HARD"\ }]\ @@ -66,7 +66,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "packageDependencies": [\ ["@discord-player/extractor", "npm:4.4.3"],\ ["file-type", "npm:16.5.4"],\ - ["genius-lyrics", "npm:4.4.3"],\ + ["genius-lyrics", "npm:4.4.6"],\ ["isomorphic-unfetch", "npm:4.0.2"],\ ["node-html-parser", "npm:6.1.5"],\ ["reverbnation-scraper", "npm:2.0.0"],\ @@ -781,37 +781,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "linkType": "HARD"\ }]\ ]],\ - ["cheerio", [\ - ["npm:1.0.0-rc.12", {\ - "packageLocation": "./.yarn/cache/cheerio-npm-1.0.0-rc.12-6785a97c2a-5d4c1b7a53.zip/node_modules/cheerio/",\ - "packageDependencies": [\ - ["cheerio", "npm:1.0.0-rc.12"],\ - ["cheerio-select", "npm:2.1.0"],\ - ["dom-serializer", "npm:2.0.0"],\ - ["domhandler", "npm:5.0.3"],\ - ["domutils", "npm:3.1.0"],\ - ["htmlparser2", "npm:8.0.2"],\ - ["parse5", "npm:7.1.2"],\ - ["parse5-htmlparser2-tree-adapter", "npm:7.0.0"]\ - ],\ - "linkType": "HARD"\ - }]\ - ]],\ - ["cheerio-select", [\ - ["npm:2.1.0", {\ - "packageLocation": "./.yarn/cache/cheerio-select-npm-2.1.0-e92bc2f296-843d6d4799.zip/node_modules/cheerio-select/",\ - "packageDependencies": [\ - ["cheerio-select", "npm:2.1.0"],\ - ["boolbase", "npm:1.0.0"],\ - ["css-select", "npm:5.1.0"],\ - ["css-what", "npm:6.1.0"],\ - ["domelementtype", "npm:2.3.0"],\ - ["domhandler", "npm:5.0.3"],\ - ["domutils", "npm:3.1.0"]\ - ],\ - "linkType": "HARD"\ - }]\ - ]],\ ["chownr", [\ ["npm:2.0.0", {\ "packageLocation": "./.yarn/cache/chownr-npm-2.0.0-638f1c9c61-c57cf9dd07.zip/node_modules/chownr/",\ @@ -1045,7 +1014,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "packageLocation": "./.yarn/__virtual__/discord-player-virtual-5d4dbc4bbe/0/cache/discord-player-npm-6.6.3-4a62083a91-c98d853980.zip/node_modules/discord-player/",\ "packageDependencies": [\ ["discord-player", "virtual:20c353e2d6536e37339997f03975c6a660f4d296e664d291bd43620c6162cca8eb5ef90b0998dc9db75ff6862e5da587d0530bae26805f5fadc8f17aaa4ff794#npm:6.6.3"],\ - ["@discord-player/equalizer", "npm:0.2.2"],\ + ["@discord-player/equalizer", "npm:0.2.3"],\ ["@discord-player/extractor", "npm:4.4.3"],\ ["@discord-player/ffmpeg", "npm:0.1.0"],\ ["@discord-player/utils", "npm:0.2.2"],\ @@ -1336,12 +1305,12 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { }]\ ]],\ ["genius-lyrics", [\ - ["npm:4.4.3", {\ - "packageLocation": "./.yarn/cache/genius-lyrics-npm-4.4.3-caaf25a1b9-5612f4b975.zip/node_modules/genius-lyrics/",\ + ["npm:4.4.6", {\ + "packageLocation": "./.yarn/cache/genius-lyrics-npm-4.4.6-beff04ebb7-05a7420c75.zip/node_modules/genius-lyrics/",\ "packageDependencies": [\ - ["genius-lyrics", "npm:4.4.3"],\ - ["cheerio", "npm:1.0.0-rc.12"],\ - ["undici", "npm:5.23.0"]\ + ["genius-lyrics", "npm:4.4.6"],\ + ["node-html-parser", "npm:6.1.10"],\ + ["undici", "npm:5.24.0"]\ ],\ "linkType": "HARD"\ }]\ @@ -1409,19 +1378,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "linkType": "HARD"\ }]\ ]],\ - ["htmlparser2", [\ - ["npm:8.0.2", {\ - "packageLocation": "./.yarn/cache/htmlparser2-npm-8.0.2-5d9f901bb6-29167a0f92.zip/node_modules/htmlparser2/",\ - "packageDependencies": [\ - ["htmlparser2", "npm:8.0.2"],\ - ["domelementtype", "npm:2.3.0"],\ - ["domhandler", "npm:5.0.3"],\ - ["domutils", "npm:3.1.0"],\ - ["entities", "npm:4.5.0"]\ - ],\ - "linkType": "HARD"\ - }]\ - ]],\ ["http-cache-semantics", [\ ["npm:4.1.1", {\ "packageLocation": "./.yarn/cache/http-cache-semantics-npm-4.1.1-1120131375-83ac0bc60b.zip/node_modules/http-cache-semantics/",\ @@ -2066,6 +2022,15 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { }]\ ]],\ ["node-html-parser", [\ + ["npm:6.1.10", {\ + "packageLocation": "./.yarn/cache/node-html-parser-npm-6.1.10-155152e1bc-927f6a38b3.zip/node_modules/node-html-parser/",\ + "packageDependencies": [\ + ["node-html-parser", "npm:6.1.10"],\ + ["css-select", "npm:5.1.0"],\ + ["he", "npm:1.2.0"]\ + ],\ + "linkType": "HARD"\ + }],\ ["npm:6.1.5", {\ "packageLocation": "./.yarn/cache/node-html-parser-npm-6.1.5-b4e338af4b-b54257b319.zip/node_modules/node-html-parser/",\ "packageDependencies": [\ @@ -2157,27 +2122,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "linkType": "HARD"\ }]\ ]],\ - ["parse5", [\ - ["npm:7.1.2", {\ - "packageLocation": "./.yarn/cache/parse5-npm-7.1.2-aa9a92c270-59465dd05e.zip/node_modules/parse5/",\ - "packageDependencies": [\ - ["parse5", "npm:7.1.2"],\ - ["entities", "npm:4.5.0"]\ - ],\ - "linkType": "HARD"\ - }]\ - ]],\ - ["parse5-htmlparser2-tree-adapter", [\ - ["npm:7.0.0", {\ - "packageLocation": "./.yarn/cache/parse5-htmlparser2-tree-adapter-npm-7.0.0-38e1b3a974-fc5d01e077.zip/node_modules/parse5-htmlparser2-tree-adapter/",\ - "packageDependencies": [\ - ["parse5-htmlparser2-tree-adapter", "npm:7.0.0"],\ - ["domhandler", "npm:5.0.3"],\ - ["parse5", "npm:7.1.2"]\ - ],\ - "linkType": "HARD"\ - }]\ - ]],\ ["path-is-absolute", [\ ["npm:1.0.1", {\ "packageLocation": "./.yarn/cache/path-is-absolute-npm-1.0.1-31bc695ffd-060840f92c.zip/node_modules/path-is-absolute/",\ @@ -2711,6 +2655,14 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["busboy", "npm:1.6.0"]\ ],\ "linkType": "HARD"\ + }],\ + ["npm:5.24.0", {\ + "packageLocation": "./.yarn/cache/undici-npm-5.24.0-df83ac4dd5-0795b69e0f.zip/node_modules/undici/",\ + "packageDependencies": [\ + ["undici", "npm:5.24.0"],\ + ["busboy", "npm:1.6.0"]\ + ],\ + "linkType": "HARD"\ }]\ ]],\ ["unfetch", [\ diff --git a/src/commands/calculator.ts b/src/commands/calculator.ts new file mode 100644 index 0000000..c04a961 --- /dev/null +++ b/src/commands/calculator.ts @@ -0,0 +1,50 @@ +import Discord from 'discord.js'; +import TClient from '../client.js'; +import MessageTool from '../helpers/MessageTool.js'; +import FormatTime from '../helpers/FormatTime.js'; + +export default { + run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){ + const now = Date.now(); + const exp = interaction.options.getString('expression', true).replace(/[^-()\d/*+.]/g, ''); + try { + const result = eval(exp); + switch (exp) { + case '1+1': + return interaction.reply('2, quick maths.'); + case '2+2': + return interaction.reply('2+2 is 4, minus 1 that\'s 3, quick maths.'); + 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' + )); + }; + 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: ${FormatTime(Date.now() - now, 3)}`})]}) + } catch { + interaction.reply('The given expression is invalid.'); + } + }, + data: new Discord.SlashCommandBuilder() + .setName('calculator') + .setDescription('Calculate a math expression or simple 2+2') + .addStringOption(x=>x + .setName('expression') + .setDescription('The expression to be calculated') + .setRequired(true)) +} + +// Copilot conversation: + +//Q: Why do we need to replace the string? +//A: Because eval() is dangerous and can run any code +// So we need to make sure that the string is a math expression +// and nothing else + +//Q: If we receive a string like "1+1; console.log('Hello World')" +// will it run the console.log()? +//A: No, because we are replacing all characters that are not +// numbers, operators, parenthesis, etc. So it will only run +// the math expression \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 9cf893a..d95a558 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6,9 +6,9 @@ __metadata: cacheKey: 8 "@discord-player/equalizer@npm:^0.2.2": - version: 0.2.2 - resolution: "@discord-player/equalizer@npm:0.2.2" - checksum: 33b9991a840d6f87cea7e5f5d9c8e4e3cb73e90152c3797f44545916899794cf4e0efa0c6c595cdfdddf0ac7619cf23a3f51b508f2439870a2fbd7d4dfe59c05 + version: 0.2.3 + resolution: "@discord-player/equalizer@npm:0.2.3" + checksum: 6f60c41b483253c647d79d18a4b94ed413281aa38af190b62fc8cd4c11feb1ea84a6662dbb08922cdb9000e32dee78ac5cb13c4f9a47711890e5a9ebb24e9f08 languageName: node linkType: hard @@ -618,35 +618,6 @@ __metadata: languageName: node linkType: hard -"cheerio-select@npm:^2.1.0": - version: 2.1.0 - resolution: "cheerio-select@npm:2.1.0" - dependencies: - boolbase: ^1.0.0 - css-select: ^5.1.0 - css-what: ^6.1.0 - domelementtype: ^2.3.0 - domhandler: ^5.0.3 - domutils: ^3.0.1 - checksum: 843d6d479922f28a6c5342c935aff1347491156814de63c585a6eb73baf7bb4185c1b4383a1195dca0f12e3946d737c7763bcef0b9544c515d905c5c44c5308b - languageName: node - linkType: hard - -"cheerio@npm:^1.0.0-rc.9": - version: 1.0.0-rc.12 - resolution: "cheerio@npm:1.0.0-rc.12" - dependencies: - cheerio-select: ^2.1.0 - dom-serializer: ^2.0.0 - domhandler: ^5.0.3 - domutils: ^3.0.1 - htmlparser2: ^8.0.1 - parse5: ^7.0.0 - parse5-htmlparser2-tree-adapter: ^7.0.0 - checksum: 5d4c1b7a53cf22d3a2eddc0aff70cf23cbb30d01a4c79013e703a012475c02461aa1fcd99127e8d83a02216386ed6942b2c8103845fd0812300dd199e6e7e054 - languageName: node - linkType: hard - "chownr@npm:^2.0.0": version: 2.0.0 resolution: "chownr@npm:2.0.0" @@ -950,7 +921,7 @@ __metadata: languageName: node linkType: hard -"entities@npm:^4.2.0, entities@npm:^4.4.0": +"entities@npm:^4.2.0": version: 4.5.0 resolution: "entities@npm:4.5.0" checksum: 853f8ebd5b425d350bffa97dd6958143179a5938352ccae092c62d1267c4e392a039be1bae7d51b6e4ffad25f51f9617531fedf5237f15df302ccfb452cbf2d7 @@ -1084,12 +1055,12 @@ __metadata: linkType: hard "genius-lyrics@npm:^4.4.3": - version: 4.4.3 - resolution: "genius-lyrics@npm:4.4.3" + version: 4.4.6 + resolution: "genius-lyrics@npm:4.4.6" dependencies: - cheerio: ^1.0.0-rc.9 - undici: ^5.8.2 - checksum: 5612f4b97503a5e1fab912ccd46c3dfe95495e2f0b90179a718a4454422fbd0370690734dad5d79e0c043e533ee4e3bd90540de0e6ab6668875b3cb0947a0391 + node-html-parser: ^6.1.9 + undici: ^5.24.0 + checksum: 05a7420c7548badce414cd3cb45f4ba9822d951f8b7ed0d9b1b546cf5117847f9e17d8453db883d56e6cba745401da3d7de81cc6a625d54b313cef95e5f0c259 languageName: node linkType: hard @@ -1152,18 +1123,6 @@ __metadata: languageName: node linkType: hard -"htmlparser2@npm:^8.0.1": - version: 8.0.2 - resolution: "htmlparser2@npm:8.0.2" - dependencies: - domelementtype: ^2.3.0 - domhandler: ^5.0.3 - domutils: ^3.0.1 - entities: ^4.4.0 - checksum: 29167a0f9282f181da8a6d0311b76820c8a59bc9e3c87009e21968264c2987d2723d6fde5a964d4b7b6cba663fca96ffb373c06d8223a85f52a6089ced942700 - languageName: node - linkType: hard - "http-cache-semantics@npm:^4.1.1": version: 4.1.1 resolution: "http-cache-semantics@npm:4.1.1" @@ -1736,6 +1695,16 @@ __metadata: languageName: node linkType: hard +"node-html-parser@npm:^6.1.9": + version: 6.1.10 + resolution: "node-html-parser@npm:6.1.10" + dependencies: + css-select: ^5.1.0 + he: 1.2.0 + checksum: 927f6a38b3b1cbc042bce609e24fb594d3b1e0f1067ffb416a925fa5a699e907be31980f349e094d55bab706dc16a71958b08f8dcdab62faf7b12013f29442bc + languageName: node + linkType: hard + "nopt@npm:^5.0.0": version: 5.0.0 resolution: "nopt@npm:5.0.0" @@ -1816,25 +1785,6 @@ __metadata: languageName: node linkType: hard -"parse5-htmlparser2-tree-adapter@npm:^7.0.0": - version: 7.0.0 - resolution: "parse5-htmlparser2-tree-adapter@npm:7.0.0" - dependencies: - domhandler: ^5.0.2 - parse5: ^7.0.0 - checksum: fc5d01e07733142a1baf81de5c2a9c41426c04b7ab29dd218acb80cd34a63177c90aff4a4aee66cf9f1d0aeecff1389adb7452ad6f8af0a5888e3e9ad6ef733d - languageName: node - linkType: hard - -"parse5@npm:^7.0.0": - version: 7.1.2 - resolution: "parse5@npm:7.1.2" - dependencies: - entities: ^4.4.0 - checksum: 59465dd05eb4c5ec87b76173d1c596e152a10e290b7abcda1aecf0f33be49646ea74840c69af975d7887543ea45564801736356c568d6b5e71792fd0f4055713 - languageName: node - linkType: hard - "path-is-absolute@npm:^1.0.0": version: 1.0.1 resolution: "path-is-absolute@npm:1.0.1" @@ -2285,7 +2235,7 @@ __metadata: languageName: node linkType: hard -"undici@npm:^5.22.1, undici@npm:^5.8.2": +"undici@npm:^5.22.1": version: 5.23.0 resolution: "undici@npm:5.23.0" dependencies: @@ -2294,6 +2244,15 @@ __metadata: languageName: node linkType: hard +"undici@npm:^5.24.0": + version: 5.24.0 + resolution: "undici@npm:5.24.0" + dependencies: + busboy: ^1.6.0 + checksum: 0795b69e0f7e1b2b162bce0d1670e6b44c968960e519f5b450df5196fd9c5102e0838ed854e68e61588f3c2436a3dc3d4390f9bf4a24b04eeb03926fe0eaa599 + languageName: node + linkType: hard + "unfetch@npm:^5.0.0": version: 5.0.0 resolution: "unfetch@npm:5.0.0"