UnhandledPromiseRejectionWarning:TypeError:无法读取未定义Discord.JS的属性“ forEach”

时间:2020-04-08 08:30:23

标签: javascript node.js discord discord.js

我试图让我的歌曲机器人不和谐,让我们查看排队的歌曲列表,但我正在努力使for或forEach循环正常工作。我主要是想使循环现在能够正常工作,我将添加正确的内容来显示标题和显示的持续时间,但是如果我能得到一些帮助,以使此代码能够正常工作,那就太好了。

这是错误

(节点:42564)UnhandledPromiseRejectionWarning:TypeError:无法读取 未定义的属性“ forEach” 在songQueue(C:\ Users \ Purev \ Desktop \ Discord Bot \ index.js:163:23) 在客户处。 (C:\ Users \ Purev \ Desktop \ Discord Bot \ index.js:46:5) 在Client.emit(events.js:311:20) 在MessageCreateAction.handle(C:\ Users \ Purev \ Desktop \ Discord Bot \ node_modules \ discord.js \ src \ client \ actions \ MessageCreate.js:31:14) 在Object.module.exports [作为MESSAGE_CREATE](C:\ Users \ Purev \ Desktop \ Discord Bot \ node_modules \ discord.js \ src \ client \ websocket \ handlers \ MESSAGE_CREATE.js:4:32) 在WebSocketManager.handlePacket(C:\ Users \ Purev \ Desktop \ Discord Bot \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketManager.js:386:31) 在WebSocketShard.onPacket上(C:\ Users \ Purev \ Desktop \ Discord Bot \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketShard.js:435:22) 在WebSocketShard.onMessage(C:\ Users \ Purev \ Desktop \ Discord Bot \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketShard.js:293:10) 在WebSocket.onMessage(C:\ Users \ Purev \ Desktop \ Discord Bot \ node_modules \ ws \ lib \ event-target.js:120:16) 在WebSocket.emit(events.js:311:20)

这是所有代码:

    const Discord = require("discord.js");
    const { prefix, token } = require("./config.json");
    const ytdl = require("ytdl-core");

    const client = new Discord.Client();

    const queue = new Map();

    client.once("ready", () => {
      console.log("VeloBot is running...");
    });

    client.once("reconnecting", () => {
      console.log("Reconnecting!");
    });

    client.once("disconnect", () => {
      console.log("Disconnect!");
    });

    client.on("message", async message => {
      if (message.author.bot) return;
      if (!message.content.startsWith(prefix)) return;

      const serverQueue = queue.get(message.guild.id);

      if (message.content.startsWith(`${prefix}play`)) {
        execute(message, serverQueue);
        return;
      } else if (message.content.startsWith(`${prefix}skip`)) {
        skip(message, serverQueue);
        return;
      } else if (message.content.startsWith(`${prefix}stop`)) {
        stop(message, serverQueue);
        return;
      } else if (message.content.startsWith(`${prefix}pause`)) {
        pause(message, serverQueue)
        return;
      } else if (message.content.startsWith(`${prefix}resume`)) {
        resume(message, serverQueue)
        return;
      } else if (message.content.startsWith(`${prefix}nowplaying`)) {
        nowPlaying(message, serverQueue)
        return;
      } else if (message.content.startsWith(`${prefix}queue`)) {
        songQueue(message, serverQueue)
        return;
      } else if (message.content.startsWith(`${prefix}restart`)) {
        restart(message, serverQueue)
        return;
      }
        else {
        message.channel.send("Hey stupid not a valid command!");
      }
    });

    async function execute(message, serverQueue) {
      const args = message.content.split(" ");

      const voiceChannel = message.member.voice.channel;
      if (!voiceChannel)
        return message.channel.send(
          "Hop in a channel to play music!"
        );
      const permissions = voiceChannel.permissionsFor(message.client.user);
      if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
        return message.channel.send(
          "Yo dawg I need permissions join and speak in your voice channel!"
        );
      }

      const songInfo = await ytdl.getInfo(args[1]);
      const song = {
        title: songInfo.title,
        url: songInfo.video_url
      };

      if (!serverQueue) {
        const queueContruct = {
          textChannel: message.channel,
          voiceChannel: voiceChannel,
          connection: null,
          songs: [],
          volume: 1,
          playing: true
        };

        queue.set(message.guild.id, queueContruct);

        queueContruct.songs.push(song);

        try {
          var connection = await voiceChannel.join();
          queueContruct.connection = connection;
          play(message.guild, queueContruct.songs[0]);
        } catch (err) {
          console.log(err);
          queue.delete(message.guild.id);
          return message.channel.send(err);
        }
      } else {
        serverQueue.songs.push(song);
        return message.channel.send(`${song.title} has been added to the queue!`);
      }
    }

    function skip(message, serverQueue) {
      if (!message.member.voice.channel)
        return message.channel.send(
          "You have to be in a voice channel to skip the song!"
        );
      if (!serverQueue)
        return message.channel.send("There is no song that I could skip!"
        );
      serverQueue.connection.dispatcher.end();
    }

    function stop(message, serverQueue) {
      if (!message.member.voice.channel)
        return message.channel.send(
          "You have to be in a voice channel to stop the music!"
        );
      serverQueue.songs = [];
      serverQueue.connection.dispatcher.end();
    }
    function pause(message, serverQueue) {
        if(!message.member.voice.channel)
        return message.channel.send(
            "You have to be in a voice channel to pause the song!"
        );
        if(!serverQueue)
        return message.channel.send("There is no song to pause!"
        );
        serverQueue.connection.dispatcher.pause();
    }
    function resume(message, serverQueue) {
        if(!message.member.voice.channel)
        return message.channel.send(
            "You have to be in a voice channel to resume the song!"
        );
        if(!serverQueue)
        return message.channel.send("There is no song to resumme!"
        );
        serverQueue.connection.dispatcher.resume();
    }
    function nowPlaying(message, serverQueue) {
        if(!serverQueue)
        return message.channel.send("There is no song that is playing!"
        );
        return message.channel.send(`Now playing: ${serverQueue.songs[0].title}`);

    }
    function songQueue(message, serverQueue) {
        if(!message.member.voice.channel)
        return message.channel.send(
            "You have to be in a channel to view the Queue!"
        );
        if(!serverQueue)
        return message.channel.send("There is no song playing!"
        );
        var queueOutput;
        var count = 1;
        serverQueue.array.forEach(entry =>{
          queueOutput = queueOutput + count + "." + entry + "\n";
          count++;
        });
        message.channel.send(queueOutput);
    }

    function restart(message, serverQueue) {
          message.channel.send("VeloBot restarting..."
          );
           serverQueue.voiceChannel.leave();
           process.exit();
    }

    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.error(error));
      dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
      serverQueue.textChannel.send(`Started playing: **${song.title}**`);
     }

    client.login(token);
    ```


1 个答案:

答案 0 :(得分:0)

songQueue.array.forEach(...)更改为songQueue.songs.forEach(...)。您已将其定义为songQueue.songs,但您一直在尝试使用songQueue.array