机器人不会检测用户是否在语音通道中! ||不和谐.JS V.14



这是我的代码:

const { SlashCommandBuilder } = require('@discordjs/builders');
const { createAudioPlayer, createAudioResource, joinVoiceChannel } = require('@discordjs/voice');
const ytdl = require('ytdl-core');
const { EmbedBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('play')
.setDescription('Play a song in your current voice channel')
.addStringOption(option =>
option.setName('song')
.setDescription('Enter the YouTube URL or keywords to search for')
.setRequired(true)),
async execute(interaction) {
const song = interaction.options.getString('song');
// Check if the user is in a voice channel
if (!interaction.member.voice.channel) {
return interaction.reply({ content: 'You are not in a voice channel!', ephemeral: true });
}
// Join the user's voice channel
const connection = joinVoiceChannel({
channelId: interaction.member.voice.channel.id,
guildId: interaction.guildId,
adapterCreator: interaction.guild.voiceAdapterCreator
});
// Get video info and create an audio resource
const videoInfo = await ytdl.getInfo(song);
const stream = ytdl(song, { filter: 'audioonly' });
const resource = createAudioResource(stream);
// Create an audio player and play the resource
const player = createAudioPlayer();
player.play(resource);
// Subscribe the connection to the player
connection.subscribe(player);
// Send a confirmation message
const embed = new EmbedBuilder()
.setColor("Green")
.setTitle('Now Playing')
.setDescription(`[${videoInfo.videoDetails.title}](${song})`)
.setThumbnail(videoInfo.videoDetails.thumbnails[0].url);
interaction.reply({ embeds: [embed] });
},
};

play .js -这是一个让用户在他们所在的语音通道中播放音乐的命令。用户必须在语音通道上,运行命令,然后bot将连接到与用户相同的通道。问题是,当我在语音通道上时,它也发送'You are not in a voice channel!'并且不进行下一步!我相信问题出在这里:

if (!interaction.member.voice.channel) {
return interaction.reply({ content: 'You are not in a voice channel!', ephemeral: true });
}

谁能帮我解决这个问题(如果你发现任何其他问题,也请让我知道。)提前感谢!我有以下意图:[Guilds, GuildMembers, GuildMessages, MessageContent, DirectMessages].

注意:

这篇文章或这篇文章不能解决我的问题,而且它不是重复的。

您还需要添加GuildVoiceStates意图来查看语音状态的变化。如果没有,interaction.member.voice将始终返回一个"快照";启动机器人时的语音状态

如果没有GuildVoiceStates,则不会检测到意图变化;如果成员已经连接,当你启动你的机器人,interaction.member.voice将返回正确的语音状态,但如果他们断开连接,你再次检查它,值是相同的,所以似乎他们仍然在那个语音通道。

相关内容

  • 没有找到相关文章

最新更新