首页 > 解决方案 > 如何让你的机器人发送第二个参数

问题描述

like !L JohnGameRage#0000 将 L 给了 user#0000

标签: javascriptdiscord.js

解决方案


构造参数的一种简单方法是这样的:

let full = message.content.substr(1).trim(); //remove the prefix
let split = full.split(/ +/g); //seperate the words in the String and put them into an array
let cmd = split[0]; //the command is the first word in the array
let args = split.slice(1); //the arguments are all words except for the first one

在您的示例L中将是命令,并且JohnGameRage#0000将是第一个参数。

您可以像这样从数组中读取单个参数:

//Command = !L firstArg secondArg thirdArg fourthArg etc
cmd //returns 'L'
args[0] //returns 'firstArg'
args[1] //returns 'secondArg '
args[2] //returns 'thirdArg '
args[3] //returns 'fourthArg '
args[4] //returns 'etc'

推荐阅读