使用.split()获取包含许多空格的用户名的问题(NodeJS中的Discord.js)
我正在开发我的 Discord Bot 的一项功能,当您!lolid [enter_username_here]在 Discord 聊天室中编写以下命令时,它会返回玩家的英雄联盟统计数据,
我成功地为一个用户名只有 1 个字的人做到了这一点,但不幸的是,对我来说,英雄联盟的用户名可以有空格(可能的用户名示例:)I AM A GOD,
当用户输入以下命令时!lolid [enter_username_here],我将它作为message.content变量保存在我的 JS 文件中,这样message.content.split(" ")[1];我就可以获得他写的用户名,
但是如果用户名包含很多空格,我该怎么办?谢谢,
部分涉及的代码:
if (message.content.startsWith("!lolid")) {
console.log(message.content.split(" ")); => if someone types !lolid 1 2 3, it returns ["lolid", "1", "2", "3"];
const username = message.content.split(" ")[1];
const promise1 = new Promise(async (resolve, reject) => {
resolve(await getSummonerId(username)); => returns id needed to do the request for the stats
});
}
回答
为什么不删除命令部分:
const username = message.content.slice(7);
或者
const username = message.content.split("!lolid ")[1];
或者
const username = message.content.replace(/!lolids/, "");
感谢@GACy20的第一个建议
THE END
二维码