Search code examples
discorddiscord.js

The messageCreate event isn't firing when privately DM'ing my Discord bot directly


When sending a personal message to my Discord bot, the event listener(s) that I have tried aren't firing. Am I doing anything wrong here?

const { Client, Events, GatewayIntentBits, ApplicationCommandOptionType } = require('discord.js');

const bot = new Client({
    partials: ["CHANNEL"],
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildVoiceStates,
        GatewayIntentBits.GuildMessageReactions,
        GatewayIntentBits.DirectMessages,
        GatewayIntentBits.GuildMessageTyping,
        GatewayIntentBits.DirectMessageTyping
    ]
}); // A list of the intents that my bot is currently using for other functionality on the server

bot.on('messageCreate', async msg => {
    console.log('message');
    // This DOES fires for user messages sent directly on the server only ...
    // Why does this not fire when a user sends a message to my bot in DM's?
});

// The below functionality used to work when using an old Discord.js version with the below code:

bot.on('message', async msg=>{
    console.log('message1'); // This no longer works
});

Solution

  • Your code worked when I tried it, so I'm not able to test this solution, but taken from this SO post, try adding MESSAGE to your partials array, and use the new Partial enum:

    const { Partials } = require("discord.js");
    const bot = new Client({
        partials: [Partials.Channel, Partials.Message],
        intents: [
            GatewayIntentBits.Guilds,
            GatewayIntentBits.GuildMessages,
            GatewayIntentBits.MessageContent,
            GatewayIntentBits.GuildMembers,
            GatewayIntentBits.GuildVoiceStates,
            GatewayIntentBits.GuildMessageReactions,
            GatewayIntentBits.DirectMessages,
            GatewayIntentBits.GuildMessageTyping,
            GatewayIntentBits.DirectMessageTyping
        ]
    });
    

    If this still doesn't work, give me your exact discord.js version and I'll see if I can reproduce your problem - and given the fact that you've had to use the string literal "CHANNEL" for the partials array, I have a feeling your version might be outdated.