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

0x6upsns  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(116)

这是我的代码:

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 -这是一个命令,让用户在他们所在的语音通道中播放音乐。用户必须在语音通道上,运行命令,然后机器人将连接到与用户相同的通道。问题是,当我在语音通道上时,它也发送'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]

注意:

This articlethis不能解决我的问题,它不是重复的。

omhiaaxx

omhiaaxx1#

您还需要添加GuildVoiceStates Intent来查看语音状态的更改。如果没有此Intent,interaction.member.voice将始终返回启动机器人时语音状态的“快照”。
如果没有GuildVoiceStates,则不会检测到意图更改;如果成员在你启动机器人时已经连接,interaction.member.voice将正确返回语音状态,但如果他们断开连接,你再次检查,值是相同的,所以似乎他们仍然在该语音通道中。

相关问题