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.
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.
Share Improve this question asked Nov 8, 2018 at 4:11 Mark FaragMark Farag 211 gold badge1 silver badge2 bronze badges 3- It sounds like you're asking for SO to write code for you. Could you show what you've tried to change within those lines, and explain why it didn't work? Maybe link the part of the docs you're trying to deal with – TankorSmash Commented Nov 8, 2018 at 5:30
-
I tried changing the code to look like
if(embed.Message.content.toLowerCase().includes('cyber')) message.channel.send("Key Word Detected ");
if(embed.content.toLowerCase().includes('cyber')) message.channel.send("Key Word Detected ");
nothing seems to be working and everything I've looked up seems to not have any solution to my problem – Mark Farag Commented Nov 8, 2018 at 5:44 -
Are you sure that embed.Message exists ? For me, after looking at this guide, it seems like an embed is a list of fields, so I would do something like
fields = message.embed.fields; len = fields.length; for var i in range(len){ if fields[i].value.contains('cyber')...
– Djazouli Commented Nov 8, 2018 at 6:25
2 Answers
Reset to default 4The 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 ;)
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;
}
}