【发布时间】:2021-10-07 08:01:48
【问题描述】:
在过去的一段时间里,我一直在尝试使用 Slash Commands 创建一个“Eval”命令。
我有一个与我的命令处理程序一起使用的系统,但是,它特别是导致问题的“Eval”命令。
我目前正在使用@discordjs/builders,但是,它对此没有帮助。
这就是斜杠命令的构建方式:
slash: new SlashCommandBuilder()
.setName("eval")
.setDescription("Evaluates JavaScript code.")
.addStringOption(option => option.setName("code").setDescription("Your JavaScript code.").setRequired(true)),
在 Discord 中,“代码”选项甚至不会出现。当我尝试通过运行来运行命令时:
/eval ```js
console.log("Hello World");
我收到一条错误消息,提示 Cannot read property "replace" of null
这是斜线命令的脚本:
runSlash: async (interaction, bot) => {
if (interaction.user.id !== interaction.guild.ownerId) {
interaction.reply("You are not allowed to do this.");
return bot.commandLog(`${msg.author} attempted to use the EVAL command, but was not Atelo.`);
}
const toEval = interaction.options.getString("code");
console.log(interaction.options);
/*
this is what ^^^ returns:
CommandInteractionOptionResolver {
_group: null,
_subcommand: null,
_hoistedOptions: []
}
*/
console.log(interaction.options.getString("code")); // Also returns null
const toRemove = /^(```js)|(```)$/gi;
const evalEmbed = new MessageEmbed();
try {
const evalled = eval(toEval.replace(toRemove, ''));
evalEmbed.setTitle("Evaluation Successful");
evalEmbed.addFields({ name: "Output", value: "```js\n" + evalled + "\n```" });
evalEmbed.setFooter(interaction.guild.name, interaction.guild.iconURL());
evalEmbed.setTimestamp();
} catch (err) {
evalEmbed.setTitle("Evaluation Failed");
evalEmbed.addFields({ name: "Output", value: "```js\n" + err + "\n```" });
evalEmbed.setFooter(interaction.guild.name, interaction.guild.iconURL());
evalEmbed.setTimestamp();
}
interaction.reply({ embeds: [evalEmbed] });
return bot.commandLog(`${msg.author} successfully used the EVAL command.`);
}
}
我已经尝试调试了一段时间,我非常困惑。 感谢您提供的任何帮助!
【问题讨论】:
标签: javascript discord discord.js