【问题标题】:how to create a music bot如何创建音乐机器人
【发布时间】:2021-06-29 03:08:15
【问题描述】:

我正在尝试使用 Javascript 构建一个音乐机器人,但我卡住了,当我使用命令播放音乐时,机器人加入了语音频道,但它没有开始播放歌曲,而且我知道我的代码有一些奇怪的部分 但我不知道如何更新它,我也确实在“discord.js ffmpeg fluent-ffmpeg @discordjs/opus ytdl-core”这一行中安装了运行音乐所需的软件包 请问我可以得到一些帮助吗 我使用的代码:

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("Ready!");
});

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 {
    message.channel.send("You need to enter 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(
      "You need to be in a voice channel to play music!"
    );
  const permissions = voiceChannel.permissionsFor(message.client.user);
  if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
    return message.channel.send(
      "I need the permissions to join and speak in your voice channel!"
    );
  }

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

  if (!serverQueue) {
    const queueContruct = {
      textChannel: message.channel,
      voiceChannel: voiceChannel,
      connection: null,
      songs: [],
      volume: 5,
      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 stop the music!"
    );
  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!"
    );
    
  if (!serverQueue)
    return message.channel.send("There is no song that I could stop!");
    
  serverQueue.songs = [];
  serverQueue.connection.dispatcher.end();
}

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(`Start playing: **${song.title}**`);
}

client.login(token);

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    我可能会和你启动 discord-bot 完全相同的代码。

    我将描述我的情况。

    1. 我的参数有空格
      我的前缀是 hey! (包括空格),我想播放像 hey! play [music] 这样的音乐。
      在这种情况下,args[1] 是“播放”,所以它什么都不播放。

    2. 不是 youtube 链接。
      我不确定 ytdl,我只是复制并粘贴它。
      当我尝试!play popsong 时,它不会出现一些错误。
      这是因为,args[1] (await ytdl.getInfo(args[1]);) 必须是包含 https:// 的 youtube 链接。

    所以,我更改了execute 函数中的一些代码。

    async function execute(message, serverQueue) {
      const args = message.content.split(' ');
      var another = args.slice(1, args.length).join(' ');
    
      if(another.includes('youtu') && !another.startsWith('https://')) {
        another = "https://"+another;
      }
    
      if(another.includes('youtu')) {
        // play here
      }
      else {
        // search youtube data api
      }
    }
    
    

    只是为了测试,检查上面的2个案例。

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 2020-11-01
      • 2022-01-11
      • 2020-09-22
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 2019-08-04
      相关资源
      最近更新 更多