Discord.js 命令输入字符串
Discord.js Command Input Strings
我正在编写一个 Discord 机器人游戏,我的问题是玩家必须创建一个角色。然而,每个标准人都有名字和姓氏,但我在一个命令中接受其他输入。我的问题是,如果确实放置了引号,我不确定我的代码是否会忽略名称周围的引号。
假设某人将其角色命名为 Joe Shmoe。当他们输入命令 p://ccreate "Joe Shmoe" Male 38
时,我担心我的参数解释器可能会将“"Joe' as args[0], 'Shmoe"”作为 args[1],Male 作为 args[2],并将 38 作为 args[3]。我的问题是我希望 "Joe Shmoe" 被解释为一个参数。这是我的代码:
client.on("message", async message => {
if(message.author.bot) return;
if(message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
如果我是对的,您能否详细说明如何编辑它,以便在我的代码收到此类问题时将字符串保持为一个?但是,我也知道一些替代方案可能需要在任何非整数、布尔值等周围加上引号,所以最好你能找到一种方法,将引号中的任何内容都包含为一个字符串,如果一个字符串只是一个单词,它不抛代码,依然可以识别为字符串。我知道这已经完成了,只是不知道怎么做。
有多种方法可以做到这一点,这是一种可能的方法,其中的注释解释了每一步中发生的事情。
/* The user enters p://ccreate "Joe Shmoe" Male 38 */
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
// args = ['p://ccreate', '"Joe', 'Shmoe"', 'Male, '38'];
const command = args.shift().toLowerCase();
// args = ['"Joe', 'Shmoe"', 'Male', '38'];
/* Now you want to get the full name, i.e: the first 2 elements from args */
const name = args.slice(0,2).join(' ').replace(/"/g,'');
/* args.slice(0,2) gets the first 2 elements
.join(' ') joins these elements separating them with a space
.replace(/"/g,'') removes all instances of the character " */
// So now, name = 'Joe Shmoe'
const gender = args.slice(-2)[0];
/* args.slice(-2) gets the last 2 elements in the array
[0] gets the first element in the returned array of 2 elements */
// Therefore, gender = 'Male'
const age = args.slice(-1);
/* args.slice(-1) gets the last element in the array */
// As a result, age = '38'. Keep in mind that this is a string, you could use parseInt(age) if you require it as an integer.
我正在编写一个 Discord 机器人游戏,我的问题是玩家必须创建一个角色。然而,每个标准人都有名字和姓氏,但我在一个命令中接受其他输入。我的问题是,如果确实放置了引号,我不确定我的代码是否会忽略名称周围的引号。
假设某人将其角色命名为 Joe Shmoe。当他们输入命令 p://ccreate "Joe Shmoe" Male 38
时,我担心我的参数解释器可能会将“"Joe' as args[0], 'Shmoe"”作为 args[1],Male 作为 args[2],并将 38 作为 args[3]。我的问题是我希望 "Joe Shmoe" 被解释为一个参数。这是我的代码:
client.on("message", async message => {
if(message.author.bot) return;
if(message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
如果我是对的,您能否详细说明如何编辑它,以便在我的代码收到此类问题时将字符串保持为一个?但是,我也知道一些替代方案可能需要在任何非整数、布尔值等周围加上引号,所以最好你能找到一种方法,将引号中的任何内容都包含为一个字符串,如果一个字符串只是一个单词,它不抛代码,依然可以识别为字符串。我知道这已经完成了,只是不知道怎么做。
有多种方法可以做到这一点,这是一种可能的方法,其中的注释解释了每一步中发生的事情。
/* The user enters p://ccreate "Joe Shmoe" Male 38 */
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
// args = ['p://ccreate', '"Joe', 'Shmoe"', 'Male, '38'];
const command = args.shift().toLowerCase();
// args = ['"Joe', 'Shmoe"', 'Male', '38'];
/* Now you want to get the full name, i.e: the first 2 elements from args */
const name = args.slice(0,2).join(' ').replace(/"/g,'');
/* args.slice(0,2) gets the first 2 elements
.join(' ') joins these elements separating them with a space
.replace(/"/g,'') removes all instances of the character " */
// So now, name = 'Joe Shmoe'
const gender = args.slice(-2)[0];
/* args.slice(-2) gets the last 2 elements in the array
[0] gets the first element in the returned array of 2 elements */
// Therefore, gender = 'Male'
const age = args.slice(-1);
/* args.slice(-1) gets the last element in the array */
// As a result, age = '38'. Keep in mind that this is a string, you could use parseInt(age) if you require it as an integer.