Discord.js music bot hosted on heroku crashes after couple minutes

I have a discord bot hosted on heroku which among it’s other functionalities, also provides members to listen to music (much like the famous Rythm bot). However, every time I play a song, the bot crashes and restarts itself after 7-10 of playing songs. This only seems to happen when I’m playing music. Other than that, the bot seems to operate smoothly without any issues or crashes. I’ve included the start of the error message I get below (full error is very long)

node:events:371
      throw er; // Unhandled 'error' event
      ^

AudioPlayerError: aborted
    at connResetException (node:internal/errors:691:14)
    at TLSSocket.socketCloseListener (node:_http_client:407:19)
    at TLSSocket.emit (node:events:406:35)
    at node:net:672:12
    at TCP.done (node:_tls_wrap:580:7)
Emitted 'error' event on AudioPlayer instance at:
    at OggDemuxer.onStreamError (/app/node_modules/@discordjs/voice/dist/audio/AudioPlayer.js:213:22)
    at Object.onceWrapper (node:events:514:26)
    at OggDemuxer.emit (node:events:406:35)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  resource: <ref *4> AudioResource {
    playbackDuration: 486900,
    started: true,
    silenceRemaining: -1,
    edges: [
      <ref *1> {
        type: 'ffmpeg ogg',
        to: Node {
          edges: [ [Object], [Object], [Object] ],
          type: 'ogg/opus'
        },
        cost: 2,
        transformer: [Function: transformer],
        from: Node { edges: [ [Object], [Circular *1] ], type: 'arbitrary' }
      },
      <ref *2> {
        type: 'ogg/opus demuxer',
        to: Node { edges: [ [Object] ], type: 'opus' },
        cost: 1,
...

Answer

Often times when making bots (such as music bots), we can get these strange errors that are very difficult to debug as it is likely an issue with the library/discord API. I’ve gotten similar errors regarding permissions.

Are you using .catch() on events/promises? Based on the error message, it suggests you are not properly handling errors.

This could also be due to trying to play audio that is not downloaded yet. You should always use a promise when getting the audio and wait for it to return before trying to play it, then finally using .catch().