Files
discord-music-bot/src/commands/join.js

75 lines
3.7 KiB
JavaScript

const { SlashCommandBuilder, PermissionFlagsBits, ChannelType } = require('discord.js');
const logger = require('../utils/logger');
module.exports = {
data: new SlashCommandBuilder()
.setName('join')
.setDescription('Joins your current voice channel'),
async execute(interaction, client) { // Added client parameter
await interaction.deferReply({ ephemeral: true }); // Defer reply as joining might take time
const member = interaction.member;
const voiceChannel = member?.voice?.channel;
// 1. Check if user is in a voice channel
if (!voiceChannel) {
return interaction.editReply('You need to be in a voice channel to use this command!');
}
// 2. Check bot permissions
const permissions = voiceChannel.permissionsFor(client.user);
if (!permissions.has(PermissionFlagsBits.Connect)) {
return interaction.editReply('I need permission to **connect** to your voice channel!');
}
if (!permissions.has(PermissionFlagsBits.Speak)) {
return interaction.editReply('I need permission to **speak** in your voice channel!');
}
// Ensure it's a voice channel (not stage, etc.) although erela might handle this
if (voiceChannel.type !== ChannelType.GuildVoice) {
return interaction.editReply('I can only join standard voice channels.');
}
// 3. Create or get the player and connect
let player = client.manager.get(interaction.guildId);
if (!player) {
try {
player = client.manager.create({
guild: interaction.guildId,
voiceChannel: voiceChannel.id,
textChannel: interaction.channelId, // Store the channel where command was used
selfDeafen: true, // Automatically deafen the bot
// selfMute: false, // Bot starts unmuted
});
player.connect();
logger.info(`Created player and connected to voice channel ${voiceChannel.name} (${voiceChannel.id}) in guild ${interaction.guild.name} (${interaction.guildId})`);
await interaction.editReply(`Joined ${voiceChannel.name}! Ready to play music.`);
} catch (error) {
logger.error(`Failed to create/connect player for guild ${interaction.guildId}: ${error.message}`, error);
// Try to destroy player if partially created
if (player) player.destroy();
return interaction.editReply('An error occurred while trying to join the voice channel.');
}
} else {
// If player exists but is not connected or in a different channel
if (player.voiceChannel !== voiceChannel.id) {
player.setVoiceChannel(voiceChannel.id);
if (!player.playing && !player.paused && !player.queue.size) {
player.connect(); // Connect if not already playing/paused/queued
}
logger.info(`Moved player to voice channel ${voiceChannel.name} (${voiceChannel.id}) in guild ${interaction.guildId}`);
await interaction.editReply(`Moved to ${voiceChannel.name}!`);
} else {
// Already in the correct channel
await interaction.editReply(`I'm already in ${voiceChannel.name}!`);
}
// Update text channel if needed
if (player.textChannel !== interaction.channelId) {
player.setTextChannel(interaction.channelId);
logger.debug(`Updated player text channel to ${interaction.channel.name} (${interaction.channelId}) in guild ${interaction.guildId}`);
}
}
},
};