Vineeth B V
Vineeth B V

Reputation: 75

How to check which message is sent and return reply accordingly?

I'm building a discord bot and I want the bot to reply based on what input I give. My JSON file looks like this.

const greetings = [
    {
        id: 1,
        received: [
            "Hi",
            "Hi there",
            "Hello",
            "Hey",
            "Hey there"
        ],
        reply: "Hi there"
    },
    {
        id: 2,
        recived: [
            "Wassup",
            "Sup"
        ],
        reply: "Nothing much. Wassup",
    },
    {
        id: 3,
        received: [
            "How are you doing",
            "How are you doing?",
            "How have you been doing",
            "How have you been doing?",
        ],
        reply: "I'm good. And I hope you're doing well too",
    },
];

When I send a message like Hi or Hello there, I want it to respond with Hi there. And when I send a message like How are you doing, it should reply with I'm good. And I hope you're doing well too.

How do I check which message is sent and return the appropriate reply?

Upvotes: 0

Views: 187

Answers (1)

dropdb
dropdb

Reputation: 656

You can use Array.find to find an array item with its value and Array.some to check if it exists.

The code would be:

if (greetings.some(x => x.received.includes(message.content))) {
  message.channel.send(greetings.find(x => x.received.includes(message.content)).reply);
})

Upvotes: 1

Related Questions