0

所以我试图通过 Visual Studio Code 中的 discord.js-commando 编写一个 Discord 机器人。我正在尝试对音乐播放器进行编码,并且我能够让机器人加入语音频道。但是,当输入我希望它播放的 URL 时,终端给了我这个错误:

(node:17316) DeprecationWarning: Collection#filterArray: use 
Collection#filter instead
(node:17316) UnhandledPromiseRejectionWarning: TypeError: Cannot read 
property '524399562249601026' of undefined
at Play 
(C:\Users\Vincent\Documents\AstralBot\commands\music\join_channel.js:6:24)
at message.member.voiceChannel.join.then.connection 
(C:\Users\Vincent\Documents\AstralBot\commands\music\join_channel.js:48:25)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:17316) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 
This error originated either by throwing inside of an async function without 
a catch block, or by rejecting a promise which was not handled with 
.catch(). (rejection id: 1)
(node:17316) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.

这是代码:

const commando = require('discord.js-commando');
const YTDL = require('ytdl-core');

function Play(connection, message)
{
    var server = server[message.guild.id]
    server.dispatcher = connection.playStream(YTDL(server.queue[0], {filter: "audioonly"}));
    server.queue.shift();
    server.dispatcher.on("end", function(){
        if(server.queue[0])
        {
            Play(connection, message);
        }
        else
        {
            connection.disconnect();
        }
    });
}

class JoinChannelCommand extends commando.Command
{
    constructor(client)
    {
        super(client,{
            name: 'play',
            group: 'music',
            memberName: 'play',
            description: 'Plays whatever you link from YouTube.'
        });
    }

    async run(message, args)
    {
        if(message.member.voiceChannel)
        {
            if(!message.guild.voiceConnection)
            {
                if(!servers[message.guild.id])
                {
                    servers[message.guild.id] = {queue: []}
                }
                message.member.voiceChannel.join()
                    .then(connection =>{
                        var server = servers[message.guild.id];
                        message.reply("Successfully joined!")
                        server.queue.push(args);
                        Play(connection, message);
                    })
            }
            else
            {
                message.reply("You must first join a voice channel before inputting the command.")
            }
        }
    }
}

module.exports = JoinChannelCommand;

我对此有点新,所以任何帮助将不胜感激,谢谢。

4

1 回答 1

0

问题发生在var server = server[message.guild.id]说它无法读取524399562249601026. undefined所以在这种情况下server是未定义的。

查看更多代码片段,我认为您犯了一个错字,因为它应该是servers[...]而不是server[...].

于 2018-12-20T07:54:33.727 回答