This is my code:
const WebSocket = require('ws');
const dgram = require('dgram');
const fs = require('fs');
function start(token, guild, endpoint, id, session) {
console.log(token, guild, endpoint, id, session);
console.log('Connecting to voice gateway...');
// Play audio.mp3
const audio = fs.readFileSync('audio.mp3');
const ws = new WebSocket("wss:" + endpoint);
ws.on('open', () => {
console.log('Connected to voice gateway');
ws.send(JSON.stringify({
op: 0,
d: {
server_id: guild,
user_id: id,
session_id: session,
token: token
}
}));
})
ws.on('message', (data) => {
data = JSON.parse(data);
console.log(data);
if (data.op === 2) {
console.log('Received voice gateway data');
const udp = dgram.createSocket('udp4');
udp.send(audio, 0, audio.length, data.d.port, data.d.ip);
udp.on('message', (data) => {
console.log(data);
});
// Send heartbeat
setInterval(() => {
ws.send(JSON.stringify({
op: 3,
d: Date.now()
}));
}
, data.d.heartbeat_interval);
}
})
}
module.exports = { start };
For some reason, nothing happens when I start the function after my bot joins the voice, I have an already made audio.mp3 file ready, I just can't find a good way to stream it to the websocket.
https://discord.com/developers/docs/topics/voice-connections