首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError: connection.play不是一个函数

TypeError: connection.play不是一个函数
EN

Stack Overflow用户
提问于 2022-03-28 18:30:52
回答 1查看 359关注 0票数 1

我有一个音乐机器人项目,我从YouTube那里得到了它,我给它添加了一些我自己的东西。

问题是每次我发送?play (songname)命令时,它都会发送一个错误给connection.play is not a function。我该怎么办?

我所犯的错误:

代码语言:javascript
运行
复制
connection.play(stream, {seek: 0, volume: 1})
           ^

TypeError: connection.play is not a function
    at Object.execute (C:\Users\Pauli.Salminen\Desktop\DiscordBotPRojects\reactionroles\commands\play.js:41:24)       
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

这是我的play.js

代码语言:javascript
运行
复制
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
const { joinVoiceChannel } = require('@discordjs/voice');

module.exports = {
  name: 'play',
  description: 'joins aadn plays muusic',
  async execute(kaoru, message, args) {
    const voiceChannel = message.member.voice.channel;

    if (!voiceChannel)
      return message.channel.send('> **You need to join voicechannel first!**');
    const permissions = voiceChannel.permissionsFor(message.client.user);
    if (!permissions.has('CONNECT'))
      return message.channel.send('> **You dont have right permissions!**');
    if (!permissions.has('SPEAK'))
      return message.channel.send('> **You dont have right permissions!**');
    if (!args.length)
      return message.channel.send('> **You need to insert name of the song!**');

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

    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.channel.send('No video results found');
    }
  }
};
EN

Stack Overflow用户

回答已采纳

发布于 2022-03-28 21:14:04

discord.js v13中有一些更改,您必须使用@discordjs/voice模块。

您的第一个错误是您没有在channelId: message.member.voice.channel提供ID。通道ID应该是message.member.voice.channel.id

第二,play()connection上不再可用。在v13中,您必须首先使用createAudioPlayer()方法创建音频播放器,然后创建音频资源。音频资源包含音频,可由音频播放器播放到语音连接。要创建一个参数,您可以使用createAudioResource()方法并传递您的stream作为参数。

一旦资源被创建,您可以使用player.play()在音频播放器上播放它们。您还需要将您的connection订阅到player,以便连接将广播您的player正在播放的任何内容。为此,使用播放机作为参数,在您的语音subscribe()上调用connection方法。

此外,也没有connection.on侦听器。不过,您可以使用player.on。要检查歌曲是否已经完成,您可以订阅AudioPlayerStatus.Idle事件。

最后一件事,离开一个通道,而不是voiceChannel.leave(),您应该使用connection.disconnect()connection.destroy()

您可以在下面找到工作代码:

代码语言:javascript
运行
复制
const {
  AudioPlayerStatus,
  createAudioPlayer,
  createAudioResource,
  joinVoiceChannel,
} = require('@discordjs/voice');
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');

module.exports = {
  name: 'play',
  description: 'joins aadn plays muusic',
  async execute(kaoru, message, args) {
    const voiceChannel = message.member.voice.channel;

    if (!voiceChannel)
      return message.channel.send('> **You need to join voicechannel first!**');
    const permissions = voiceChannel.permissionsFor(message.client.user);
    if (!permissions.has('CONNECT'))
      return message.channel.send('> **You dont have right permissions!**');
    if (!permissions.has('SPEAK'))
      return message.channel.send('> **You dont have right permissions!**');
    if (!args.length)
      return message.channel.send('> **You need to insert name of the song!**');

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

    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' });
      const player = createAudioPlayer();
      const resource = createAudioResource(stream);

      await player.play(resource);
      connection.subscribe(player);

      player.on('error', (error) => console.error(error));
      player.on(AudioPlayerStatus.Idle, () => {
        console.log(`song's finished`);
        connection.disconnect();
      });

      await message.reply(`:thumbsup: Now playing ***${video.title}***`);
    } else {
      message.channel.send('No video results found');
    }
  },
};

PS:确保启用了GUILD_VOICE_STATES意图。没有这一点,你的机器人将无法连接到一个语音频道。

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71652080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档