相关文章推荐
被表白的围巾  ·  JS散度(Jensen–Shannon ...·  1 月前    · 
另类的核桃  ·  六个零碳建筑案例·  10 月前    · 
没读研的奔马  ·  c# - What is the ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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! index@1.0.0 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 index@1.0.0 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);

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!

The hosting i am using is pebblehost and it is only using 145 MB of memory and the plan i bought provides 1024 MB. So i don't think that is the issue. – BlurryMeal Aug 26, 2021 at 12:32

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

npm uninstall opusscript
npm install @discordjs/opus
                As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
– Community
                Feb 11, 2022 at 19:16
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.