Discord.js在前缀和命令之间添加空格

时间:2020-09-30 20:44:56

标签: javascript node.js discord.js

我创建了一个音乐漫游器,当有人点击!play文本频道时,该流媒体就会播放音乐

现在,我想用!play切换please play,但是当我尝试更改代码时,它只响应pleaseplay,它们之间没有空格:

const prefix = 'please'; //before
const prefix = 'please '; //after

但它根本无法正常工作

日志(节点:5296)未处理的PromiseRejection警告:错误:未找到视频ID:

const {Client, Attachment, Message} = require('discord.js');
const {token} = require("./config.json");
const bot = new Client();
const prefix = 'please ';


const ytdl = require("ytdl-core");
const request = require('request');
const cheerio = require('cheerio');

const queue = new Map();



bot.on('ready', () => {
    console.log('Client is online!');


bot.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.title,
      url: songInfo.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!"
      );
    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(`streaming: **${song.title}**`);
  }

bot.login(token);

2 个答案:

答案 0 :(得分:2)

问题是您的参数是根据空格字符分割的。

const args = message.content.split(" ");
引用

args[1]以获得命令名称后的文本,但是在命令中带有空格的情况下,args[1]将始终是“ play”(或他们使用的任何命令)

一种快速解决方案是将args[1]更改为args[2]

const songInfo = await ytdl.getInfo(args[2]);

答案 1 :(得分:1)

编辑:完整的升级代码...也像水晶一样清晰。

const {Client, Attachment, Message} = require('discord.js');
const {token} = require("./config.json");
const bot = new Client();
const prefix = 'please';

const ytdl = require("ytdl-core");
const request = require('request');
const cheerio = require('cheerio');

const queue = new Map();


bot.on('ready', () => {
  console.log('Client is online!');
}

bot.on("message", async message => {
  let content = message.content.split(' ');

  if (message.author.bot) return;

  if (content.shift() !== prefix) return;

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

  switch (content.shift()) {
    case 'play': exec(content, message, serverQueue); break;
    case 'skip': skip(content, message, serverQueue); break;
    case 'stop': stop(content, message, serverQueue); break;
    default: message.channel.send("You need to enter a valid command!");
  }
});


async function exec (ctx, msg, que) {
  const voiceChannel = msg.member.voice.channel;
  if (!voiceChannel)
    return msg.channel.send(
      "You need to be in a voice channel to play music!"
    );

  const permissions = voiceChannel.permissionsFor(msg.client.user);
  if (!permissions.has("CONNECT") || !permissions.has("SPEAK"))
    return msg.channel.send(
      "I need the permissions to join and speak in your voice channel!"
    );
  
  const songInfo = await ytdl.getInfo(ctx.join(' '));

  const song = {
    title: songInfo.title,
    url: songInfo.video_url
  };

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

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

    queueContruct.songs.push(song);

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

await function play(gui, sng) {
  const serverQueue = queue.get(gui.id);
  if (!sng) {
    serverQueue.voiceChannel.leave();
    queue.delete(gui.id);
    return;
  }

  const dispatcher = serverQueue.connection
    .play(ytdl(sng.url))
    .on("finish", () => {
      serverQueue.songs.shift();
      play(gui, serverQueue.songs[0]);
    })
    .on("error", error => console.error(error));
  dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  serverQueue.textChannel.send(`streaming: **${sng.title}**`);
}

async function stop(ctx, msg, que) {
  if (!msg.member.voice.channel)
    return msg.channel.send(
      "You have to be in a voice channel to stop the music!"
    );

  que.songs = [];
  que.connection.dispatcher.end();
}

async function skip(ctx, msg, que) {
  if (!msg.member.voice.channel)
    return msg.channel.send(
      "You have to be in a voice channel to stop the music!"
    );

  if (!que)
    return msg.channel.send("There is no song that I could skip!");

  que.connection.dispatcher.end();
}


bot.login(token);