Discord bot not responding to new member joins

I created a Discord bot that’s supposed to automatically ban new members if their account is less than 30 days old. But it’s not working as expected. When someone joins, nothing happens and the logs don’t show anything. I’ve tried joining several servers, but the bot doesn’t log the join or ban the member automatically.

Here’s my code:

const { Client, GatewayIntentBits } = require('discord.js');
const bot = new Client({
  intents: GatewayIntentBits.Guilds
});

bot.on('memberJoin', user => {
  console.log(`${user.name} joined the server.`);
  const joinDate = Date.now();
  const accountCreation = user.createdAt.getTime();
  const accountAge = (joinDate - accountCreation) / (1000 * 60 * 60 * 24);

  if (accountAge < 30) {
    console.log(`${user.name}'s account is less than a month old. Banning and sending message...`);
    user.ban({ reason: 'Account created less than a month ago' });
    user.sendMessage('Sorry, your account is too new to join this server.')
      .then(() => console.log(`Message sent to ${user.name}.`))
      .catch(err => console.error(`Error sending message: ${err}`));
  }    
});

bot.login('my_token')
  .then(() => console.log('Bot logged in.'))
  .catch(err => console.error(`Login error: ${err}`));

Any ideas what could be causing this issue? Thanks for your help!

I’ve encountered this exact issue before. The problem lies in your bot’s configuration and event handling. You’re missing the crucial GuildMembers intent, which is essential for detecting member join events. Also, the event name you’re using is incorrect.

To fix this, modify your code like this:

const { Client, GatewayIntentBits } = require('discord.js');
const bot = new Client({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers]
});

bot.on('guildMemberAdd', member => {
  // Your existing code here
});

This should resolve your issue. The bot will now properly detect new members joining and execute your ban logic as intended. Remember to thoroughly test after making these changes to ensure everything works as expected.

hey man, i had similar issue. check ur intents, u need GuildMembers intent for member events. also, event name is ‘guildMemberAdd’ not ‘memberJoin’. fix those and it should work. goodluck!

It appears that the issues stem from two main points. First, the bot only uses the Guilds intent, but for member join events you’ll need to include the GuildMembers intent. Second, the event is misnamed: the proper event name is ‘guildMemberAdd’ rather than ‘memberJoin’. Adjust your code accordingly as shown below:

const bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] });

bot.on('guildMemberAdd', member => { /* code here */ });

These changes should help the bot properly detect new members and manage the ban logic.