获取 args[0] discord.js 之后的所有参数
Get all arguments after args[0] discord.js
我试图制作一个静音命令,我正在添加一个系统,您可以在其中出于某种原因将它们静音。机器人会回复“(用户的用户名)被静音。原因:(原因)”。对我来说 args[0] 只是提到你想静音的用户,但我不知道如何在 args[0] 之后得到所有。我试过这样做 message.channel.send('I have muted' + (mutedUser) + 'Reason: ' + args[1++]
。但这显然行不通 - 我有点猜测 - 我转而列出 4 个这样的参数。
message.channel.send('I have muted ' + taggedUser.user.username + ' Reason: ' + args[1] + ' ' + args[2] + ' ' + args[3] + ' ' + args[4])
但显然,这不是很有效 - 有谁知道如何在 args[0] 之后获取所有参数?
您可以使用Array.prototype.join()
and Array.prototype.slice()
const str = 'first second third fourth fifth sixth seventh...';
const args = str.split(' ');
console.log(args[0]); // first
console.log(args.slice(1).join(' ')); // everything after first
console.log(args[3]); // fourth
console.log(args.slice(4).join(' ')); // everything after fourth
// basically, `Array.prototype.join()` can join every element of an array (with an optional separator- in this case a space)
console.log(args.join(' ')); // join all elements with a space in between
// and `Array.prototype.slice()` can slice off elements of an array
console.log(args.slice(5)); // slice off 5 elements
// now you can combine these two :)
取args
的数组和slice()
要删除的参数个数,然后join()
将剩余的数组元素合并成一个字符串
快速提示
使用模板文字更容易格式化字符串和变量
const reason = args.slice(1).join(' ');
message.channel.send(`I have muted ${mutedUser}, Reason: ${reason}`);
在一般情况下,您可以这样使用 destructuring assignment:
const [foo, ...bar] = args;
此处,foo
等于 args[0]
,args
的其余部分作为数组包含在 bar
中。
根据您的具体情况,您可以在命令中执行此操作:
const [mutedUser, reason] = args;
然后按照 Elitezen 的建议,使用模板文字发送消息。
message.channel.send(`I have muted ${mutedUser}, Reason: ${reason}`);
我试图制作一个静音命令,我正在添加一个系统,您可以在其中出于某种原因将它们静音。机器人会回复“(用户的用户名)被静音。原因:(原因)”。对我来说 args[0] 只是提到你想静音的用户,但我不知道如何在 args[0] 之后得到所有。我试过这样做 message.channel.send('I have muted' + (mutedUser) + 'Reason: ' + args[1++]
。但这显然行不通 - 我有点猜测 - 我转而列出 4 个这样的参数。
message.channel.send('I have muted ' + taggedUser.user.username + ' Reason: ' + args[1] + ' ' + args[2] + ' ' + args[3] + ' ' + args[4])
但显然,这不是很有效 - 有谁知道如何在 args[0] 之后获取所有参数?
您可以使用Array.prototype.join()
and Array.prototype.slice()
const str = 'first second third fourth fifth sixth seventh...';
const args = str.split(' ');
console.log(args[0]); // first
console.log(args.slice(1).join(' ')); // everything after first
console.log(args[3]); // fourth
console.log(args.slice(4).join(' ')); // everything after fourth
// basically, `Array.prototype.join()` can join every element of an array (with an optional separator- in this case a space)
console.log(args.join(' ')); // join all elements with a space in between
// and `Array.prototype.slice()` can slice off elements of an array
console.log(args.slice(5)); // slice off 5 elements
// now you can combine these two :)
取args
的数组和slice()
要删除的参数个数,然后join()
将剩余的数组元素合并成一个字符串
快速提示 使用模板文字更容易格式化字符串和变量
const reason = args.slice(1).join(' ');
message.channel.send(`I have muted ${mutedUser}, Reason: ${reason}`);
在一般情况下,您可以这样使用 destructuring assignment:
const [foo, ...bar] = args;
此处,foo
等于 args[0]
,args
的其余部分作为数组包含在 bar
中。
根据您的具体情况,您可以在命令中执行此操作:
const [mutedUser, reason] = args;
然后按照 Elitezen 的建议,使用模板文字发送消息。
message.channel.send(`I have muted ${mutedUser}, Reason: ${reason}`);