简体   繁体   中英

This interaction failed - Discord.js

I'm making a discord slash command nowplaying with save button in it and sent the current music playing in user's dm. i manage to create the buttons working but my problem is after clicking the button "Save Song" it says "This interaction failed" tho the buttons works it sent a embed of the current music playing. Can someone help me why it saying that error?

Node: v17.7.2

Discord: ^13.2.0

my InteractionCreate events for the button "Save Song"

const client = require("../index");
const { MessageEmbed } = require('discord.js');
const ee = require('../config.json');

client.on("interactionCreate", async (interaction) => {
    // Slash Command Handling
    if (interaction.isCommand()) {
        await interaction.deferReply({ ephemeral: false }).catch(() => {});

        const cmd = client.slashCommands.get(interaction.commandName);
        if (!cmd)
            return interaction.followUp({ content: "An error has occured " });

        const args = [];

        for (let option of interaction.options.data) {
            if (option.type === "SUB_COMMAND") {
                if (option.name) args.push(option.name);
                option.options?.forEach((x) => {
                    if (x.value) args.push(x.value);
                });
            } else if (option.value) args.push(option.value);
        }
        interaction.member = interaction.guild.members.cache.get(interaction.user.id);

        cmd.run(client, interaction, args);
    }

    // Context Menu Handling
    if (interaction.isContextMenu()) {
        await interaction.deferReply({ ephemeral: false });
        const command = client.slashCommands.get(interaction.commandName);
        if (command) command.run(client, interaction);
    }

  //Save Song button function
    if (interaction.isButton()){
        const queue = client.distube.getQueue(interaction.guildId);
    switch (interaction.customId) {
        case 'saveTrack': {
       if (!queue || !queue.playing){
       return interaction.followUp({ content: `No music currently playing. ❌`, ephemeral: true, components: [] });
       } else {
         const song = queue.songs[0];
      const but_save = new MessageEmbed()
      .setColor(ee.color)
      .setTitle(client.user.username + " - Save Track")
      .setThumbnail(client.user.displayAvatarURL())
      .addField(`🎶 Track`, `\`${song.name}\``)
      .addField(`⏳ Duration`, `\`${song.formattedDuration}\``, true)
      .addField(`🔗 URL`, `${song.url}`)
      .addField(`㊗ Saved Server`, `\`${interaction.guild.name}\``)
      .addField(`➡ Requested By`, `${song.user}`, true)
      .setTimestamp()
      .setFooter({ text: 'H_M Save Music!', iconURL: interaction.user.displayAvatarURL({ dynamic: true }) });
       interaction.user.send({ embeds: [but_save ] }).then(() => {
        interaction.followUp({ content: `✅ | I sent the name of the music via private message.`, ephemeral: true }).catch(e => { })
        }).catch(error => {
        interaction.followUp({ content: `❌ | Unable to send you private message.`, ephemeral: true }).catch(e => { })
        });
       }
      }
    }
  }
});

and here is my nowplaying.js

const { MessageEmbed, MessageActionRow, MessageButton  } = require('discord.js');
const ee = require('../../config.json');
const Format = Intl.NumberFormat();
const status = queue =>
`Volume: \`${queue.volume}%\` | Filters: \`${queue.filters.join(', ') || 'Off'}\` | Loop: \`${
    queue.repeatMode ? (queue.repeatMode === 2 ? 'Playlist' : 'Song') : 'Off'
  }\` | Autoplay: \`${queue.autoplay ? 'On' : 'Off'}\``

module.exports = {
    name: "nowplaying",
    description: "Shows the current song playing",
    usage: "nowplaying",
    run: async (client, interaction, args) => {
        const queue = client.distube.getQueue(interaction);

        const song = queue.songs[0];
        const embed = new MessageEmbed()
        .setColor(ee.color)
        .setAuthor({name: 'Now playing...', iconURL: 'https://i.imgur.com/81ig9jl.jpg'})
        .setDescription(`[${song.name}](${song.url})`)
        .setThumbnail(song.thumbnail)
        .addField("🌭 | Status", `${status(queue).toString()}`, false)
        .addField('👀 | Listens', `${Format.format(song.views)}`, true)
        .addField('👍 | Prefer', `${Format.format(song.likes)}`, true)
        .addField('⌛ | Played', `${queue.formattedCurrentTime} / ${song.formattedDuration}`, true)
        .addField('📩 | Download link', `[Click here](${song.streamURL})`, true)
        .addField("👌 | Requested by",` ${song.user}`, true)

        const saveButton = new MessageButton();

        saveButton.setLabel('Save Song');
        saveButton.setCustomId('saveTrack');
        saveButton.setStyle('SUCCESS');

        const row = new MessageActionRow().addComponents(saveButton);
      
        interaction.followUp({embeds: [embed], components: [row] });
    }
}

After you switched case to your id, you need to defer your interaction with await interaction.deferReply() also if you want it an ephemeral message there's option as await interaction.deferReply({ ephemeral: true }) . This line should be awaited.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM