messageCreate isn't firing

I'm not getting messageCreate triggers when a message is sent in a chat channel. To cover items from similar questions: - I've triple-checked my intents (see below) - I've got the "partials" thing in the code - The bot is in the server (/commands work) - The bot has access to all (one) channels - Perms are properly set in the bot portal Relevant Code:
// create a new Client instance
const discord_client = new Client({
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageTyping,
GatewayIntentBits.MessageContent,
],
partials: [
Partials.Message,
Partials.Channel,
]
});

// listen for the client to be ready
discord_client.once(Events.ClientReady, (c) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});

// listen for general chat and respond
discord_client.on(Events.MessageCreate, async interaction => {
console.log(`Responding to chat in ${interaction.channelId}`);
});
// create a new Client instance
const discord_client = new Client({
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageTyping,
GatewayIntentBits.MessageContent,
],
partials: [
Partials.Message,
Partials.Channel,
]
});

// listen for the client to be ready
discord_client.once(Events.ClientReady, (c) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});

// listen for general chat and respond
discord_client.on(Events.MessageCreate, async interaction => {
console.log(`Responding to chat in ${interaction.channelId}`);
});
The log from the ClientReady event is seen as expected, but the log from MessageCreate never shows up at all. Unless I'm totally understanding things, I should see that log every time someone writes something in the server where the bot is present, right?
17 Replies
d.js toolkit
d.js toolkitβ€’3d 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!
d.js docs
d.js docsβ€’3d ago
If you aren't getting any errors, try to place console.log checkpoints throughout your code to find out where execution stops. - Once you do, log relevant values and if-conditions - More sophisticated debugging methods are breakpoints and runtime inspections: learn more
Kinect3000
Kinect3000β€’3d ago
In the ready event, can you log discord_client.channels.cache.get(<"channel_id">).viewable?
ChainsawXIV
ChainsawXIVOPβ€’3d ago
Looks like that narrows the problem... I added:
var count = discord_client.channels.cache.size;
console.log(`Channels: ${count}`);
if( count > 0 ){
var temp = discord_client.channels.cache.get("general").isSendable();
console.log(`Can send in #general: ${temp}`);
}
var count = discord_client.channels.cache.size;
console.log(`Channels: ${count}`);
if( count > 0 ){
var temp = discord_client.channels.cache.get("general").isSendable();
console.log(`Can send in #general: ${temp}`);
}
And it tells me the bot has 0 cached channels. Now the question is why... (Incidentally, viewable doesn't appear to be a property of channels.)
Kinect3000
Kinect3000β€’3d ago
It’s a property of guild channels You would need to cast or use the type guard
ChainsawXIV
ChainsawXIVOPβ€’3d ago
Gotcha. In any case, there's nothing in the collection there. I've double checked the perms and they look right to me. Any thoughts on what's missing?
No description
Kinect3000
Kinect3000β€’3d ago
Can you check the guilds cache?
ChainsawXIV
ChainsawXIVOPβ€’3d ago
Sure thing πŸ™‚ It also reports being in 0 guilds. (or there are zero in the cache at least - not sure if that means what I expect it to)
Kinect3000
Kinect3000β€’3d ago
And this is being logged in the ready event?
ChainsawXIV
ChainsawXIVOPβ€’3d ago
Yup:
// listen for the client to be ready
discord_client.once(Events.ClientReady, (c) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
let count = discord_client.channels.cache.size;
let gcount = discord_client.guilds.cache.size;
console.log(`Channels: ${count}, Guilds: ${gcount}`);
if( count > 0 ){
let temp = discord_client.channels.cache.get("general");
console.log(`Can see #general: ${(temp as GuildChannel).viewable}`);
}
});
// listen for the client to be ready
discord_client.once(Events.ClientReady, (c) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
let count = discord_client.channels.cache.size;
let gcount = discord_client.guilds.cache.size;
console.log(`Channels: ${count}, Guilds: ${gcount}`);
if( count > 0 ){
let temp = discord_client.channels.cache.get("general");
console.log(`Can see #general: ${(temp as GuildChannel).viewable}`);
}
});
monbrey
monbreyβ€’3d ago
Sounds like the part about the bot being in the server isn't true - you may have added the slash commands, or the slash commands are user installed, with the bot scope being enabled for that server
ChainsawXIV
ChainsawXIVOPβ€’3d ago
I... think that makes sense, but I'm not entirely sure I actually understand πŸ˜„ In fact, I'm fully sure I don't completely understand... Can you throw that at me in different words and/or speculate about how that happens and how to fix it? I have verified that the /commands are in fact talking to this specific server and back by checking the server logs and changing the message sent back in response to a simple "ping" even to something unique.
monbrey
monbreyβ€’3d ago
Slash commands use (or can use) a completely different system to adding the bot user to a server The user is required for the guild (and its channels) to be in cache, and for regular events like messageCreate to be received and processed If you only add its slash commands to the guild it will only receive and process interactionCreate events
ChainsawXIV
ChainsawXIVOPβ€’3d ago
That makes sense, thanks. Now I'm trying to figure out how to google that problem considering it contains almost no unique search terms πŸ˜„
monbrey
monbreyβ€’3d ago
Its based on the scopes you defined when you add a bot to a server "bot" is the bot user, "applications.commands" is the slash commands So try re-adding it first with those scopes
Naomi
Naomiβ€’3d ago
You can use the URL generator in the dev portal to create an invite link with the necessary scopes.
ChainsawXIV
ChainsawXIVOPβ€’3d ago
Thanks much - this was in fact the issue, and easily fixed with your guidance. πŸ™‚ Much appreciated to all who helped! β™₯️ For posterity and anyone coming along later with this problem, we needed to go into the bot's setup on https://discord.com/developers/applications, to the Installation section, add bot to the Guild Install Scopes then add the appropriate permissions from the dropdown, then re-add the bot to the server and approve all its new perms. It was non-obvious that adding bot would expand into a bunch more options, and we thought we had everything we needed with our other perms.

Did you find this page helpful?