Discord.js-ytdl机器人不会播放音乐

时间:2019-07-16 18:36:55

标签: javascript node.js discord discord.js

我将ytdl的代码放入另一个.js文件中,现在似乎不再起作用了。我收到控制台日志“有效URL”,但该漫游器只是无法加入。关于机器人为何不加入的任何想法?复制和粘贴时我是否弄乱了代码?因此,我就像一个绝对的初学者,而且我自己很难发现错误,请对我轻松一点

这是代码:

const Discord = require("discord.js");
const botconfig = require("../botconfig.json");
const colors = require("../colors.json")
const ytdl = require('ytdl-core');
const streamOptions = {
  seek: 0,
  volume: 1
}

const bot = new Discord.Client({
  disableEveryone: true
});

var musicUrls = [];

module.exports.run = async(bot, message, args) => {
  bot.on("message", async message => {
    if (message.author.bot)
      return;

    if (message.content.toLowerCase().startsWith("?play")) {
      let args = message.content.split(" ");
      let url = args[1];
      let voiceChannel = message.guild.channels.find(channel => channel.name === 'Musik Bot');

      if (ytdl.validateURL(url)) {
        console.log("Valid URL");
        var flag = musicUrls.some(element => element === url);
        if (!flag) {
          musicUrls.push(url);
          if (voiceChannel != null) {

            if (voiceChannel.connection) {
              console.log("Connection exists");
              const embed = new Discord.RichEmbed();
              embed.setAuthor(bot.user.username, bot.user.displayAvatarURL);
              embed.setDescription("Du hast erfolgreich einen Track zu der Playlist hinzugefügt!");
              message.channel.send(embed);
            } else {
              try {
                const voiceConnection = await voiceChannel.join();
                await playSong(message.channel, voiceConnection, voiceChannel);
              } catch (ex) {
                console.log(ex);
              }
            }
          }
        }
      }
    }

    async function playSong(messageChannel, voiceConnection, voiceChannel) {
      const stream = ytdl(musicUrls[0], {
        filter: 'audioonly'
      });
      const dispatcher = voiceConnection.playStream(stream, streamOptions);

      dispatcher.on('end', () => {
        musicUrls.shift();

        if (musicUrls.length == 0)
          voiceChannel.leave();
        else {
          setTimeout(() => {
            playSong(messageChannel, voiceConnection, voiceChannel);
          }, 1000);
        }
      });
    }
  });
}

module.exports.config = {
  name: "play",
  aliases: []
}

0 个答案:

没有答案
相关问题