join command discrd.js
// join a voice channel when use command (ex. !join)
// I write this code in discord.js v13 (13.0.1 and 13.0.2 can use it).
// node module install
// npm i @discordjs/voice
const {
Client,
Message,
MessageEmbed
} = require('discord.js');
const { joinVoiceChannel, getVoiceConnection } = require('@discordjs/voice');
module.exports = {
name: 'join',
aliases: ['j'],
/**
* @param {Client} client
* @param {Message} message
* @param {String[]} margs
*/
run: async (client, message, margs) => {
const { channel } = message.member.voice;
const connected = getVoiceConnection(message.guild.id);
if (!message.member.voice.channel) return; //if member call bot but that member isn't still in any voice channel. (do something)
if (connected) return; //if bot still in any voice channel in that guild. (do something)
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
}
}