private message

I have a strange issue occurring when trying to read messages in my Discord Bot DM. If I remove my Discord bot from my server and re-add it,I am able to start viewing the messages sent to the bot via dm.

If I disconnect my bot and restart it wont start receiving messages. If I remove the bot from a server and re-add it to a server it will start logging the private messages correctly. If I turn my server off and restart the bot will stop logging.

If I also remove it from a server the bot will stop logging. How do I resolve this and is this the expected behaviour?

import { Client, GatewayIntentBits, Partials } from 'discord.js';
import eventHandlers from './events';
require('dotenv').config();

const client = new Client({
        intents: [GatewayIntentBits.DirectMessageTyping,GatewayIntentBits.DirectMessageReactions,GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages],
        partials: [Partials.Message]
    });

for (const [event, handler] of Object.entries(eventHandlers)) {
    if (handler) {
        client.on(event, handler);
    }
}
client.login(process.env.DISCORD_KEY);
export default client;


Handler:
import { Message,ChannelType } from 'discord.js';
import logger from '../lib/logger';

const handleMessageReceived = (message: Message) => {
    if (message.channel.type === ChannelType.DM){
        logger.info(`Message received in DM from user ${message.author.id}: ${message.content}`);
        return;
    }

    if (message.author.bot) return; // Ignore messages from other bots
        logger.info(`Message received in channel ${message.channel.id} from user ${message.author.id}: ${message.content}`);
};

export default handleMessageReceived;
Was this page helpful?