繁体   English   中英

Discord.js 无法读取未定义的属性“推送”

[英]Discord.js Cannot read property 'push' of undefined

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);
 }
};

类型错误:无法读取未定义的属性“推送”

请忽略这一点:快速的棕狐跳过懒惰的狗,快速的棕色狐狸跳过懒惰的狗,快速的棕色狐狸跳过懒惰的狗,快速的棕色狐狸跳过懒惰的狗,快速的棕色狐狸跳过懒狗。

您遇到的错误意味着您要推送到的数组未定义或不存在。 尝试在 push 语句之前使用console.log(queue construct) queueconstruct) 检查 queueConstruct 中的数据是否显示在控制台上。

如果它显示未定义,请尝试将 const 更改为 var。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM