How to check if message contains emojis using discord.js?

Member

by mac , in category: Javascript , 7 months ago

How to check if message contains emojis using discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cortez.connelly , 6 months ago

@mac 

You can check if a message contains emojis by using regular expressions in Discord.js. Here's an example code snippet to achieve this:

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

client.on('message', (message) => {
    // Define a regular expression to match emojis
    const emojiRegex = /<:.+:d+>/;

    // Check if the message content contains emojis
    if (emojiRegex.test(message.content)) {
        console.log('Message contains emojis!');
    }
});

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


In this code, we create a regular expression emojiRegex that matches the pattern of custom Discord emojis (<:name:id>). We then use the test() method to check if the message content contains any emojis. If the test result is true, it means that the message contains emojis and we log a message to the console.

Related Threads:

How to check if a message exists in discord.js?
How to detect message content using discord.js?
How to send a message to specific channel using discord.js?
How to check if member has a role using discord.js?
How to stop a message loop in discord.js?
How to turn a message into a string with discord.js?