0
votes

I'm pretty new to discord.js but I'm trying to make a bot give roles based on time elapsed since the user joined (the bot's role is above the needed roles and the bot itself has admin permissions)

client.on("guildMemberAdd", (member) => {
    member.roles.add(801917861110874122);
});

client.on("message", (message) => {
    if(Date.now() - message.member.joinedAt < 1000*60*60*24*1) {
        message.member.roles.add(801917434558808074);
    }
});

Basically, the bot doesn't give the role neither when the user joins nor when the wanted amount of time passes.

1

1 Answers

0
votes

You need to pass a role to .add(), which can be done by looking up the ID from the cache.

Here is an example:

let role = message.guild.roles.cache.get("801917861110874122");
member.roles.add(role);

You can also search through roles using the name instead of the ID like this:

let role = message.guild.roles.cache.find(r => r.name == "Role name");
member.roles.add(role);

Edit:

To make your time function work, you need to use message.member.joinedTimestamp instead of message.member.joinedAt, like this:

client.on("message", (message) => {
    if(Date.now() - message.member.joinedTimestamp < 1000*60*60*24*1) {
        message.member.roles.add(801917434558808074);
    }
});