I'm trying to use .setSelfDeaf() in my code but I couldn't figure it out. Every way I tried ended up crashing the bot when executed.
Could someone help me to use it? I want is the bot the deafen itself every time it joins a voice channel.
Edit: added the error I am getting and the updated code upon request.
The error I am getting:
2021-03-04T06:25:06.238627+00:00 app[worker.1]: /app/code.js:53
2021-03-04T06:25:06.238628+00:00 app[worker.1]: connection.voice.setSelfDeaf(true);
2021-03-04T06:25:06.238629+00:00 app[worker.1]: ^
2021-03-04T06:25:06.238629+00:00 app[worker.1]:
2021-03-04T06:25:06.238629+00:00 app[worker.1]: ReferenceError: connection is not defined
2021-03-04T06:25:06.238630+00:00 app[worker.1]: at Client.<anonymous> (/app/code.js:53:5)
2021-03-04T06:25:06.238630+00:00 app[worker.1]: at Client.emit (events.js:327:22)
2021-03-04T06:25:06.238631+00:00 app[worker.1]: at VoiceStateUpdate.handle (/app/node_modules/discord.js/src/client/actions/VoiceStateUpdate.js:40:14)
2021-03-04T06:25:06.238632+00:00 app[worker.1]: at Object.module.exports [as VOICE_STATE_UPDATE] (/app/node_modules/discord.js/src/client/websocket/handlers/VOICE_STATE_UPDATE.js:4:35)
2021-03-04T06:25:06.238632+00:00 app[worker.1]: at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
2021-03-04T06:25:06.238633+00:00 app[worker.1]: at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
2021-03-04T06:25:06.238633+00:00 app[worker.1]: at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
2021-03-04T06:25:06.238634+00:00 app[worker.1]: at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:132:16)
2021-03-04T06:25:06.238634+00:00 app[worker.1]: at WebSocket.emit (events.js:315:20)
2021-03-04T06:25:06.238634+00:00 app[worker.1]: at Receiver.receiverOnMessage (/app/node_modules/ws/lib/websocket.js:825:20)
2021-03-04T06:25:06.279643+00:00 heroku[worker.1]: Process exited with status 1
2021-03-04T06:25:06.340294+00:00 heroku[worker.1]: State changed from up to crashed
The updated code:
const Discord = require('discord.js'),
DisTube = require('distube'),
client = new Discord.Client(),
config = {
prefix: "em!",
token: process.env.TOKEN || "[insert discord bot token]"
};
const distube = new DisTube(client, { searchSongs: true, emitNewSongOnly: true });
client.on('ready', () => {
console.log(`e-Music is up and running`);
client.user.setActivity(`em!play`)
});
client.on("message", async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(config.prefix)) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift();
if (command == "play")
distube.play(message, args.join(" "));
if (["repeat", "loop"].includes(command))
distube.setRepeatMode(message, parseInt(args[0]));
if (command == "stop") {
distube.stop(message);
message.channel.send("Stopped the music!");
}
if (command == "skip")
distube.skip(message);
if (command == "queue") {
let queue = distube.getQueue(message);
message.channel.send('Current queue:\n' + queue.songs.map((song, id) =>
`**${id + 1}**. ${song.name} - \`${song.formattedDuration}\``
).slice(0, 10).join("\n"));
}
if ([`3d`, `bassboost`, `echo`, `karaoke`, `nightcore`, `vaporwave`].includes(command)) {
let filter = distube.setFilter(message, command);
message.channel.send("Current queue filter: " + (filter || "Off"));
}
});
client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => {
if (newVoiceState.id == client.user.id) {
connection.voice.setSelfDeaf(true);
};
});
const status = (queue) => `Volume: \`${queue.volume}%\` | Filter: \`${queue.filter || "Off"}\` | Loop: \`${queue.repeatMode ? queue.repeatMode == 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\``;
distube
.on("playSong", (message, queue, song) => message.channel.send(
`Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`
))
.on("addSong", (message, queue, song) => message.channel.send(
`Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`
))
.on("playList", (message, queue, playlist, song) => message.channel.send(
`Play \`${playlist.name}\` playlist (${playlist.songs.length} songs).\nRequested by: ${song.user}\nNow playing \`${song.name}\` - \`${song.formattedDuration}\`\n${status(queue)}`
))
.on("addList", (message, queue, playlist) => message.channel.send(
`Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`
))
.on("searchResult", (message, result) => {
let i = 0;
message.channel.send(`**Choose an option from below**\n${result.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`);
})
.on("searchCancel", (message) => message.channel.send(`Searching canceled`))
.on("error", (message, e) => {
console.error(e)
message.channel.send("An error encountered: " + e);
});
client.login(config.token);
If you read the error
ReferenceError: connection is not defined
, it tells you what is wrong.connection
in the following code is not a variable:There is a
connection
property onnewVoiceState
though, although that isn't what you want in this scenario. You would want to use the.setSelfDeaf
method fromVoiceState
: