提问者:小点点

我如何发送参数[2]和它之后的所有参数?


我现在的代码让你使用这个命令,提到你想把它发送给的人,说出你想让消息发送多少次,然后是消息。但是,当我这样做时,它只会发送一个参数。如何使它发送args[2]及其后面的所有参数?

当前代码:

module.exports = {
    name: 'attack',
    description: 'attack',
    execute(message, args) {
      
      let recipient = message.mentions.users.first();
  
      if (!args[2])
        return message.channel.send(
          'Please include the message you want to send.',
        );
      
      if (isNaN(args[1]))
        return message.channel.send(
          'Please include how many times you want the message to send.',
        );
  
      if (message.author.id === 'My ID') {
        for (let i = 0; i < args[1]; i++) {
          recipient.send(args[2]);
        }
      }
      
      if (!message.author.id === 'My ID') {
        message.channel.send('You do not have the authority to use this command.')
      }
    },
};

共1个答案

匿名用户

您可以使用.slice()将数组中的前两个项剪掉,并使用.join()将其余项按空格连接起来。

您可以运行下面的代码段:

null

const args = ['remove 1', 'remove 2', 'this', 'is', 'the', 'message']
const message = args.slice(2).join(' ')

console.log(message)