Please describe the problem you are having in as much detail as possible:
Receiving audio does not work.
Whole code of my bot is pasted below. I don't have any output in console, except information about bot logging. If i change stream to write file instead of playing it to same channel it just writes an empty file.
Include a reproducible code sample here, if possible:
import "dotenv/config";
import { Client } from "discord.js";
const client = new Client();
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("message", async (msg) => {
if (msg.content === "listen") {
const connection = await msg.member.voice.channel.join();
const userId = msg.member.id;
const audio = connection.receiver.createStream(userId, {
mode: "opus",
end: "manual",
});
audio
.on("data", (chunk) => console.log(chunk))
.on("close", () => console.log("close"))
.on("error", (e) => console.log(e))
.on("readable", () => console.log("readable"))
.on("close", () => console.log("closed"));
connection.play(audio, { type: "opus" });
}
});
client.login(process.env.DISCORD_TOKEN);
Further details:
I guess I should reference to https://github.com/discordapp/discord-api-docs/issues/808 this issue here
you need to send audio before you can receive. there was PR that _should_ have handled that by sending a silence frame. Yet I have to send the silence buffer manually aswell, so I suggest you do that too
@adrifcastr I changed the bot to play (different) audio whole time and write stream to file and the only change was that I got bunch of readable event triggers
Dependencies (maybe I am missing something?)
"dependencies": {
"@babel/core": "^7.9.6",
"@babel/node": "^7.8.7",
"@babel/preset-env": "^7.9.6",
"@discordjs/opus": "^0.3.2",
"discord.js": "^12.2.0",
"dotenv": "^8.2.0",
"ffmpeg": "^0.0.4",
"fluent-ffmpeg": "^2.1.2",
"nodemon": "^2.0.3"
}
I modified bot to emit one frame of silence, but -again- nothing has changed
import "dotenv/config";
import { Client } from "discord.js";
import { Readable } from "stream";
class Silence extends Readable {
_read() {
this.push(Buffer.from([0xf8, 0xff, 0xfe]));
}
}
const client = new Client();
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("message", async (msg) => {
if (msg.content === "listen") {
const connection = await msg.member.voice.channel.join();
connection.play(new Silence(), { type: "opus" });
const userId = msg.member.id;
const audio = connection.receiver.createStream(userId, {
mode: "opus",
end: "manual",
});
audio
.on("data", (chunk) => console.log(chunk))
.on("close", () => console.log("close"))
.on("error", (e) => console.log(e))
.on("readable", () => console.log("readable"))
.on("close", () => console.log("closed"));
connection.play(audio, { type: "opus" });
}
});
client.login(process.env.DISCORD_TOKEN);
Receiving voice works fine, the problem lies in a weird bug that your code triggers.
I noticed weird behavior when trying to play a stream that origins from the same connection.
I don't know if its d.js or discords fault, but when trying to play the stream on the same connection, that you just created it from, no data is being received.
Try removing
connection.play(audio, { type: "opus" });
To check if you now receive voice.
Here is my hack around the problem
doing a setTimeout somehow does the trick.
Additional Info:
When playing the just created stream in a different connection, there are no issues, it only happens while playing it within the same connection, which makes the bug even weirder
Closing as it is a stale issue, reopen or check open issues if this is still an issue.