简体   繁体   中英

Javascript/Discord.js-How do I make a bot only respond to a message if it contains a specific word and does not have letters around it

client.on("messageCreate", message => {

    let string = message.content;

    string = string.toLowerCase();

    if (string.indexOf("andy") >= 0) {
        message.channel.send("Test")
    }

})

I was wondering how one would make the code not send "test" if string would be a string like candy where it responds as if the string is andy. I would like it to only respond if the string is andy and not if the string has letters around it which change the meaning of the word.

I am sorry for this basic question but I did not know how to word it when searching for it. Thank you for any help you can provide.

This would be done by simply checking if the message content matched and includes. By putting a space before and after it will only look for that word if it isn't part of another word like candy or randy.

client.on("messageCreate", async message => {
    if (message.content.toLowerCase().includes(' andy ')) {
        message.channel.send("Test")
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM