Discord.js报错:this.options.components?.map is not a function 求助
TypeError: this.options.components?.map is not a function 问题解决
this.options.components?.map is not a function
我已经用了最新版Node.js(v19.6.1),网上搜的方案基本都是让更新Node.js,但没用,还是报这个错。
我的代码如下:
const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, StringSelectMenuBuilder, Component } = require('discord.js'); function makeid(length) { var result = ''; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var charactersLength = characters.length; for (var i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } module.exports = { data: new SlashCommandBuilder() .setName('script') .setDescription('รับสคริป'), execute: async (interaction, con, log, client, config) => { log(`[LOG Command] ${interaction.user.username}, use command getscript`, "green") const user = interaction.options.getUser('user'); const menu = new ActionRowBuilder().addComponents( new StringSelectMenuBuilder() .setCustomId("SELRCTION") .addOptions( { label: "BloxFruit", description: "script BloxFruit ", value: "BF", emoji: "🍎"}, ), ); /*interaction.relpy({ content: "You Select ", ephemeral: true, components: {menu}});*/ await con.query(`SELECT userkey, discord_id,Blacklisted FROM bloxfruit Where discord_id = '${interaction.user.id}'`, async function(err, results, fields) { if(!results[0]) { await interaction.reply({ embeds: [ new EmbedBuilder() .setColor("#FF0000") .setTitle('**"WET HUB" | ᴡʜɪᴛᴇʟɪsᴛ ᴍᴀɴᴀɢᴇʀ**') .setDescription("คุณไม่มีไวริสนะคะ กรุณาก่อนใช้ด้วยค่ะ") .setTimestamp() ], ephemeral: true }) } else { if(results[0].Blacklisted == "True") { await interaction.reply({ embeds: [ new EmbedBuilder() .setColor("#FF0000") .setTitle('**WET HUB | ᴡʜɪᴛᴇʟɪsᴛ ᴍᴀɴᴀɢᴇʀ**') .setDescription("คุณอยู่ในบัญชีดำนะคะ") .setTimestamp() ], ephemeral: true }) } else { let key = results[0].userkey let discord_id = results[0].discord_id await interaction.reply({ content: "You Select ", ephemeral: true, components: {menu}}) if (value == "BF") { await interaction.reply({ embeds: [ new EmbedBuilder() .setColor("00FF46") .setTitle('**WET HUB | ᴡʜɪᴛᴇʟɪsᴛ ᴍᴀɴᴀɢᴇʀ**') .setDescription('```lua\ngetgenv().Key = "'+ key + '"\ngetgenv().DiscordId = "'+ discord_id + '"\nloadstring(game:HttpGet("https://raw.githubusercontent.com/Galiverking/CheckPointWT/main/check-lua.lua"))()```') .setTimestamp() ], ephemeral: true }) } }; } }) }
运行时的完整报错信息:
C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\node_modules\discord.js\src\structures\MessagePayload.js:137 const components = this.options.components?.map(c => (isJSONEncodable(c) ? c : new ActionRowBuilder(c)).toJSON()); ^ TypeError: this.options.components?.map is not a function at MessagePayload.resolveBody (C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\node_modules\discord.js\src\structures\MessagePayload.js:137:49) at ChatInputCommandInteraction.reply (C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:109:56) at Query.<anonymous> (C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\commands\getscript.js:57:31) at Query.<anonymous> (C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\node_modules\mysql\lib\Connection.js:526:10) at Query._callback (C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\node_modules\mysql\lib\Connection.js:488:16) at Sequence.end (C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24) at Query._handleFinalResultPacket (C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\node_modules\mysql\lib\protocol\sequences\Query.js:149:8) at Query.EofPacket (C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\node_modules\mysql\lib\protocol\sequences\Query.js:133:8) at Protocol._parsePacket (C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\node_modules\mysql\lib\protocol\Protocol.js:291:23) at Parser._parsePacket (C:\Users\user\Documents\BOT WHITELIST PACK\main\slash_BOT\node_modules\mysql\lib\protocol\Parser.js:433:10) Node.js v19.6.1
问题根源
报错是因为discord.js内部在调用map方法处理组件,但你传入的components是对象{menu},而discord.js要求这个参数必须是数组类型。对象没有map方法,所以直接抛出类型错误。
修复方案
修正组件参数格式
把调用interaction.reply时的components: {menu}改成components: [menu],因为discord.js需要接收组件数组:// 错误写法 // await interaction.reply({ content: "You Select ", ephemeral: true, components: {menu}}) // 正确写法 await interaction.reply({ content: "You Select ", ephemeral: true, components: [menu] })处理其他潜在问题
- 你定义了
const user = interaction.options.getUser('user');但没有使用,可以考虑删除或者根据需求使用 if (value == "BF")中的value未定义,当前代码逻辑无法获取用户选择的菜单值,需要在主文件中监听interactionCreate事件来处理选择菜单的交互,示例逻辑如下:// 在你的主bot文件中添加 client.on('interactionCreate', async interaction => { if (!interaction.isStringSelectMenu()) return; if (interaction.customId === 'SELRCTION') { const selectedValue = interaction.values[0]; // 这里可以获取用户选择的value,比如"BF",然后执行对应的逻辑 // 记得要回复交互,比如: await interaction.reply({ content: `你选择了${selectedValue}`, ephemeral: true }); } });
- 你定义了
内容的提问来源于stack exchange,提问作者GaliverKing
相关产品推荐
相关产品推荐

