Exit channel without specifying it

This commit is contained in:
radiantly 2020-10-27 00:33:22 +05:30
parent 210496a9fb
commit 6f122f5060
No known key found for this signature in database
GPG key ID: 6B113D80D68C409C
2 changed files with 20 additions and 16 deletions

View file

@ -31,19 +31,23 @@ exports.enter = function(msg, channelName) {
.catch(err => { throw err; });
}
exports.exit = function(msg, channelName) {
const voiceChannel = msg.guild.channels.cache.find(channel => channel.name === channelName);
exports.exit = function (msg) {
// Use optional chaining when we upgrade to Node 14.
if (
!(
msg &&
msg.guild &&
msg.guild.voice &&
msg.guild.voice.channel &&
msg.guild.voice.connection
)
)
return;
if (!voiceChannel || voiceChannel.type !== 'voice')
return msg.reply(`The channel #${channelName} doesn't exist or isn't a voice channel.`);
voiceChannel.join()
.then(conn => {
const dispatcher = conn.play(__dirname + '/../sounds/badumtss.mp3', { volume: 0.45 });
dispatcher.on('finish', () => {
const { channel: voiceChannel, connection: conn } = msg.guild.voice;
const dispatcher = conn.play(__dirname + "/../sounds/badumtss.mp3", { volume: 0.45 });
dispatcher.on("finish", () => {
voiceChannel.leave();
console.log(`\nSTOPPED RECORDING\n`);
});
});
}
};

View file

@ -10,7 +10,7 @@ client.on('message', msg => {
const channelName = commandBody[1];
if (commandBody[0] === ('enter') && commandBody[1]) commands.enter(msg, channelName);
if (commandBody[0] === ('exit') && commandBody[1]) commands.exit(msg, channelName);
if (commandBody[0] === ('exit')) commands.exit(msg);
}
});