#Timed Role Assignment Bot

6 messages · Page 1 of 1 (latest)

last ivy
#

So I have this code that was supposed to make a bot I created assign roles to members of my server after they’ve been in it for a certain period of time and announce when they had achieved getting each role. But it keeps giving me errors with the Intents. I made sure the perms for the bot matched the intents but still no luck.

const { Client, Intents } = require("discord.js");
const {token, guildId} = require("./config.json");
const {DateTime} = require("luxon");
const client = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS],
token: '[REDACTED]'
});
const roles = [
  ["Newcomer", 0],
  ["Apprentice Trainer", 7],
  ["Regular Trainer", 14],
  ["Advanced Trainer", 30],
  ["Experienced Trainer", 60],
  ["Proficient Trainer", 90],
  ["Veteran Trainer", 180],
  ["Expert Trainer", 270],
  ["Master Trainer", 365],
  ["Elite Trainer", 540],
  ["Champion Trainer", 730],
  ["Legendary Trainer", 1095],
  ["Grandmaster Trainer", 1460]
];


client.once("ready", () => {
  console.log(`Logged in as ${client.user.tag} - PokeProgressor`);
  checkRolesLoop.start();
});


async function checkRoles() {
  const guild = await client.guilds.fetch(1243576389324767383);
  const rankUpChannel = guild.channels.cache.find(channel => channel.name === "rank-up");
  if (!rankUpChannel) {
    console.log("Rank-up channel not found. Please create a channel named 'rank-up");
    return;
  }


  guild.members.cache.forEach(async member => {
    const joinDate = member.joinedAt;
    const daysInServer = DateTime.utc().diff(DateTime.fromJSDate(JoinDate), "days").days;


    for (const [roleName, daysRequired] of roles.reverse()) {
      const role = guild.roles.cache.find(role => role.name === roleName);
      if (!role) continue;


      if (daysInServer >= daysRequired && !member.roles.cache.has(role.id)){
        await member.roles.add(role);
        switch (roleName){
          case "Champion Trainer":
            await rankUpChannel.send(`🏅 ${member} has ascended to the rank of **${roleName}**! You're among the best of the best!`);
            break;
          case "Legendary Trainer":
            await rankUpChannel.send(`🌟 ${member} has reached the prestigious rank of **${roleName}**! Your legend continues to grow!`);
            break;
          case "Grandmaster Trainer":
            await rankUpChannel.send(`👑 ${member} has achieved the highest honor as **${roleName}**! Congratulations on becoming a true Pokémon Master!`);
            break;
          default:
            await rankUpChannel.send(`🎉 ${member} has reached the rank of **${roleName}**! Keep progressing and aim for the top!`);
          }
          break;
        }
      }
    });
}
setInterval(checkRoles, 24 * 60 * 60 * 1000); // Run every 24 hours
checkRoles();


client.login(token);
wide viper
#

If you're using discord.js v14

Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS

This is wrong. Intents doesn't exist.

#

The official discord.js guide shows you how this stuff works.
https://discordjs.guide/popular-topics/intents.html#privileged-intents

Additionally, if you're following a guide for say, v12,

There's literally a v12 to v13 guide and a v13 to v14 guide which explicitly discusses these changes:

For v12 to v13: https://discordjs.guide/additional-info/changes-in-v13.html#intents

- const client = new Client({ ws: { intents: [Intents.FLAGS.GUILDS] } });
+ const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

And for v13 to v14: https://discordjs.guide/additional-info/changes-in-v14.html#enum-values

- const { Client, Intents } = require('discord.js');
+ const { Client, GatewayIntentBits, Partials } = require('discord.js');

- const client = new Client({ intents: [Intents.FLAGS.GUILDS], partials: ['CHANNEL'] });
+ const client = new Client({ intents: [GatewayIntentBits.Guilds], partials: [Partials.Channel] });

So even if you're using an "outdated" tutorial or something, the official discord.js docs literally have extremely detailed docs covering the changes.

#

Using the official docs in combination with whatever tutorials you're following is an absolute must with discord development.

#

discordjs has the most beginner friendly documentation in terms of technical documentation

#

You didn't post the error you're getting, so I just made some assumptions.