我正在将ytdl-core用作音乐bot,但它会一直输出未定义的播放

时间:2019-04-04 04:36:15

标签: javascript node.js bots discord discord.js

我正在使用ytdl-core和node-opus向我的机器人添加音乐功能。我也在关注一个教程。在我开始添加排队功能之前,该机器人运行良好。在整合队列时,该机器人仍可以加入并离开语音通道,但无法播放音乐。输出(node:22116) UnhandledPromiseRejectionWarning: ReferenceError: play is not defined

根据视频评论,我尝试将play切换为playstream。最初,此方法有效,但无济于事,仅输出未定义的内容。

以下是命令:

      if (!message.member.voiceChannel) return message.channel.send("You must be connected to a voice channel.");
      if (!args[0]) return message.channel.send("You must supply a __valid__ URL.");
      let validate = await ytdl.validateURL(args[0]);
      if (!validate) return message.channel.send("You must supply a __valid__ URL.");
      let info = await ytdl.getInfo(args[0]);
      let data = active.get(message.guild.id) || {};
      if (!data.connection) data.connection = await message.member.voiceChannel.join();
      if (!data.queue) data.queue = [];
      data.guildID = message.guild.id; 
      data.queue.push ({
        songTitle: info.title,
        requester: message.author.tag,
        url: args[0],
        announceChannel: message.channel.id
      });
      if (!data.dispatcher) play();
      else {
        message.channel.send(`Added song to queue: ${info.title} || Requested by: ${message.author.id}`);
        active.set(message.guild.id, data);

我希望仍然可以按照本教程来完全整合队列。

1 个答案:

答案 0 :(得分:1)

您没有在之前的代码中定义play()函数,因此也无法使用它。

这是您的play()函数外观的一个示例

const queue = msg.client.queue;
const ytdl = require('ytdl-core');

async function play(guild, song) {
    const serverQueue = await queue.get(guild.id);

    if (!song) {
        await serverQueue.voiceChannel.leave();
        await queue.delete(guild.id);
        return;
    }

    const stream = await ytdl(song.url, {
        filter: 'audioonly'
    });
    const dispatcher = await serverQueue.connection.playStream(stream)
        .on('end', async reason => {
            if (reason === 'Stream is not generating quickly enough.');
            serverQueue.songs.shift('Stream is not generating quickly enough');
            await play(guild, serverQueue.songs[0]);
        })
        .on('error', error => console.error(error));
    dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
}

我的队列的结构如下:

const queueConstruct = {
                    textChannel: msg.channel,
                    voiceChannel: voiceChannel,
                    connection: null,
                    songs: [],
                    volume: 2,
                    playing: true
                };

可能是因为您必须更改一些代码行才能使其适用于您的机器人!