Androz2091 / discord-player

🎧 Complete framework to simplify the implementation of music commands using discord.js v14
https://discord-player.js.org/
MIT License
603 stars 190 forks source link

Bot crash while trying to play long youtube video. #1374

Closed chaos3326 closed 2 years ago

chaos3326 commented 2 years ago

The bot worked perfectly, until it won't. When I tried to add a very long video (like 3 hours or longer) or sometimes it happened to short video when the queue is already long enough, the bot will crash with the following error in the console:

DiscordAPIError[10062]: Unknown interaction
    at SequentialHandler.runRequest (D:\Coding\DiscordBot\imaginary-friend\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:287:15)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (D:\Coding\DiscordBot\imaginary-friend\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:99:14)
    at async REST.request (D:\Coding\DiscordBot\imaginary-friend\node_modules\@discordjs\rest\dist\lib\REST.cjs:52:22)
    at async ChatInputCommandInteraction.reply (D:\Coding\DiscordBot\imaginary-friend\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:110:5)      
    at async Object.execute (D:\Coding\DiscordBot\imaginary-friend\src\commands\music\play.js:119:5)
    at async Object.execute (D:\Coding\DiscordBot\imaginary-friend\src\events\client\interactionCreate.js:10:9) {
  rawError: { message: 'Unknown interaction', code: 10062 },
  code: 10062,
  status: 404,
  method: 'POST',
  url: 'https://discord.com/api/v10/interactions/1023172127735689226/aW50ZXJhY3Rpb246MTAyMzE3MjEyNzczNTY4OTIyNjpzWlZvSWVISDBqVjRjdGhudnpwZUVwV29qQmh5Rk9CWVFRNU56Wk0wYnQ2WFp5V05pUkgyZTBiUzBobEt2dVlhZUdvSzVVczJDbEF0SWtoYW43OUpMM1l6bUt3VUV3Vlc2dndOcnduN1FvelhCaG1PandBZmluUlNGMjVkRzU3OQ/callback',
  requestBody: { files: [], json: { type: 4, data: [Object] } }
}
D:\Coding\DiscordBot\imaginary-friend\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:287
        throw new DiscordAPIError.DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
              ^

DiscordAPIError[10062]: Unknown interaction
    at SequentialHandler.runRequest (D:\Coding\DiscordBot\imaginary-friend\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:287:15)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (D:\Coding\DiscordBot\imaginary-friend\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:99:14)
    at async REST.request (D:\Coding\DiscordBot\imaginary-friend\node_modules\@discordjs\rest\dist\lib\REST.cjs:52:22)
    at async ChatInputCommandInteraction.reply (D:\Coding\DiscordBot\imaginary-friend\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:110:5)      
    at async Object.execute (D:\Coding\DiscordBot\imaginary-friend\src\events\client\interactionCreate.js:13:9) {
  rawError: { message: 'Unknown interaction', code: 10062 },
  code: 10062,
  status: 404,
  method: 'POST',
  url: 'https://discord.com/api/v10/interactions/1023172127735689226/aW50ZXJhY3Rpb246MTAyMzE3MjEyNzczNTY4OTIyNjpzWlZvSWVISDBqVjRjdGhudnpwZUVwV29qQmh5Rk9CWVFRNU56Wk0wYnQ2WFp5V05pUkgyZTBiUzBobEt2dVlhZUdvSzVVczJDbEF0SWtoYW43OUpMM1l6bUt3VUV3Vlc2dndOcnduN1FvelhCaG1PandBZmluUlNGMjVkRzU3OQ/callback',
  requestBody: {
    files: [],
    json: {
      type: 4,
      data: {
        content: 'Something went wrong while trying to execute the command!',
        tts: false,
        nonce: undefined,
        embeds: undefined,
        components: undefined,
        username: undefined,
        avatar_url: undefined,
        allowed_mentions: undefined,
        flags: 64,
        message_reference: undefined,
        attachments: undefined,
        sticker_ids: undefined
      }
    }
  }
}

I need support to handle this bug, like... this will add nothing to the queue instead of crash the bot. Thank you very much for readding this message.

retrouser955 commented 2 years ago

Can you send your code?

twlite commented 2 years ago

It is pretty much straightforward to know that this isnt a bug plus it is neither a problem caused by discord-player. You are trying to reply to an unknown interaction in your command-error handler (src\events\client\interactionCreate.js) file

chaos3326 commented 2 years ago

Here's the code in my play.js file:

const {
  SlashCommandBuilder,
  EmbedBuilder,
  // ActionRowBuilder,
  // ButtonBuilder,
  // ButtonStyle,
} = require("discord.js");
const { getVoiceConnection } = require("@discordjs/voice");
const { QueryType } = require("discord-player");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("play")
    .setDescription("play a song from YouTube.")
    .addStringOption((option) =>
      option
        .setName("input")
        .setDescription("search keywords, or video, playlist link")
        .setRequired(true)
    ),
  async execute(interaction, client) {
    // Make sure the user is inside a connectable voice channel
    if (!interaction.member.voice.channel)
      return await interaction.reply(
        "You need to be in a Voice Channel to play a song."
      );
    const mychannel = getVoiceConnection(interaction.guild.id)?.joinConfig
      .channelId;
    if (mychannel && interaction.member.voice.channelId !== mychannel)
      return await interaction.reply({
        content: "You are not in my voice channel!",
        ephemeral: true,
      });

    // Create a play queue for the server
    const queue = await client.player.createQueue(interaction.guild);

    // Wait until you are connected to the channel
    try {
      if (!queue.connection)
        await queue.connect(interaction.member.voice.channel);
    } catch {
      queue.destroy();
      return await interaction.reply({
        content: "Could not join your voice channel!",
        ephemeral: true,
      });
    }

    let embed = new EmbedBuilder();

    // Search for the song using the discord-player
    let input = interaction.options.getString("input");
    const result = await client.player.search(input, {
      requestedBy: interaction.user,
      searchEngine: QueryType.AUTO,
    });

    // finish if no tracks were found
    console.log(queue);
    if (result.tracks.length === 0) return interaction.editReply("No results");

    // if it's not a playlist
    if (!result.playlist) {
      if (result.tracks.length === 1) {
        const song = result.tracks[0];
        await queue.addTrack(song);
        embed
          .setAuthor({ name: song.author })
          .setDescription(
            `**[${song.title}](${song.url} "${song.title}")** has been added to the Queue`
          )
          .setThumbnail(song.thumbnail)
          .setFooter({ text: `Duration: ${song.duration}` });
      } else {
        const message = await Promise.all(
          result.tracks.map(async (track, index) => {
            if (index >= 10) return "";
            return `**.[${track.title}](${track.url} )** from channel **${track.author}**`;
          })
        );
        embed
          .setAuthor({
            name: interaction.member.nickname || interaction.user.tag,
            iconURL: interaction.member.displayAvatarURL(),
          })
          .setDescription(
            `**Please select a songs.**\n` +
              message.filter((x) => x.length > 0).join("\n")
          );
      }
    } else {
      const message = await Promise.all(
        result.tracks.map(async (track, index) => {
          await queue.addTrack(track);
          if (index >= 10) return "";
          return `**.[${track.title}](${track.url} )** from channel **${track.author}**`;
        })
      );
      embed
        .setAuthor({
          name: interaction.member.nickname || interaction.user.tag,
          iconURL: interaction.member.displayAvatarURL(),
        })
        .setDescription(
          `**Added songs** from **[${result.playlist.title}](${result.playlist.url} )** playlist:\n` +
            message.filter((x) => x.length > 0).join("\n") +
            (result.tracks.length > 10
              ? `\n*and ${result.tracks.length - 10} more*`
              : ``)
        );
      console.log(result);
    }

    // Play the song
    if (!queue.playing) await queue.play();

    // Respond with the embed containing information about the player
    await interaction.reply({
      embeds: [embed],
    });
  },
};
chaos3326 commented 2 years ago

Sorry for being so dumb. I found the issue myself. It happened because I used interaction.editReply (line 61) but there is no reply yet for this interaction, so it crashed the bot.

chaos3326 commented 2 years ago

I just learned that discord only gives the bot 3s to do a task. If it needs more, it need defer reply. If the interaction has defer reply, it will use editReply instead of reply.