过去 3-4 天我在 Discord 上做的一些项目遇到了问题。当然,这与机器人有关,我选择的语言是 javascript (discord.js)。
所以,事情看起来有点简单,但我真的陷入了这个困境,因为我对 javascript 只有一点经验。
机器人应该读取消息上的两个值,这些值是字符串和数字。机器人只会简单地给您起一个字符串和数字的昵称。
示例:用户说:john123 40 机器人:将用户重命名为“John123 | 40 ”
昵称命令等是简单的部分,对我来说困难的是我应该如何告诉机器人“取字符串,将其放在“|”左侧,取数字,将其放在“|”右侧。 ” “。我的意思是机器人甚至无法读取它们。这是我的尝试:
var name = message.content.includes(String)
var number = message.content.includes("1"|| "2"|| "3"|| "4"|| "5"|| "6"|| "7"|| "8"|| "9"|| "10"|| "11"|| "12"|| "13"|| "14"|| "15"|| "16"|| "17"|| "18"|| "19"|| "20"|| "21"|| "22"|| "23"|| "24"|| "25"|| "26"|| "27"|| "28"|| "29"|| "30"|| "31"|| "32"|| "33"|| "34"|| "35"|| "36"|| "37"|| "38"|| "39"|| "40")
function theNaming (name, number){
message.member.setNickname('name'|' number')
.then(console.log)
.catch(console.error);
}
(级别应该不会高于 40,所以我认为它可以在包含内工作)
最佳答案
How should I tell the bot "take the string, put it left of the "|", take the number, put it right of the "|" ". I mean the bot can't even read them.
如果您以正确的格式传递消息,机器人就可以读取它们。您的问题应该提到 name
和 number
将用空格分隔。这就是机器人从输入的消息中识别名称
和号码
的方式。
String.split()可用于将消息字符串拆分为 name
和 number
。
var message = "john123 40";
var words = message.split(' '); // Using space as the separator.
console.log(words);
现在 words
数组包含消息中用空格分隔的字符串。您可以使用它们的索引来访问它们。
var name = words[0];
var number = words[1];
然后将它们作为参数传递给命名函数,例如 theNaming(name, number);
关于javascript - 机器人通过用户命令重命名用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58045379/