From 6f122f506018aa906fbb505ffdf3372c459b26af Mon Sep 17 00:00:00 2001 From: radiantly <44368997+radiantly@users.noreply.github.com> Date: Tue, 27 Oct 2020 00:33:22 +0530 Subject: [PATCH] Exit channel without specifying it --- bin/commands.js | 34 +++++++++++++++++++--------------- index.js | 2 +- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/bin/commands.js b/bin/commands.js index ba8c254..d702906 100644 --- a/bin/commands.js +++ b/bin/commands.js @@ -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', () => { - voiceChannel.leave(); - console.log(`\nSTOPPED RECORDING\n`); - }); - }); - -} \ No newline at end of file + 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`); + }); +}; \ No newline at end of file diff --git a/index.js b/index.js index 2afbe0b..5509a94 100644 --- a/index.js +++ b/index.js @@ -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); } });