评估命令 Discord.JS

时间:2021-03-09 23:32:12

标签: javascript node.js discord discord.js

我正在尝试在 Discord.JS v12 中创建一个 eval 命令。除了当我将命令与嵌入一起使用时输出返回 undefined 的事实之外,该代码完美无缺。这是我的代码:

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)
  }

这是当前的结果: enter image description here

1 个答案:

答案 0 :(得分:3)

当您运行 console.log('e') 时,eval 函数没有任何返回值,因此它返回 undefined。如果您尝试评估 2+2,它应该返回 4

在此处提供更多信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval