【问题标题】:discord.js v13 - Cannot use Interaction Optionsdiscord.js v13 - 无法使用交互选项
【发布时间】: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


    【解决方案1】:

    所以,我意识到我在命令处理程序中使用了错误的变量,这破坏了斜杠命令。

    哎呀!

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2021-10-17
    相关资源
    最近更新 更多