尝试为 Discord bot 编写音乐播放器时出错

Posted

技术标签:

【中文标题】尝试为 Discord bot 编写音乐播放器时出错【英文标题】:Error when trying to code a music player for Discord bot 【发布时间】:2019-05-20 10:09:05 【问题描述】:

所以我试图在 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;

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

【问题讨论】:

【参考方案1】:

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

查看您代码的更多 sn-ps,我认为您打错字了,应该是 servers[...] 而不是 server[...]

【讨论】:

以上是关于尝试为 Discord bot 编写音乐播放器时出错的主要内容,如果未能解决你的问题,请参考以下文章

Discord.py Bot 不播放音乐

Discord Bot JS:编辑来自先前斜杠命令交互的回复

Discord bot:如何在语音频道中播放 mp3 文件

discord.py 音乐机器人:如何结合播放和队列命令

从 twitch bot 调用 Discord.js 音乐问题找到语音频道

Discord bot python:discord.errors.ClientException:找不到ffmpeg