RuntimeError: memory access out of bounds for Discord Bot [Node.JS] using Distube NPM Module

368 Views Asked by At

I have made music commands for my discord bot using the distube NPM module. The issue i am facing is that sometimes ( this doesn't happen always ) and its not with a specific command, it occurs randomly with any music command under distube.

This is the error that i am facing.

26.08 00:11:45 [Bot] Startup wasm://wasm/0010d1fa:1
26.08 00:11:45 [Bot] Startup ^
26.08 00:11:45 [Bot] Startup RuntimeError: memory access out of bounds
26.08 00:11:45 [Bot] Startup at <anonymous>:wasm-function[268]:0x2177a
26.08 00:11:45 [Bot] Startup at <anonymous>:wasm-function[267]:0x21732
26.08 00:11:45 [Bot] Startup at OpusScriptHandler$_decode [as _decode] (eval at Db (/node_modules/opusscript/build/opusscript_native_wasm.js:1:1), <anonymous>:11:10)
26.08 00:11:45 [Bot] Startup at OpusScript.decode (/node_modules/opusscript/index.js:80:28)
26.08 00:11:45 [Bot] Startup at Decoder._decode (/node_modules/prism-media/src/opus/Opus.js:64:25)
26.08 00:11:45 [Bot] Startup at Decoder._transform (/node_modules/prism-media/src/opus/Opus.js:189:20)
26.08 00:11:45 [Bot] Startup at Decoder.Transform._read (internal/streams/transform.js:205:10)
26.08 00:11:45 [Bot] Startup at Decoder.Transform._write (internal/streams/transform.js:193:12)
26.08 00:11:45 [Bot] Startup at writeOrBuffer (internal/streams/writable.js:358:12)
26.08 00:11:45 [Bot] Startup at Decoder.Writable.write (internal/streams/writable.js:303:10)
26.08 00:11:45 [Bot] Startup at Encoder.ondata (internal/streams/readable.js:719:22)
26.08 00:11:45 [Bot] Startup at Encoder.emit (events.js:315:20)
26.08 00:11:45 [Bot] Startup at addChunk (internal/streams/readable.js:309:12)
26.08 00:11:45 [Bot] Startup at readableAddChunk (internal/streams/readable.js:284:9)
26.08 00:11:45 [Bot] Startup at Encoder.Readable.push (internal/streams/readable.js:223:10)
26.08 00:11:45 [Bot] Startup at Encoder.Transform.push (internal/streams/transform.js:166:32)
26.08 00:11:45 [Bot] Startup npm ERR! code ELIFECYCLE
26.08 00:11:45 [Bot] Startup npm ERR! errno 1
26.08 00:11:45 [Bot] Startup npm ERR! [email protected] start: `node index.js`
26.08 00:11:45 [Bot] Startup npm ERR! Exit status 1
26.08 00:11:45 [Bot] Startup npm ERR! 
26.08 00:11:45 [Bot] Startup npm ERR! Failed at the [email protected] start script.
26.08 00:11:45 [Bot] Startup npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
26.08 00:11:45 [Bot] Startup npm ERR! A complete log of this run can be found in:
26.08 00:11:45 [Bot] Startup npm ERR!     /tmp/npm/_logs/2021-08-26T04_11_45_714Z-debug.log
26.08 00:11:46 [PebbleHost] Server shut down (running)
26.08 00:11:46 [PebbleHost] Server stopped
26.08 00:11:58 [PebbleHost] Received start command
26.08 00:11:58 [PebbleHost] Starting server!

This is the code for the music commands.

const DisTube = require('distube'),
  config = {
    prefix: "x",
  };

// Create a new DisTube
const distube = new DisTube(client, {
  searchSongs: true,
  emitNewSongOnly: true
});

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on("message", async(message) => {
  if (message.author.bot) return;
  if (!message.content.startsWith(config.prefix)) return;
  const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  const command = args.shift();

  if (command == "play")
    distube.play(message, args.join(" "));

  if (["repeat", "loop"].includes(command))
    distube.setRepeatMode(message, parseInt(args[0]));

  if (command == "loop" || command == "repeat") {
    const exampleEmbed = new Discord.MessageEmbed()
      .setColor('#9966CC')
      .setTitle('Current Song has been Looped!')

    message.channel.send(exampleEmbed);
  }

  if (command == "stop") {
    distube.stop(message);
    const exampleEmbed = new Discord.MessageEmbed()
      .setColor('#9966CC')
      .setTitle('Bot has left the voice channel!')

    message.channel.send(exampleEmbed);
  }
  if (command == "skip") {
    const exampleEmbed = new Discord.MessageEmbed()
      .setColor('#9966CC')
      .setTitle('Song has been skipped!')

    message.channel.send(exampleEmbed);
  }

  if (command == "skip")
    distube.skip(message);

  if (command == "volume")
    distube.setVolume(message, args[0]);
  if (command == "volume") {
    const exampleEmbed = new Discord.MessageEmbed()
      .setColor('#9966CC')
      .setTitle(`Volume has been set to ${args[0]}% `)

    message.channel.send(exampleEmbed);
  }

  if (command == "jump")
    distube.jump(message, parseInt(args[0] - 1));

  if (command == "shuffle")
    distube.shuffle(message);

  if (command == "shuffle") {
    const exampleEmbed = new Discord.MessageEmbed()
      .setColor('#9966CC')
      .setTitle(`Queue has been shuffled!`)

    message.channel.send(exampleEmbed);
  }

  if (command == "pause")
    distube.pause(message);


  if (command == "pause") {
    const exampleEmbed = new Discord.MessageEmbed()
      .setColor('#9966CC')
      .setTitle(`Current Song has been paused!`)

    message.channel.send(exampleEmbed);
  }


  if (command == "resume")
    distube.resume(message);

  if (command == "resume") {
    const exampleEmbed = new Discord.MessageEmbed()
      .setColor('#9966CC')
      .setTitle(`Current Song has been resumed!`)

    message.channel.send(exampleEmbed);
  }

  if (command == "queue") {
    let queue = distube.getQueue(message);
    const exampleEmbed = new Discord.MessageEmbed()
      .setColor('#9966CC')
      .setTitle('Current Queue')
      .setDescription(queue.songs.map((song, id) =>
        `**${id + 1}**) ${song.name} - \`${song.formattedDuration}\``).slice(0, 10).join("\n"))

    message.channel.send(exampleEmbed);
  }

  if ([`3d`, `bassboost`, `echo`, `karaoke`, `nightcore`, `vaporwave`, `reverse`, `surround`, `earwax`].includes(command)) {
    let filter = distube.setFilter(message, command);
    const exampleEmbed = new Discord.MessageEmbed()
      .setColor('#9966CC')
      .setTitle('Current Filter')
      .setDescription(filter || 'Off')

    message.channel.send(exampleEmbed);
  }
});

// Queue status template
const status = (queue) => `Volume: \`${queue.volume}%\` | Filter: \`${queue.filter || "Off"}\` | Loop: \`${queue.repeatMode ? queue.repeatMode == 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\``;

// DisTube event listeners, more in the documentation page
distube

  .on("playSong", (message, queue, song) =>
    message.channel.send(
      `Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`
    ))
  .on("addSong", (message, queue, song) => message.channel.send(
    `Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`
  ))
  .on("playList", (message, queue, playlist, song) => message.channel.send(
    `Play \`${playlist.name}\` playlist (${playlist.songs.length} songs).\nRequested by: ${song.user}\nNow playing \`${song.name}\` - \`${song.formattedDuration}\`\n${status(queue)}`
  ))
  .on("addList", (message, queue, playlist) => message.channel.send(
    `Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`
  ))
  // DisTubeOptions.searchSongs = true
  .on("searchResult", (message, result) => {
    let i = 0;
    const exampleEmbed = new Discord.MessageEmbed()
      .setColor('#9966CC')
      .setTitle('Choose an option from below')
      .setDescription(`${result.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n\n*Enter anything else or wait 60 seconds to cancel*`)
    message.channel.send(exampleEmbed);

  })
  // DisTubeOptions.searchSongs = true
  .on("searchCancel", (message) => message.channel.send(`Searching canceled`))
  .on("error", (message, e) => {
    console.error(e)
    message.channel.send("An error encountered: " + e);
  });
2

There are 2 best solutions below

1
On

I'm not pretty sure of my answer but it seems you're running out of memory as it shows in lines 3,

26.08 00:11:45 [Bot] Startup RuntimeError: memory access out of bounds

So maybe try to get a better host with more memory that can handle your bot!

1
On

Due to opusscript package. Uninstall it and install @discordjs/opus instead.

npm uninstall opusscript
npm install @discordjs/opus