【问题标题】:I'm using ytdl-core for a music bot, and it keeps outputting that play is not defined我正在将 ytdl-core 用于音乐机器人,它一直输出未定义播放
【发布时间】:2019-08-25 17:40:25
【问题描述】:

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

根据视频中的 cmets,我尝试将 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);

我希望仍然能够按照教程来完全集成排队。

【问题讨论】:

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


    【解决方案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
                    };
    

    可能是您必须更改一些代码行,以便它适用于您的机器人!

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 2021-05-05
      • 2021-10-25
      • 1970-01-01
      • 2020-04-14
      • 2021-05-28
      • 2020-09-03
      • 2020-03-16
      • 2022-01-21
      相关资源
      最近更新 更多