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;
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;
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;
5 Replies
d.js toolkit
d.js toolkit14mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Mark
Mark14mo ago
show client constructor and relevant code please
LunarRage | DNA
LunarRage | DNAOP14mo ago
Sorry about that!
Mark
Mark14mo ago
you need the channel partial
d.js docs
d.js docs14mo ago
To receive direct message events on "messageCreate" with your bot, you will need: - The DirectMessages gateway intent - The Channel partial setting
Want results from more Discord servers?
Add your server