我的嵌入消息正在编辑,但仍然出现错误“交互失败”

问题描述 投票:0回答:1

我的嵌入正在修改,但其他所有内容都没有更改,并且不和谐表示交互失败。

看起来我正在不和谐方面度过一段时间。我想我缺少如何修改它的按钮部分

也许这段代码可以重构得更小,请随时提供一些提示

const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, MessageEmbed } = require('discord.js');
const fs = require('fs');

const jsonData = fs.readFileSync('src/pld.json');
const data = JSON.parse(jsonData);
const { getUsers, getFiche } = require('../../types/pld.js');// that go throw my json and give me only valid user and fiche

module.exports = {
  data: new SlashCommandBuilder()
    .setName('fiche')
    .setDescription('Affiche et gere vos fiches'),
  async execute(interaction) {
    try {
      const users = await getUsers(data);
      const user = users.find(user => user.discord_id === interaction.user.id); //that way i find on the json who is asking for this commande 
      const fiche = await getFiche(user); //i only have the fiche of the user that is valid
      let x = 0;

      const emdedBuilder = (x) => {
        const embed = new EmbedBuilder()
        .setColor('#0099ff')
        .setTitle(fiche[x].name)
        .setDescription(fiche[x].description)
        .setAuthor({ name: interaction.member.nickname, iconURL: 'https://cdn.discordapp.com/avatars/' + interaction.user.id + "/" + interaction.user.avatar, url: 'https://cdn.discordapp.com/avatars/' + interaction.user.id + "/" + interaction.user.avatar })
        .setTimestamp()
        .addFields(
          fiche[x].dod.map((dod) => ({
            name: dod.name,
            value: dod.done ? ":white_check_mark:" : ":x:",
            inline: true,
          }))
        )
        .setFooter({ text: `Fiche ${x + 1}/${fiche.length}` });
        
        return embed;
      }


      const previousButton = new ButtonBuilder()
        .setCustomId('previous')
        .setLabel("⬅️")
        .setStyle(1)

      const nextButton = new ButtonBuilder()
        .setCustomId('next')
        .setLabel("➡️")
        .setStyle(1)


      const response = await interaction.reply({ embeds: [emdedBuilder(x)], components: [
        new ActionRowBuilder({
          components: [
          ...(x > 0 ? [previousButton] : []),
           ...(x + 1  < fiche.length ? [nextButton] : [])
          ]
        })
      ], ephemeral: true });

      const collectorFilter = i => i.user.id === interaction.user.id;
      const confirmation = await response.awaitMessageComponent({ filter: collectorFilter, time: 600000 });
      if (confirmation.customId === 'previous' && x > 0) {
        x = x - 1;

        interaction.editReply({ embeds: [emdedBuilder(x)], components: [
          new ActionRowBuilder({
            components: [
            ...(x > 0 ? [previousButton] : []),
             ...(x + 1  < fiche.length ? [nextButton] : [])
            ]
          })], ephemeral: true });
        return;
      }
      if (confirmation.customId === 'next' && x < fiche.length -1) {
        x = x + 1;

        interaction.editReply({ embeds: [emdedBuilder(x)], components: [
          new ActionRowBuilder({
            components: [
            ...(x > 0 ? [previousButton] : []),
             ...(x + 1  < fiche.length ? [nextButton] : [])
            ]
          })], ephemeral: true });
        return;
      }

    } catch (error) {
      console.log(error);
    }
  }
};

为什么只有嵌入得到修改,以及为什么即使我的嵌入得到修改,不和谐也会给我带来交互失败

javascript discord discord.js
1个回答
0
投票

问题在于,当有人单击按钮时,机器人无法正确识别,这会在尝试再次交互时导致错误。

添加以下行,它应该修复它,确保按钮单击。

await confirmation.deferUpdate(); 

添加 LINE 后,您的最终代码将如下所示。

const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, MessageEmbed } = require('discord.js');
const fs = require('fs');

const jsonData = fs.readFileSync('src/pld.json');
const data = JSON.parse(jsonData);
const { getUsers, getFiche } = require('../../types/pld.js');// that go throw my json and give me only valid user and fiche

module.exports = {
  data: new SlashCommandBuilder()
    .setName('fiche')
    .setDescription('Affiche et gere vos fiches'),
  async execute(interaction) {
    try {
      const users = await getUsers(data);
      const user = users.find(user => user.discord_id === interaction.user.id); //that way i find on the json who is asking for this commande 
      const fiche = await getFiche(user); //i only have the fiche of the user that is valid
      let x = 0;

      const emdedBuilder = (x) => {
        const embed = new EmbedBuilder()
        .setColor('#0099ff')
        .setTitle(fiche[x].name)
        .setDescription(fiche[x].description)
        .setAuthor({ name: interaction.member.nickname, iconURL: 'https://cdn.discordapp.com/avatars/' + interaction.user.id + "/" + interaction.user.avatar, url: 'https://cdn.discordapp.com/avatars/' + interaction.user.id + "/" + interaction.user.avatar })
        .setTimestamp()
        .addFields(
          fiche[x].dod.map((dod) => ({
            name: dod.name,
            value: dod.done ? ":white_check_mark:" : ":x:",
            inline: true,
          }))
        )
        .setFooter({ text: `Fiche ${x + 1}/${fiche.length}` });
        
        return embed;
      }


      const previousButton = new ButtonBuilder()
        .setCustomId('previous')
        .setLabel("⬅️")
        .setStyle(1)

      const nextButton = new ButtonBuilder()
        .setCustomId('next')
        .setLabel("➡️")
        .setStyle(1)


      const response = await interaction.reply({ embeds: [emdedBuilder(x)], components: [
        new ActionRowBuilder({
          components: [
          ...(x > 0 ? [previousButton] : []),
           ...(x + 1  < fiche.length ? [nextButton] : [])
          ]
        })
      ], ephemeral: true });

      const collectorFilter = i => i.user.id === interaction.user.id;
      const confirmation = await response.awaitMessageComponent({ filter: collectorFilter, time: 600000 });

      await confirmation.deferUpdate(); // Added this line

      if (confirmation.customId === 'previous' && x > 0) {
        x = x - 1;

        interaction.editReply({ embeds: [emdedBuilder(x)], components: [
          new ActionRowBuilder({
            components: [
            ...(x > 0 ? [previousButton] : []),
             ...(x + 1  < fiche.length ? [nextButton] : [])
            ]
          })], ephemeral: true });
        return;
      }
      if (confirmation.customId === 'next' && x < fiche.length -1) {
        x = x + 1;

        interaction.editReply({ embeds: [emdedBuilder(x)], components: [
          new ActionRowBuilder({
            components: [
            ...(x > 0 ? [previousButton] : []),
             ...(x + 1  < fiche.length ? [nextButton] : [])
            ]
          })], ephemeral: true });
        return;
      }

    } catch (error) {
      console.log(error);
    }
  }
};
© www.soinside.com 2019 - 2024. All rights reserved.