Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const data = {
MESSAGE_NOT_TAGGED: "```Tag a message or enter a number to proceed.```",
NOT_BLOCK_BOT: "```Bot can not block itself```"
},
calc: {
DESCRIPTION: "Calculate the given expression.",
EXTENDED_DESCRIPTION:
"```This module can be used to calculate the expression presented.\n\nExample usage,```\n*.calc 2+2*",
},
carbon: {
DESCRIPTION: "Convert text/code to a carbon image.",
EXTENDED_DESCRIPTION: "```This module can be used to convert text/code into carbon images.\n\nExample Usage,```\n *.carbon <text>* \n *.carbon* ```and reply to a text message.\n\nUse the -t flag after``` *.carbon* ```to get the list of themes availble.\nIn order to specify the theme, use``` *.carbon <text> -t <theme>* .",
Expand Down
32 changes: 32 additions & 0 deletions modules/calc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { MessageType } = require("@adiwajshing/baileys");
const Strings = require("../lib/db");
const format = require("python-format-js");
const inputSanitization = require("../sidekick/input-sanitization");
const calc = Strings.calc;
const math = require("mathjs");

module.exports = {
name: "calc",
description: calc.DESCRIPTION,
extendedDescription: calc.EXTENDED_DESCRIPTION,
demo: { isEnabled: true, text: ".calc" },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a sample expression for demo

async handle(client, chat, BotsApp, args) {
let output;
try{
output = math.evaluate(args[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Handle the case where the user sends multiple expressions in a single command.
  2. Provide a list of operations allowed.

}
catch(err){
await inputSanitization.invalidFormat(err, client, BotsApp);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle invalid format error using db.js. No need of separate function.

return;
}
try {
client.sendMessage(
BotsApp.chatId,
`output: ${output}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the argument itself instead of "output". e.g. 5+2 = 7

MessageType.text
).catch(err => inputSanitization.handleError(err, client, BotsApp));
} catch (err) {
await inputSanitization.handleError(err, client, BotsApp);
}
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"google-tts-api": "^2.0.2",
"got": "^11.8.2",
"jsdom": "^17.0.0",
"mathjs": "^10.1.0",
"ocr-space-api-wrapper": "^1.0.6",
"pg": "^8.7.1",
"python-format-js": "^1.3.9",
Expand Down
8 changes: 8 additions & 0 deletions sidekick/input-sanitization.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ exports.isMember = async (chatId, groupMembers) => {
}
}

exports.invalidFormat = async(err, client, BotsApp) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required.

client.sendMessage(
BotsApp.chatId,
"*Invalid format.*\nApproved Syntax:\n```1. 1 + 2``` \n```2. (1*2) + 2``` \n```3. 4! + 2^2```",
MessageType.text
);
}

exports.handleError = async(err, client, BotsApp, customMessage = "```Something went wrong. The error has been logged in log chats```") => {
console.log(chalk.redBright.bold("[ERROR] " + err));
data = {
Expand Down