-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added calc module with regards to #88 #92
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,30 @@ | ||||||
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 2^4+5!" }, | ||||||
async handle(client, chat, BotsApp, args) { | ||||||
let output = math.evaluate(args[0]); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of restricting the user to add their expressions without any spaces (more values in the args array), it's better if we concatenate the full array in a single string before using the evaluate call. |
||||||
if(!isNaN(output)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having
Suggested change
|
||||||
await client | ||||||
.sendMessage(BotsApp.chatId, calc.INVALID_FORMAT, MessageType.text); | ||||||
return; | ||||||
} | ||||||
try { | ||||||
client.sendMessage( | ||||||
BotsApp.chatId, | ||||||
`${args[0]} = ${output}`, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can bold out the output using *, looks much better |
||||||
MessageType.text | ||||||
).catch(err => inputSanitization.handleError(err, client, BotsApp)); | ||||||
} catch (err) { | ||||||
await inputSanitization.handleError(err, client, BotsApp); | ||||||
} | ||||||
}, | ||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can wrap a try catch block around this function call. Thus, whenever any error happens, the error is handled there and the value of output is set to undefined. You can then handle the error in the catch block, sending a message to WA with the Error Message. Have a look -