问题描述
const discord = require('discord.js');
const serverQueue = require('./play.js');
const ytdl = require('ytdl-core');
module.exports.run = async (bot,message,args) => {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel)
return message.channel.send(
'**You need to be in a Voice Channel to play music :no_entry:!**'
);
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT'))
return message.channel.send(
'**I have Insufficient permissions :no_entry:!**'
);
if (!permissions.has('SPEAK'))
return message.channel.send(
'**I have Insufficient permissions :no_entry:!**'
);
const songInfo = await ytdl.getInfo(args[0]);
const song = {
title: songInfo.title,url: songInfo.video_url,};
if (!serverQueue) {
const queueConstruct = {
textChannel: message.channel,voiceChannel: voice.channel,connection: null,songs: [],volume: 5,playing: true,};
queue.set(message.guild.id,queueConstruct);
queueConstruct.songs.push(song);
try {
var connection = await voiceChannel.join();
queueConstruct.connection = connection;
play(message.guild,queueConstruct.songs[0]);
} catch (error) {
console.log(`There was an error connecting to the Voice Channel: ${error}`);
queue.delete(message.guild.id);
return message.channel.send(
`There was an error connecting to the Voice Channel: ${error}`
);
}
} else {
serverQueue.songs.push(song);
return message.channel.send(
`**${song.title} has been added to the queue :white_check_mark:!**`
);
}
return undefined;
function play(guild,song) {
const serverQueue = queue.get(guild.id);
if (!song) {
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
return;
}
const dispatcher = serverQueue.connection
.play(ytdl(song.url))
.on('finish',() => {
serverQueue.songs.shift();
play(guild,serverQueue.songs[0]);
})
.on('error',(error) => {
console.log(error);
});
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
}
};
TypeError:无法读取未定义的属性“ push”
请忽略以下内容:棕色狐狸越过懒狗,棕色狐狸越过懒狗,棕色狐狸越过懒狗,棕色狐狸越过懒狗,棕色狐狸跳过了那只懒狗。
解决方法
您遇到的错误意味着您要推送到的数组未定义或不存在。
尝试使用push语句之前的 console.log(queue construct)
检查queueConstruct中的数据是否出现在控制台上。
如果显示未定义,请尝试将const更改为var。