【问题标题】:Discordjs add space between prefix and commandDiscordjs 在前缀和命令之间添加空格
【发布时间】:2021-01-16 14:12:51
【问题描述】:

我创建了一个音乐机器人,当有人点击 !play 文本频道时播放音乐

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

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

但它根本不起作用并出现错误

日志(节点:5296)UnhandledPromiseRejectionWarning:错误:未找到视频 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);

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案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);

    【讨论】:

    • 我得到了 3x ';'预期的。 ts(1005)
    • 啊抱歉。现在更新。编辑:已修复。忘记加;以防万一。
    【解决方案2】:

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

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

    args[1] 被引用以获取命令名称后的文本,但由于命令中有空格,args[1] 将始终是“播放”(或他们使用的任何命令)

    快速解决方法是将args[1] 更改为args[2]

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      相关资源
      最近更新 更多