5

Normally I am able to solve my own issues, but this time I am unable to even find the core reason for the issue and I desperately need help.

When downloading streams and playing them back to voice channels in Discord, I noticed the bot's audio got glitchy/choppy roughly every 20-22 seconds. After days of testing, I figured it was all due to sent packet loss. However, I still am unable find the source of the issue.

What I checked:

  • the code (I am 100% sure the issue isn't with my code because it was doing the same exact thing with two different bots operating differently)
  • discord.js version (both v12 and v13 yielded same results)
  • CPU, disk and RAM usage (nothing out of the ordinary, the server isn't stressed at all)
  • opus/ffmpeg-static/fluent-ffmpeg package versions (all at latest versions)
  • server speed using both ookla speedtest and speedtest npm package (both showed high speeds, so the issue isn't with connection speed at all)

Additional information:

  • node.js: v14.18.0 (the bot works on my PC with node.js of this version, without any issues at all)
  • discord.js: v13.2.0
  • Ubuntu: 20.04.3 LTS

Discord's bot connection stats after playing over two minutes of music (top lines may have increased numbers because I had to stitch two screenshots together)

Discord's bot connection status

The code

Discord.js v13 voice connection code (simplified):

var connectToChannel = async function (channel) {
    const connection = joinVoiceChannel({
        channelId: channel.id,
        guildId: channel.guild.id,
        adapterCreator: channel.guild.voiceAdapterCreator,
    });
    connection.resource = null;
    connection.play = function(resource) {
        connection.on = function(eventType, cb) { player.player.on(eventType, cb); return connection; };
        connection.resource = createAudioResource(resource, {
            inlineVolume: true
        });

        player.player.play(connection.resource);
        connection.subscribe(player.player);
        return connection;
    }
    return connection;
};
// somewhere else...
const connection = connectToChannel(member.voice.channel).then(async connection => {
    connection.play(ytdl(yturl));
}

(I'm using discord-ytdl-core (which doesn't need highwatermark option), but I tried ytdl-core as well. I even tried loading a literal stream from a file and played it back, with exact same results (!!))

Discord.js v12 voice connection code (this time around playing a stream from a file; it doesn't matter where the stream came from, the results stays same.):

var voiceChannel = message.member.voice.channel;
voiceChannel.join().then(connection => {
    console.log("preparing to start");
    const dispatcher = connection.play('./_.ogg', { highWaterMark: 50 });
}).catch(err => console.log(err));

(Adjusting the highWaterMark option, even to 10MB, does not have any effect on the packet loss at all.)

If anyone would have any ideas as to what could be the issue or at least check further to find out the reason, I would be absolutely grateful.

PDKnight
  • 712
  • 6
  • 25
  • Can we see the playing code? Just to make sure it's not a problem with code – MrMythical Oct 25 '21 at 13:41
  • @MrMythical I changed the question just for you, but I am very sure the issue isn't with the code as I tried gazillions of variants of the code in different scenarios and the issue persisted + the bot worked well with this code before until it started glitching. – PDKnight Oct 25 '21 at 14:03
  • What is your packet TTL on the device/server you're running your bot on? I've had issues in the past where I set a smaller TCP/IP packet TTL (so I could use my phones hotspot without my carrier counting it against my hotspot balance) and I experienced issues with discord calls soft dropping and packet loss. I didn't even realize the cause for 6 months. – spicy.dll Oct 25 '21 at 14:04
  • @spicy.dll Fair, thanks for the ideas! The TTL is as by default set to 64. I tried to raise it to twice as much, but the effect was the same. – PDKnight Oct 25 '21 at 15:42
  • @PDKnight That was the same TTL I was having issues with. I changed mine to 128. You may need to reboot your system in order for the TTL change to take affect. I remember even having to reboot my router and modem for some reason as well. – spicy.dll Oct 25 '21 at 18:23
  • 1
    With discord.js V13, your bot should not be able to run with a node version that "old". Have you tried it, with the latest version available? – Christoph Blüm Oct 26 '21 at 17:25
  • 1
    Following on what @ChristophBlüm said, any versions of Node.js that aren't compatible with Discord.js v13 will cause a lot of issues. Try using Node v16. I've had issues in the past and they all were resolved by a simple update. – Coder Tavi Oct 28 '21 at 18:13
  • @spicy.dll Thank you! I tried changing the value the same day (even put it to the maximum of 255), it didn't work even after rebooting. I'll ask the owner to restart the router if nothing works! – PDKnight Oct 28 '21 at 22:02
  • @ChristophBlüm + Coder Tavi Thank you both, I will update the nodejs tomorrow. The reason why I left it at lower version was to avoid tons of issues the update caused, but I will push through and face them all. This has to be fixed no matter what. If that doesn't work either, I will be very hopeless. – PDKnight Oct 28 '21 at 22:02
  • Hi! As @ChristophBlüm and Coder Tavi have already said, Node.js 16.6.0 or newer is required to run Discord.JS v13. I've myself gotten lots of issues in the past with Node.js v15. You should try updating your Node and tell us how it goes :) – Fowled Oct 30 '21 at 19:57
  • 1
    Greetings @Fowled, thank you! I updated node.js to the very latest version and it did unfortunately not have any effect. I may probably reinstall the whole ubuntu to make sure the issue isn't on my end. I tried everything, I have no idea now. – PDKnight Oct 31 '21 at 18:45
  • 1
    If anyone is wondering, the case is closed. It was server host's issue all this time. I did restart the whole machine and reinstall everything from the ground up and the issue persisted. It's very likely their network problem and I will choose a different server. Thank you everyone for the help! – PDKnight Nov 05 '21 at 14:38

0 Answers0