How to detect message content using discord.js?

by mallory_cormier , in category: Javascript , a month ago

How to detect message content using discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by addison , a month ago

@mallory_cormier 

You can detect message content in Discord.js by accessing the content of the message object. Here's a simple example on how to do this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
const Discord = require('discord.js');
const client = new Discord.Client();

client.on('message', message => {
    if (message.content.toLowerCase().includes('hello')) {
        message.reply('Hi there!');
    }
});

client.login('your-bot-token');


In this example, we use the message event to listen for any messages sent in the server. We then use the content property of the message object to access the text content of the message. We use the toLowerCase() method to convert the content to lowercase to make the detection case-insensitive. We then use the includes() method to check if the message content includes the word "hello". If it does, we reply with a message saying "Hi there!".


You can customize this code to detect different message content or perform different actions based on the content of the message.