englezaowo
englezaowo

Reputation: 1

How do I check for a word in a message, in discord js?

I'm trying to make a simple discord bot. How can I make it so it checks for a word inside a message? For example if somebody says "i love bananas" and the trigger word is banana, the bot responds with "same". All I could figure out is how to make it reply to a specific message (like only "banana"), how do I make something like the example I made?

Upvotes: 0

Views: 2402

Answers (1)

amlxv
amlxv

Reputation: 2005

discord.js v13.5.1

const client = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});

const triggerWords = ['banana', 'fire', 'white'];

client.on('messageCreate', (message) => {
  if (message.author.bot) return false;

  triggerWords.forEach((word) => {
    if (message.content.includes(word)) {
      message.reply(message.content);
    }
  });
});

https://discord.js.org/#/docs/discord.js/stable/class/Client?scrollTo=e-messageCreate

Upvotes: 1

Related Questions