首页 > 解决方案 > 如何发送 args[2] 和之后的所有 args?

问题描述

我当前的代码让您使用该命令,提及您要将其发送给的人,说出您希望发送多少次消息,然后是消息。但是,当我这样做时,它只会发送一个参数。我怎样才能让它发送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.')
      }
    },
};

标签: javascriptnode.jsdiscorddiscord.jsbots

解决方案


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

你可以运行下面的代码片段来看看它是如何工作的:

const args = ['<@!322655127249097071>', '15', 'this', 'is', 'the', 'message']
const message = args.slice(2).join(' ')

console.log(message)

如果要删除前 2 项,可以使用.slice(2)

if (message.author.id === 'My ID') {
  const messageToSend = args.slice(2).join(' ');

  for (let i = 0; i < args[1]; i++) {
    recipient.send(messageToSend);
  }
}

推荐阅读