我试图让我的机器人用一个词来回答多种不同的信息:
const bomba = new Discord.Client();
const a = "bomba" || "bomb" || "bob";
const b = "hey" || "sup" || "hello" || "hi";
bomba.on("message", message => {
if (message.author == bomba.user) return;
if (message.content.toLowerCase() === a + b) {
bomba.channels.cache.get(`${message.channel.id}`).send("Hi!");
};
});
我该怎么做?
您可以使用Array.includes()
:
if (["bomba", "bomb", "bob"].includes(message.content.toLowerCase())) {
message.channel.send("Hi!");
};
请注意,最好按用户比较用户。id
属性,而不是检查它们是否引用了与代码中相同的实例。
if (message.author.id == bomba.user.id) return;
关于==
运算符的MDN文档:
如果两个操作数都是对象,则仅当两个操作数引用同一对象时,才返回true。
您可以使用正则表达式和。match()
函数根据几个单词检查消息内容。请看下面的代码并尝试一下:
const bomba = new Discord.Client();
const clientNames = ["bomba", "bomb", "bob"].join('|');
const greetings = ["hey", "sup", "hello", "hi"].join('|');
const regex = new RegExp(`^(${clientNames})\\s(${greetings})$`, 'gi');
bomba.on("message", message => {
if (message.author == bomba.user) return;
if (message.content.match(regex)) {
bomba.channels.cache.get(`${message.channel.id}`).send("Hi!");
}
});
有关正则表达式的更多信息,请查看此stackoverflow问题/答案