0

recently I made a discord bot that bans new members automaticly when someone who created their account less then 30 days ago joins.The problem is that when someone joins nothing happens and neither the log shows anything.Wondering what could be the problem.thanks!!

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

   client.on('guildMemberAdd', member => {
  console.log(`${member.user.username} csatlakozott a szerverhez.`);
  const currentTime = Date.now();
  const creationTime = member.user.createdAt.getTime();
  const ageInMs = currentTime - creationTime;
  const ageInDays = ageInMs / 1000 / 60 / 60 / 24;

  if (ageInDays < 30) {
    console.log(`${member.user.username} fiókja egy hónapnál fiatalabb. Bannolás és üzenet küldése...`);
    member.ban({ reason: 'Fiók létrehozási dátuma egy hónapnál fiatalabb' });
    member.send('Sajnáljuk, de a fiókod egy hónapnál fiatalabb, így nem tudsz csatlakozni a     szerverhez.')
      .then(() => console.log(`Üzenet elküldve ${member.user.username}-nek.`))
      .catch(error => console.error(`Hiba történt az üzenet küldése közben: ${error}`));
  }    
});

client.login('token')
  .then(() => console.log('Bot bejelentkezve.'))
  .catch(error => console.error(`Hiba történt a bejelentkezés közben:  ${error}`));
`

tried: Joining several servers, the log should of show the join, and automaticly ban the member.

  • Does that post helps you ? Seems like a similar problem https://stackoverflow.com/questions/64559390/none-of-my-discord-js-guildmember-events-are-emitting-my-user-caches-are-basica – AlanOnym Feb 11 '23 at 14:08
  • The intents werent enabled but after I enabled them its still not showing any signs of life – Klepacs Adam Feb 11 '23 at 15:42

1 Answers1

0

According to this link https://discordjs.guide/popular-topics/intents.html#enabling-intents

If you want your bot to post welcome messages for new members (GUILD_MEMBER_ADD - "guildMemberAdd" in discord.js), you need the GuildMembers privileged intent, and so on.

change this

const client = new Client({
  intents: GatewayIntentBits.Guilds
});

for this

const client = new Client({
    intents: [
        GatewayIntentBits.GuildMembers
    ]
})

This should also work (although I did not try it)

const client = new Client({ ws: { intents: ['GUILD_MEMBER_ADD'] } });

Restart your bot and it should work. I managed to test it and ban a new account (I did not test if your bot also ban older accounts)

You can find the list of intents here https://discord.com/developers/docs/topics/gateway#list-of-intents

AlanOnym
  • 131
  • 4