Discord.js机器人发送Embed报错:yourid等变量未定义求助
问题原因
yourid、productownerid、productname、evalmsg这些变量是在customId == 'evv'的代码块内部用let声明的,变量作用域仅限于该代码块。当用户点击customId为'1'的按钮时,触发的是完全独立的代码逻辑,根本无法访问之前块里的变量,所以会抛出“未定义”错误。
解决方案
方法1:将数据嵌入按钮的customId(轻量场景推荐)
在创建按钮时,把需要的变量拼接进customId,点击按钮时再拆分提取数据:
if(interaction.customId == 'evv'){ let yourid = interaction.fields.getTextInputValue('ask_1') let productownerid = interaction.fields.getTextInputValue('ask_2') let productname = interaction.fields.getTextInputValue('ask_3') let evalmsg = interaction.fields.getTextInputValue('ask_4') const exampleEmbed = new MessageEmbed() .setColor("RANDOM") .setFields( { name: '**Buyer-Name**', value: `<@${yourid}>`}, { name: '**Owner Of The Product**', value:`**<@${productownerid}>**`}, { name: '**Product**', value:`**${productname}**`}, { name: '**Message**', value: `**${evalmsg}**`} ) .setFooter(`Requested By ${interaction.user.tag} , ${new Date()}`) // 把变量拼到按钮customId里,用下划线分隔(如果变量含下划线可换其他分隔符) let button25 = new MessageActionRow().addComponents( new MessageButton() .setCustomId(`1_${yourid}_${productownerid}_${productname}_${evalmsg}`) .setLabel("تقييم") .setStyle('PRIMARY') .setEmoji("💖") ) await interaction.guild.channels.cache.get("1048414164932108289").send({embeds: [exampleEmbed],components:[button25]}) } // 处理带数据的按钮交互 if (interaction.customId.startsWith('1')){ // 拆分customId获取变量 const [_, yourid, productownerid, productname, evalmsg] = interaction.customId.split('_'); const exampleEmbed2 = new MessageEmbed() .setColor("RANDOM") .setFields( { name: '**Buyer-Name**', value: `<@${yourid}>`}, { name: '**Owner Of The Product**', value:`**<@${productownerid}>**`}, { name: '**Product**', value:`**${productname}**`}, { name: '**Message**', value: `**${evalmsg}**`}, { name: '**Evaluation**', value: `**💖**`} ) .setFooter(`Requested By ${interaction.user.tag} , ${new Date()}`) await interaction.guild.channels.cache.get("1048414164932108289").send({embeds: [exampleEmbed2],components:[]}); // 必须回复交互,否则Discord会判定超时 await interaction.deferUpdate(); }
方法2:用Map临时存储消息关联数据(适合复杂数据)
通过消息ID关联变量,避免customId过长的问题:
// 在代码顶部定义全局Map存储数据 const messageData = new Map(); if(interaction.customId == 'evv'){ let yourid = interaction.fields.getTextInputValue('ask_1') let productownerid = interaction.fields.getTextInputValue('ask_2') let productname = interaction.fields.getTextInputValue('ask_3') let evalmsg = interaction.fields.getTextInputValue('ask_4') const exampleEmbed = new MessageEmbed() .setColor("RANDOM") .setFields( { name: '**Buyer-Name**', value: `<@${yourid}>`}, { name: '**Owner Of The Product**', value:`**<@${productownerid}>**`}, { name: '**Product**', value:`**${productname}**`}, { name: '**Message**', value: `**${evalmsg}**`} ) .setFooter(`Requested By ${interaction.user.tag} , ${new Date()}`) let button25 = new MessageActionRow().addComponents( new MessageButton() .setCustomId('1') .setLabel("تقييم") .setStyle('PRIMARY') .setEmoji("💖") ) // 发送消息并保存消息对象 const sentMessage = await interaction.guild.channels.cache.get("1048414164932108289").send({embeds: [exampleEmbed],components:[button25]}); // 用消息ID作为key存储变量 messageData.set(sentMessage.id, { yourid, productownerid, productname, evalmsg }); } if (interaction.customId == '1'){ // 通过消息ID获取存储的数据 const data = messageData.get(interaction.message.id); if (!data) { return await interaction.reply({ content: "数据已过期或不存在", ephemeral: true }); } const exampleEmbed2 = new MessageEmbed() .setColor("RANDOM") .setFields( { name: '**Buyer-Name**', value: `<@${data.yourid}>`}, { name: '**Owner Of The Product**', value:`**<@${data.productownerid}>**`}, { name: '**Product**', value:`**${data.productname}**`}, { name: '**Message**', value: `**${data.evalmsg}**`}, { name: '**Evaluation**', value: `**💖**`} ) .setFooter(`Requested By ${interaction.user.tag} , ${new Date()}`) await interaction.guild.channels.cache.get("1048414164932108289").send({embeds: [exampleEmbed2],components:[]}); await interaction.deferUpdate(); // 可选:删除已使用的数据,释放内存 messageData.delete(interaction.message.id); }
内容的提问来源于stack exchange,提问作者tompano
相关产品推荐
相关产品推荐

