2

My discord.js music bot randomly stops playing music. Here's the error report:

Emitted 'error' event on B instance at:

at OggDemuxer.t (/Users/myName/Bot/node_modules/@discordjs/voice/dist/index.js:8:288)

It says the error type is an "ECONNRESET" (code: 'ECONNRESET')

After this error the bot shortly goes offline and the code stops running (all commands don't work)

The code that I use to play music is as follows:

const { Client, Intents, MessageEmbed } = require('discord.js');
const { token } = require('./config.json');
const { joinVoiceChannel, createAudioPlayer, createAudioResource } = require('@discordjs/voice');
const ytdl = require('ytdl-core')
const ytSearch = require('yt-search')
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES] });
client.once('ready', () => {
    console.log('Ready!');
});
const videoFinder = async (search) => {
    const videoResult = await ytSearch(search)
    return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
    }
client.on('messageCreate', msg => {
if(msg.content === '!join') {
    connection = joinVoiceChannel({
        channelId: msg.member.voice.channel.id,
        guildId: msg.guild.id,
        adapterCreator: msg.guild.voiceAdapterCreator
    })
if (msg.content.substring(0, 6) === '!play ') {
    let searchContent = msg.content.substring(6)
    async function playChosen() {
    let ourVideo = await videoFinder(searchContent)
    const stream = ytdl(ourVideo.url, { filter: 'audioonly' })
    const player = createAudioPlayer()
    let resource = createAudioResource(stream)
    player.play(resource)
    connection.subscribe(player)
    }
    playChosen()
    }
    }

client.on('error', error => {
    console.error('The WebSocket encountered an error:', error);
});

client.login(token);

I know the code is kinda messy, but if anybody could help, I'd really appreciate it! Also, I use discord.js v13, and the bot will typically play part of the song before this error comes up.

NewCoder
  • 21
  • 3

1 Answers1

0

Yet again, I've posted a question that I quickly found an answer to lol. The problem here was being caused by ytdl-core, which would just randomly shut off for some reason. Play-dl worked much better to combat this. Here's a link to the place where I got this info: Discord.js/voice How to create an AudioResource?

play-dl link: https://www.npmjs.com/package/play-dl

example: https://github.com/play-dl/play-dl/blob/5a780517efca39c2ffb36790ac280abfe281a9e6/examples/YouTube/play%20-%20search.js

for the example I would suggest still using yt-search to obtain the url as I got some wonky results using their search method. Thank you!

NewCoder
  • 21
  • 3