Coder
Coder

Reputation: 5

How to make bot say if it atleast includes rather than explicitly saying !*example*

I am making a Discord Bot that separates messages into categories. How can I change it so the message doesn't have to explicitly include !example and rather say the category if the message at least includes it?

For example

bot.on('message' , msg=>{
    if(msg.content === "!part-installations"){
        msg.reply("Re Category: Part Installations");
    }
})

The following code only makes the bot state the category if it specifically states !part-installations with no other words. How can I modify the code so that it can still state the category if it at least has for example !part-installations?

Upvotes: 0

Views: 35

Answers (1)

Brant
Brant

Reputation: 1788

Looks like you could use a regex, which just checks to see if msg.content contains your substring.

if (/!part-installations/gi.test(msg.content)) { }

You could also use .indexOf and if the value is > -1, the substring is in msg.content:

if (msg.content.indexOf('!part-installations') > -1) { }

Upvotes: 1

Related Questions