Home > Software design >  Mention bot for respond discord.js v14
Mention bot for respond discord.js v14

Time:11-07

I'm trying to make a command when you mention the bot, it will send a message saying “Slash Command: /help”. I have tried to make it and I tried to find an answer but I can’t find anything that works for me.

Here is the code (I use the code in index.js):

client.on("messageCreate", (message) => {
    if (message.author.bot) return false;

    if (message.content.includes("@here") || message.content.includes("@everyone") || message.type == "REPLY") return false;

    if (message.mentions.has(client.user.id)) {
        message.channel.send("Hello there!");
    }
});

I added the GatewayIntentBits.Guilds intent to my intents array. I don't get any errors, it just doesn’t work.

CodePudding user response:

If you're only using the GatewayIntentBits.Guilds intent, you won't receive messages. You'll also need to add GatewayIntentBits.GuildMessages. It allows you to receive messages if the client/bot is mentioned.

If you also want to receive the message content when the bot is not mentioned, you'll also need to add GatewayIntentBits.MessageContent. However, if you only want to see if the bot is mentioned, adding GatewayIntentBits.GuildMessages to your intents array will be enough.

CodePudding user response:

I recommend you use RegExp, it would be done like this:

client.on("messageCreate", (message) => {
    if (message.author.bot) return;

    if (message.content.includes("@here") || message.content.includes("@everyone") || message.type == "REPLY") return;

    if (message.content.match(new RegExp(`^<@!?${client.user.id}>( |)$`))) {
        message.channel.send("Hello there!");
    }
});

The RegExp object is used for matching text with a pattern. So if it matches what you've typed (in this case the @bot ), send the message

  • Related