Home > Mobile >  Listen to messageCreate event in DM Channel discord.js
Listen to messageCreate event in DM Channel discord.js

Time:11-25

I've seen this question a lot, and I feel like I've tried most suggestions, but I can't get it to work. For now, I'm just logging the messageCreate event, but it just won't work. Help would be very much appreciated.

This is my code:

as you can see I've tried a couple things^^


const { GatewayIntentBits, Partials, Guilds, Events } = require('discord.js');
const Discord = require('discord.js');

const Client = new Discord.Client({
    intents: [
        /* GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.DirectMessages, 
        GatewayIntentBits.DirectMessageTyping,
        GatewayIntentBits.MessageContent, */

        'Guilds', 'GuildMessages', 'DirectMessages', 'MessageContent'

    ],
    partials: [
        /* Partials.Channel, 
        Partials.GuildMember,
        Partials.Message,
        Partials.MessageReaction, 
        Partials.User,
        Partials.GuildMessages,
        Discord.PartialGroupDMChannel */

        "CHANNEL", "GUILD_MEMBER", "MESSAGES", "MESSAGE_REACTION", "USER", "DMCHANNEL"
    ]
});


Client.on('messageCreate', async message => {
    console.log('messageCreate event');
};

I've tried different ways of enabling the right intents and partials, but I'm about to lose my sanity.

CodePudding user response:

You have to import Client from djs idk if the other thing will work but the way I do it is and change Client to client in messageCreate

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

const myIntents = new IntentsBitField();
myIntents.add(GatewayIntentBits.GuildMembers,GatewayIntentBits.GuildMessages,GatewayGatewayIntentBits.DirectMessages,GatewayIntentBits.DirectMessageTyping,GatewayIntentBits.Guilds,GatewayIntentBits.MessageContent);
const client = new Discord.Client({ partials: [Partials.Message, Partials.Channel, Partials.MessageReaction, Partials.User,Partials.GuildMessages, Discord.PartialGroupDMChannel], intents: myIntents }); 

CodePudding user response:

Add this into your main/bot.js

const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
 intents: [
  GatewayIntentBits.Guilds,
  GatewayIntentBits.GuildMessages,
  GatewayIntentBits.MessageContent,
  GatewayIntentBits.GuildMembers
  GatewayIntentBits.DirectMessages,
  GatewayIntentBits.DirectMessageTyping,
  GatewayIntentBits.DirectMessageReactions]
});

module.exports = client;

If you are using a eventHandler Create "messageCreate.js" file and paste this code

const client = require("YOUR_MAIN_FILE_PATH"); 
client.on("messageCreate", async(message)=>{
    //Your code
})

Some useful websites:

  1. https://discord-api-types.dev/api/discord-api-types-v10/enum/GatewayIntentBits -> For GatewayIntentBits Information
  2. https://discord.js.org/#/docs/discord.js/main/general/welcome -> For Discord.js documentation
  • Related