【问题标题】:I'm receiving the error TypeError: (intermediate value).setName(...).setDescription(...).addUserOption(...).build is not a function我收到错误 TypeError: (intermediate value).setName(...).setDescription(...).addUserOption(...).build is not a function
【发布时间】:2023-01-16 20:53:22
【问题描述】:

当我尝试启动我的机器人时,下面的代码抛出错误“TypeError: (intermediate value).setName(...).setDescription(...).addUserOption(...).build is not a function”。

代码:

const { SlashCommandBuilder, SlashCommandUserOption } = require('discord.js');

const command = new SlashCommandBuilder()
    .setName('emoji')
    .setDescription('Retrieves the image of a specified emoji.')
    .addUserOption(new SlashCommandUserOption("emoji", "Input the emoji you wish to retrieve.", true), "What emoji would you like to retrieve?")
    .build();

command.execute = async (interaction) => {
    let emoji = interaction.options.emoji;
    if(!emoji || emoji.length !== 2 || !/^[\u{1f300}-\u{1f5ff}\u{1f900}-\u{1f9ff}\u{1f600}-\u{1f64f}\u{1f680}-\u{1f6ff}\u{2600}-\u{26ff}\u{2700}-\u{27bf}\u{1f1e6}-\u{1f1ff}\u{1f191}-\u{1f251}\u{1f004}\u{1f0cf}\u{1f170}-\u{1f171}\u{1f17e}-\u{1f17f}\u{1f18e}\u{3030}\u{2b50}\u{2b55}\u{2934}-\u{2935}\u{2b05}-\u{2b07}\u{2b1b}-\u{2b1c}\u{3297}\u{3299}\u{303d}\u{00a9}\u{00ae}\u{2122}\u{23f3}\u{24c2}\u{23e9}-\u{23ef}\u{25b6}\u{23f8}-\u{23fa}]$/u.test(emoji)) {
        interaction.reply("Invalid emoji.");
        return;
    }

    let image = `https://raw.githubusercontent.com/twitter/twemoji/main/assets/72x72/${emoji.codePointAt(0).toString(16)}.png`;
    interaction.reply({
        files: [{
            attachment: image,
            name: `${emoji}.png`
        }]
    });
};

module.exports = command;

提前感谢您的帮助!

【问题讨论】:

  • 我找不到同时包含 SlashCommandBuilder.build() 的示例。你确定这种方法应该存在吗?你不是说.toJSON()吗?
  • 我用“.toJSON()”替换了“.build()”,它说“ValidationError: Expected a string primitive”。

标签: javascript node.js discord discord.js


【解决方案1】:

没有名为.build()的方法函数SlashCommandBuilder.我假设您需要 .toJSON() 函数,它将构建器转换为 JSON 对象(可以发送到 Discord)。

输入错误

在您的代码中,您要求 userOption 允许用户提及服务器中的其他人 - 看起来您想要一个字符串选项。我不熟悉您如何选择添加选项,这可能是您遇到 ValidationError: Expected a string primitive 错误的原因。

DiscordJS Guide引导您完成这种添加命令选项的方法。

const command = new SlashCommandBuilder()
   .setName('emoji')
   .setDescription('Retrieves the image of a specified emoji.')

   .addStringOption(option => option.setName('emoji').setDescription('Input the emoji you wish to retrieve.').setRequired(true))

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 2021-08-13
    • 2015-10-11
    相关资源
    最近更新 更多