Search code examples
discordlagdiscord.js

ping and packet loss go up when receiving data from wit.ai


I'm trying to make a discord bot that works with voice commands, (using wit.ai for speech to text) and I have got it working but when it sends or receives data to/from wit.ai the ping goes way up to like 350 ms.

Also the outbound packet loss rate on the voice channel goes up to around 20%, which is annoying, because then you sound robotic and it lags. When you click on debug discord says that my bot is inbound but its not sending anything to the voice channel so why would it be inbound??

The bot sends data to wit.ai every time someone speaks, so it will almost always up at high ping and stuff. If anyone can improve my code (or maybe a different library?) to make it affect the ping and packets(much) then that would be great.

Thanks in advance. Here's the code that runs when someone speaks:

const accessHeaders = function (access_token, others) {
    return _.extend(others || {}, {
        'Authorization': 'Bearer ' + access_token,
        'Accept': 'application/vnd.wit.' + "20170217"
    });
};


// Request options
const request_options = {
    url: 'https://api.wit.ai/speech',
    method: 'POST',
    json: true,
    headers: accessHeaders('TKOAGFCMGDIRYNZU36XHQDFD32HWXM6O', { 'Content-Type': 'audio/wav' })
};



var wav = require('wav');
var converter = new wav.Writer();

const receiver = member.guild.voiceConnection.createReceiver();
const inputStream = receiver.createPCMStream(member);
inputStream.pipe(converter);

converter.pipe(request(request_options, (error, response, body) => {
    receiver.destroy();
    if (response && response.statusCode != 200) {
        error = "Invalid response received from server: " + response.statusCode;
    }
    if(error)
        return console.log(error);
    console.log("you said: " + body._text);
}));

Solution

  • The lag didn't have anything to do with my code. It was on witai's end, and now they've fixed it.