I’m working on a Discord bot and need help with message detection. Right now my bot only responds when someone types exactly ‘hello’, but I want it to trigger whenever someone mentions ‘hello’ anywhere in their message. For example, if someone writes ‘hello everyone’ or ‘well hello there’, the bot should still respond. How can I modify the code to check if the target word exists within the message instead of matching the entire message content exactly?
Your code should already work for detecting ‘hello’ anywhere in the message. includes() checks if the substring exists in the string, so it’ll trigger for ‘hello everyone’, ‘well hello there’, etc. But there’s probably one issue - case sensitivity. If someone types ‘Hello’ or ‘HELLO’, your current code won’t catch it. Try msg.content.toLowerCase().includes('hello') instead. Also double-check you’re testing with the right message content and that your bot has permissions to read messages in that channel.
Indeed, the includes() method should function properly for detecting substrings. However, I’ve encountered this issue before, and it often relates to the version of Discord.js being used. If you are using version 13 or 14, you must ensure that intents are set up correctly when you create your client instance:
Without these intents, your bot may not receive any message events. Additionally, remember to include a msg.author.bot check to prevent potential loops.
your includes() method is working fine. but the issue might be that your bot isn’t receiving message events. try putting a console.log(msg.content) to see what messages are comin through. also make sure your bot has the right intents enabled in the dev portal - you’ll need MESSAGE_CONTENT_INTENT for newer discord.js versions.