#javascript #discord #discord.js
#javascript #Discord #discord.js
Вопрос:
я пытаюсь создать команду leave для моего бота, и я пытаюсь сделать так, чтобы, если человек, использующий команду, не находится на том же голосовом канале, что и бот, он ответит и скажет им, что они должны быть на том же голосовом канале, и если они есть, он уйдет
пока у меня это есть, но я не могу понять, как получить только идентификатор канала, в котором находятся бот и пользователь, чтобы сравнить их
let userVoiceChannel = message.member.voice.connection;
let crashbotVoiceChannel = Crashbot.voice.connections;
верните всю информацию о каналах, в которых находятся бот и пользователь, я просто не могу понять, как выбрать только идентификатор, используя это для сравнения
Crashbot.on('message', async message =>{
//creates an array called args and removes the first amount of charactors equal to the length of the prefix variable
let args = message.content.substring(PREFIX.length).split(" ");
let userVoiceChannel = message.member.voice.connection;
let crashbotVoiceChannel = Crashbot.voice.connections;
console.log(userVoiceChannel[0])
console.log(crashbotVoiceChannel)
//the switch equals true if the first word after the prefix is "leave"
switch (args[0]) {
case 'leave':
if (userVoiceChannel === null) return; message.reply("You must be a voice channel to use this command");
//if the message author is not in the same voice channel as the bot the bot replies and tells them that that have to be in the same channel to use that command
if (userVoiceChannel !== crashbotVoiceChannel) {
message.reply("You must be in the same channel as me to use this command");
return;
}
//if the message author is in the same voice channel as the bot it leaves the channel it is in
else if (userVoiceChannel === crashbotVoiceChannel) {
const connection = await message.member.voice.channel.leave();
message.channel.send("Successfully left the voice channel");
}
break;
}
});
Ответ №1:
Client.voice.connections
возвращает a Collection
со всеми голосовыми соединениями, которые есть у вашего бота во всех гильдиях.
Вам нужно будет получить соединение для текущей гильдии. Вы можете использовать:
message.guild.me.voice
// Getting the author's voice connection.
const userVoiceChannel = message.member.voice;
// Getting the bot's voice connection.
const botVoiceChannel = message.guild.me.voice;
// Checking if the bot is in a voice channel.
if (!botVoiceChannel.channel) return message.reply("The bot is not in a voice channel.");
// Checking if the member is in a voice channel.
if (!userVoiceChannel.channel) return message.reply("You need to be in a voice channel.");
// Checking if the member is in the same voice channel as the bot.
if (botVoiceChannel.channelID !== userVoiceChannel.channelID) return message.reply("You need to be in the same channel as the bot.");
message.reply("Everything works.");