Skip to content

Commit

Permalink
Add C++ code runner
Browse files Browse the repository at this point in the history
Signed-off-by: Prince Mendiratta <prince.mendi@gmail.com>
  • Loading branch information
Prince-Mendiratta committed Dec 6, 2021
1 parent 6090e9c commit 2f256c7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ const data = {
TAG_PERSON: "```Reply to the person that should be included in group```",
GROUP_CREATED: "```Group has been created successfully.```"
},
cpp: {
DESCRIPTION: "Execute C++ code and directly get output to WhatsApp.",
EXTENDED_DESCRIPTION: "```Use this module to execute C++ code and get the output directly on WhatsApp.```",
NO_INPUT: "```Give some C++ code to execute. Use the``` *.help cpp* ```command to get more info on this module.```",
BOILERPLATE: "#include <iostream>\nusing namespace std;\n\nint main(){\n {code}\n}",
OUTPUT_TEMPLATE: "⭐ *Output:*```\n{stdout}```\n\n⚠️ *Error:*```\n{stderr}```\n\n👨🏻‍💻 *Command:*```\n{code}```",
PROCESSING: "```Executing, please wait...```"
},
demote: {
DESCRIPTION: "Demote a person from admin",
EXTENDED_DESCRIPTION:
Expand Down
74 changes: 74 additions & 0 deletions modules/cpp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { MessageType, Mimetype } = require("@adiwajshing/baileys");
const Strings = require("../lib/db");
const format = require("python-format-js");
const inputSanitization = require("../sidekick/input-sanitization");
const CPP = Strings.cpp;
const fs = require("fs");
const { promisify } = require('util');
const exec = promisify(require("child_process").exec);
const config = require('../config')

module.exports = {
name: "cpp",
description: CPP.DESCRIPTION,
extendedDescription: CPP.EXTENDED_DESCRIPTION,
demo: { isEnabled: true, text: ['.cpp printf("namaste");', '.cpp #include <bits/stdc++.h>\n#include <bits/stdc++.h>\n#include <queue>\nusing namespace std;\n\nint main() {\n cout << "Gucci gang bole to best!" << endl;\n}'] },
async handle(client, chat, BotsApp, args) {
try {
if(args[0] == null){
await client.sendMessage(
BotsApp.chatId,
CPP.NO_INPUT,
MessageType.text
).catch(err => inputSanitization.handleError(err, client, BotsApp));
return;
}
var processing = await client.sendMessage(
BotsApp.chatId,
CPP.PROCESSING,
MessageType.text
)
var input = BotsApp.body.replace(
BotsApp.body[0] + BotsApp.commandName + " ",
""
);
var code = ""
if(!(/main\(/g.test(input))){
code = CPP.BOILERPLATE.replace("{code}", input);
}else{
code = input;
}
fs.writeFileSync('./tmp/cpp-botsapp.cpp', code);
var out = {}
var compile = "g++ ./tmp/cpp-botsapp.cpp -o ./tmp/cppBotsApp.out"
var execute = "env -i ./tmp/cppBotsApp.out"
try{
await exec(compile)
var eval = await exec(execute)
out.stderr = "N/A"
out.stdout = eval.stdout
}catch(err){
out.stderr = err.stderr
out.stdout = "N/A"
}
out.code = code
await client.sendMessage(
BotsApp.chatId,
CPP.OUTPUT_TEMPLATE.format(out),
MessageType.text
)
await client.deleteMessage(BotsApp.chatId, {
id: processing.key.id,
remoteJid: BotsApp.chatId,
fromMe: true,
})
inputSanitization.deleteFiles(
"./tmp/cppBotsApp.out",
"./tmp/cpp-botsapp.cpp"
);
} catch (err) {
console.log(err);
await inputSanitization.handleError(err, client, BotsApp);
}
}
};

0 comments on commit 2f256c7

Please sign in to comment.