This repository has been archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
silence-bot.js
63 lines (51 loc) · 1.72 KB
/
silence-bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const fs = require("fs");
const Discord = require("discord.js");
const client = new Discord.Client();
const { Readable } = require("stream");
const SILENCE_FRAME = Buffer.from([0xf8, 0xff, 0xfe]);
const DISCORD_TOKEN = process.env.DISCORD_TOKEN_SILENCE;
const controlChannel = "studio3";
const broadcastChannel = "listen";
class Silence extends Readable {
_read() {
this.push(SILENCE_FRAME);
}
}
client.once("ready", () => {
console.log("Ready!");
});
client.on("message", async (message) => {
// Ignore bot messages
if (message.member.user.bot) {
return;
}
// Only allow control from one channel
if (message.channel.name !== controlChannel) {
console.log("Incorrect control channel used");
return;
}
// User must be in the correct channel
if (message.member.voice.channel.name !== broadcastChannel) {
console.log("Users not in broadcast channel");
return;
}
if (message.content === "!listen" && message.member.voice.channel) {
const connection = await message.member.voice.channel.join();
const audio = connection.receiver.createStream(message.member, {
mode: "pcm",
end: "manual"
});
audio.pipe(fs.createWriteStream("user_audio"));
connection.play(new Silence(), { type: "opus" });
console.log("Joined channel: " + message.member.voice.channel.name);
}
});
client.on("message", async (message) => {
// Disconnect from the same voice channel of the author of the message
if (message.content === "!disconnect-silence" && message.member.voice.channel) {
const connection = await message.member.voice.channel.join();
connection.disconnect();
console.log("Disconnected from: " + message.channel.name);
}
});
client.login(DISCORD_TOKEN);