'If my message contains a specific thing, how can I make my discord bot read that?
The title may not be very helpful, but what I mean is that if my message says "hello", how can I make my discord bot say hello back, even if the "hello" is not on its own in a single message, and it does not use my bot's prefix?
Solution 1:[1]
It's quite simple actually. Let's say you have a command code like this:
client.on("messageCreate", async message => {
if (message.author.bot) return; //filters out bots
else if (message.content.startsWith(prefix)){
const args = message.content.slice(prefix.length).trim().split(/ +/g)
const commandName = args.shift().toLowerCase();
const command = await client.commands.get(commandName);
if (!command) return;
await command.run(client, message, args);
} //if starts with prefix run any applicable command
else if (message.mentions.users.first()===client.user) {
const helpembed = {
title: "Help",
description: "To view help use `+help`!",
}
return message.reply({embeds: [helpembed]});
} //if not starts with prefix but has bot mentioned
});
add another else if statement in there, after the last else if statement:
else if (message.content.includes("hello")) {
message.reply("hello");
}
note that .includes() is case sensitive.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | panzer-chan |
