Mark Farag
Mark Farag

Reputation: 21

How to get discord bot to read embed

I understand how a discord bot can read a regular user inputed message and respond using

if(message.content.toLowerCase().includes('cyber'))
    message.channel.send("Key Word Detected ");

But it won't read the message if it is an embed. Please help me change that to look for the keyword in a embed message and elicit a response from the bot.

Upvotes: 2

Views: 17001

Answers (2)

Federico Grandi
Federico Grandi

Reputation: 6816

The text in a MessageEmbed can be in author, description, footer, message.content and title. They can also be inside every filed, so might want to check for all that stuff.
Here's a little function you could use (I know it seems a mess but it's just because there are a lot of logical operators):

/*
      message {Discord.Message}: the message you want to search in
      target {string}: the string you're looking for
      {
        caseSensitive {boolean}: whether you want the search to be case case-sensitive
        author {boolean}: whether you want to search in the author's name
        description {boolean}: whether you want to search in the description
        footer {boolean}: whether you want to search in the footer
        title {boolean}: whether you want to search in the title
        fields {boolean}: whether you want to search in the fields
      }
     */
function findInMessage(message, target, {
  caseSensitive = false,
  author = false,
  description = true,
  footer = true,
  title = true,
  fields = true
}) {
  if (!target || !message) return null;
  let str = caseSensitive ? target : target.toLowerCase();

  if ((caseSensitive && message.content.includes(str)) ||
    (!caseSensitive && message.content.toLowerCase().includes(str))) return true;

  for (let embed of message.embeds) {
    if ((caseSensitive && (
        (author && embed.author.includes(str)) ||
        (description && embed.description.includes(str)) ||
        (footer && embed.footer.includes(str)) ||
        (title && embed.title.includes(str)))) ||
      (!caseSensitive && (
        (author && embed.author.toLowerCase().includes(str)) ||
        (description && embed.description.toLowerCase().includes(str)) ||
        (footer && embed.footer.toLowerCase().includes(str)) ||
        (title && embed.title.toLowerCase().includes(str))))
    ) return true;

    if (fields)
      for (let field of embed.fields) {
        if ((caseSensitive && [field.name, field.value].includes(str)) ||
          (!caseSensitive && [field.name.toLowerCase(), field.value.toLowerCase()].includes(str))) return true;
      }
  }

  return false;
}

The functions returns true when finds the word you put in, false when it doesn't find it and null when one of the non-optional arguments is missing.
You can use it like this:

if (findInMessage(message, 'cyber')) message.channel.send("Key word detected.");

There are some instructions at the top, hope this helps ;)

Upvotes: 4

Tom Martin
Tom Martin

Reputation: 300

This should be it, checking for all embeds in the message too

if(message.content.toLowerCase().includes('cyber'))
    message.channel.send("Key Word Detected ");
else {
    for(var i = 0; i < message.embeds.length; i++) {
        if(message.embeds[i].title.includes("cyber") || message.embeds[i].title.includes("cyber")) {
            message.channel.send("Detected");
            break;
    }
}

Upvotes: 1

Related Questions