【问题标题】:Running into an error with getting music bot to join VC让音乐机器人加入 VC 时遇到错误
【发布时间】:2021-11-03 22:47:04
【问题描述】:
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');

module.exports = {
name: 'play',
description: "plays music in voice chat",
async execute(message, args, Discord){
    const voiceChannel = message.member.voice.channel;
  
    if (!voiceChannel) return message.channel.send('You need to be in a voice channel to listen to music!');
    const permissions = voiceChannel.permissionsFor(message.client.user);
    if (!permissions.has('CONNECT')) return message.reply("You don't have the correct permissions to request music!");
    if (!permissions.has('SPEAK')) return message.reply("You don't have the correct permissions to request music!");
    if (!args.length) return message.channel.send("You Can't Request for no Music Silly >_<");

  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})
      .on('finish', () =>{
          voiceChannel.leave();
      });



      await message.reply(`:Thumbsup: Now Playing >>>${video.title}<<<`)
    } else {
      message.reply('No Video Results Found :(');
    }
  }
}

我的错误信息是 const connection = await voiceChannel.join();

TypeError: voiceChannel.join 不是函数

如果您有任何可行的建议,请随时进行编辑。它也只能在机器人启动时检测是否有人在语音聊天中,因此如果您在机器人启动时进行语音聊天,它会在您进入和退出语音聊天时发送语音聊天消息

【问题讨论】:

  • 你使用的是什么版本的 Discord.js?我对不和谐机器人了解不多,但从他们的大多数示例来看,他们似乎已经切换到 TS 并且还将“语音”抽象到自己的包中。如果您使用的是较新版本,我建议您学习一点 TS 并使用它。它会在编译之前发现错误 :) 话虽如此,看起来“语音”包没有“加入”方法,而是使用“订阅”对象。见下文:github.com/discordjs/voice/blob/main/examples/music-bot/src/…
  • 以上假设您使用的是此软件包的较新版本之一,因为 OP 中没有另外提及。
  • 我使用的是最新版本的 discord.js,不知道如何查看我通过 npm 下载的发行版本号
  • 这样的话,我也遇到过TS版本。我建议不要尽可能多地使用 JS,并尽可能切换到使用 TS。这些类型的错误会在编译之前被提前发现。我在上面分享的 repo 中有一些示例,以及 google 上的大量 TS 资源,希望对您有所帮助。绝对欢迎您继续使用纯 JS,但恐怕您以后可能会遇到许多类似的问题。
  • @NaturalTwitch 请给这个问题一个更具体的标题,以便在以后的搜索中更容易找到它。

标签: javascript discord.js


【解决方案1】:

在从 discord.js v12 - v13 过渡时,我们遇到了一个重大变化,即切换到 @discordjs/voice 一个单独的语音库,用于使用 discord.js 播放,现在正如 comment 建议的那样,我在这里看到你'重新使用最新版本的 discord.js,其中 VoiceChannel#join 方法不再存在。您必须提出以下更改:

  • 使用npm install @discordjs/voice 安装@discordjs/voice
  • 你需要建立一个voiceConnection,并进一步创建一个AudioPlayer实例并继续调用AudioPlayer#subscribe对于这个播放器的方法,你必须为它做一些基础添加,如下所示:
const {
    joinVoiceChannel,
    createAudioPlayer,
    createAudioResource
} = require('@discordjs/voice');

const connection = joinVoiceChannel({
    channelId: message.member.voice.channel.id,
    guildId: message.guild.id,
    adapterCreator: message.guild.voiceAdapterCreator,
});

const stream = ytdl(video.url, {
    filter: "audioonly"
});

const player = createAudioPlayer();
const resource = createAudioResource(stream);

async function play() {
    await player.play(resource);
    connection.subscribe(player);
}

他们只是让初学者理解起来过于复杂,所以不用担心!如果您觉得自己太累了,您可以随时降级到较低版本,如下所示:

npm uninstall discord.js
npm install discord.js@12.5.3

欢迎来到 stackoverflow ?

【讨论】:

    猜你喜欢
    • 2019-05-24
    • 2018-07-24
    • 2020-08-31
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 2021-06-16
    • 2021-05-05
    • 2020-12-01
    相关资源
    最近更新 更多