javascript - 如何发送 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.')
}
},
};
解决方案
您可以使用 删除数组中的前两项,.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);
}
}
推荐阅读
- sql - 如何找到每天登录游戏mroe超过1次的id?
- amazon-iam - IAM 策略通过 Image-Name 拒绝 AMI 图像
- javascript - Reactjs - 内联样式“顶部”和“左侧”
- python - 试图将镶木地板文件解析为熊猫数据框
- postgresql - 值包含文本或符号(>、<)的情况
- docker - 如何从 Docker 打印(到打印机)
- sql - 如何在 SQL 中执行单元格引用之类的操作?
- javascript - 尝试在 CSS 中使用绝对位置时出现问题
- node.js - Node.js E00007 Authorize.net 错误 - 由于身份验证值无效,用户身份验证失败
- java - 页面必须填满整个 ViewPager2(使用 match_parent),而我将其设置为匹配父级