【问题标题】:"message.member.voice" returning undefined even when I'm in a voice channel - discord.js即使我在语音频道中,“message.member.voice”也会返回未定义 - discord.js
【发布时间】:2021-11-18 19:03:48
【问题描述】:

我正在尝试让我的 discord 机器人为我的 discord 服务器播放音乐(鉴于正常关闭)。

但是当我尝试使用message.members.voice.join() 让它加入时,它总是返回未定义,即使我在不​​和谐的语音频道中也是如此。我已离开并重新加入通话,并重新启动了机器人,但没有任何效果。

const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');

module.exports = {
  name: 'play',
  description: 'Joins and plays a video from youtube',
  async execute(message, args) {
    const { voiceChannel } = message.member.voice;


    if (!voiceChannel) return message.channel.send('Join a Voice-Channel to use this command');
    const permissions = voiceChannel.permissionsFor(message.client.user);
    if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins');
    if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins');
    if (!args.length) return message.channel.send('You need to send the second argument!');

    const connection = await voiceChannel.join();


    const videoFinder = async (query) => {
      const videoResult = await ytSearch(query);



      return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;

    }


    const video = await videoFinder(args.join(' '));

    if (video) {
      const stream = ytdl(video.url, { filter: 'audioonly' })
      connection.play(stream, { seek: 0, volume: 1 });


    }
  }
}

【问题讨论】:

  • 您使用的是 v13 吗?
  • 你的 discord.js 版本是多少?

标签: discord.js


【解决方案1】:

在 discord.js 版本 12 中,您需要获取它们所连接的 GuildMember#voice 状态的 VoiceChannel 对象,因此您必须使用 VoiceState#channel,因此您可以像这样定义您的连接:

const connection = await message.member.voice.channel.join()

【讨论】:

    【解决方案2】:

    discord.js v13 需要使用@discordjs/voice 包。

    const { joinVoiceChannel } = require('@discordjs/voice')
    

    并使用joinVoiceChannel 方法加入语音或舞台频道

    let connection = joinVoiceChannel({
        ...
    })
    

    您需要指定their documentation中所示的选项

    虽然我不确定是否播放音频,但有一个方法joinVoiceChannel.subscribe()AudioPlayer 为参数,它允许播放器在连接上播放音频。

    您可以使用createAudioPlayer() 创建AudioPlayer。但是AudioPlayer中有一个方法play,它以AudioResource为参数,在播放器中播放资源。

    要制作AudioResource,请使用createAudioResource方法,该方法以input为参数。

    const {joinVoiceChannel, createAudioPlayer, createAudioResource} = require('@discordjs/voice')
    let player = createAudioPlayer()
    connection.subscribe(player)
    
    let resource = createAudioResource('')
    player.play(resource)
    

    虽然我仍然不确定参数的值,它可能是 URL 或文件路径,我没有测试它,因为我目前不在

    【讨论】:

    • 我试试看。谢谢你:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 2021-09-21
    • 2020-12-13
    • 2020-10-24
    • 2019-11-12
    相关资源
    最近更新 更多