if (message.content.startsWith('let', 'Let')){/do something/})
I want bot work when user type "let" or "Let", but it can just work when "let" typed How to fix this? Thanks for helping
You could use regular expressions here:
var terms = ["let", "Let"];
var regex = new RegExp("^(?:" + terms.join("|") + ")\\b");
var inputs = ["Let it be!", "let it be!", "Green Eggs and Ham"];
for (var i=0; i < inputs.length; ++i) {
if (regex.test(inputs[i])) {
console.log("MATCH => " + inputs[i]);
}
else {
console.log("NO MATCH => " + inputs[i]);
}
}
To be clear, we are using test()
with the regex pattern:
^(?:let|Let)\b
You may add more terms to the above alternation as needed.
To keep things flexible you could keep an array of words you want to check, and then use some
to check to see if any of those words are at the beginning of the content string.
const words = ['let', 'Let', 'Bob'];
const message1 = { content: 'let' };
const message2 = { content: 'notlet' };
const message3 = { content: 'Let' };
const message4 = { content: 'Bob' };
function startsWith(message) {
return words.some(word => {
return message.content.startsWith(word);
});
}
console.log(startsWith(message1));
console.log(startsWith(message2));
console.log(startsWith(message3));
console.log(startsWith(message4));
I believe you are looking for case insensitive match. In that case following can work assuming your message.content
is a string or object.
Here we collect user typed string in userTypedThis
variable.
if (message.content.toString().toLowerCase().startsWith(
userTypedThis.toString().toLowerCase())
){/do something/})