因此,我试图通过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;
对此我有点陌生,所以谢谢您的帮助。
答案 0 :(得分:0)
该问题发生在var server = server[message.guild.id]
,表示它无法读取524399562249601026
的属性undefined
。因此,在这种情况下server
是未定义的。
查看更多代码片段,我认为您输入的是错字,应该是servers[...]
而不是server[...]
。