Autorole gives a role to everyone the moment they join — a "Member" role that unlocks channels, or a colour that says "not verified yet". The logic is tiny; the two things that actually matter are enabling the right privileged intent and putting the bot's role in the right place.
| At a glance | |
|---|---|
| You need | a working bot from Host a discord.js bot, and the ID of the role to grant |
| Plan | Free or premium |
| Time | About ten minutes |
Built on the discord.js bot guide. It's the same join event as Welcome new members, so the two combine naturally.
Turn on the Server Members intent first
The bot can't act on a join it never hears about, and join events are a privileged intent.
⚠️ Heads up — this is a privileged intent. In the Discord Developer Portal: your application → Bot → Privileged Gateway Intents → turn on Server Members Intent → save. The code requests
GatewayIntentBits.GuildMembers; with the toggle off, the bot crashes at login withUsed disallowed intents.
The code
The complete index.js. Put the role's ID in .env as AUTOROLE_ID (Developer Mode on → right-click the role in Server Settings → Copy Role ID):
require('dotenv').config();
const { Client, Events, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
],
});
client.once(Events.ClientReady, (c) => {
console.log(`Listening as ${c.user.tag}`);
});
client.on(Events.GuildMemberAdd, async (member) => {
if (member.user.bot) return; // skip other bots if you like
try {
await member.roles.add(process.env.AUTOROLE_ID);
console.log(`Gave the auto-role to ${member.user.tag}`);
} catch (err) {
console.error(`Could not add the role to ${member.user.tag}:`, err.message);
}
});
client.login(process.env.DISCORD_TOKEN);
Your .env:
DISCORD_TOKEN=your-token
AUTOROLE_ID=123456789012345678
Save and restart.
The parts that matter
GatewayIntentBits.GuildMembersis why the join event arrives at all — it pairs with the portal toggle above.Events.GuildMemberAddfires once per join;member.roles.add(id)grants the role.if (member.user.bot) returnskips other bots so they don't get the human "Member" role. Delete the line if you want every account to get it.- The
try/catchturns a permission or hierarchy failure into a readable console line instead of an unhandled crash — useful because the two classic failures (missing permission, role too high) both throw here.
Permissions and intents
| Requirement | Detail |
|---|---|
| Server Members Intent | privileged — enable it in the Developer Portal (callout above) |
| Manage Roles permission | the bot's role needs it to assign the role |
| Role position | the bot's highest role must sit above the role it grants |
⚠️ Heads up: The most common autorole failure isn't the code — it's role order. A bot can only grant a role that sits below its own highest role. In Server Settings → Roles, drag the bot's role above the auto-role. If the console logs "Missing Permissions", this is almost always why.
Making it yours
- Welcome and role in one handler. Since Welcome new members uses the same
GuildMemberAddevent and the same intent, addmember.roles.add(...)to that handler and do both at once. - Verification flow. Give a "Unverified" role on join, then swap it for "Member" after they click a button or pass a check.
- Several roles at once. Pass an array:
member.roles.add(['id1', 'id2']). - Re-role people who were here first.
GuildMemberAddonly fires for new joins. To backfill existing members, loop overguild.members.fetch()once — but that reads the whole member list, so use it sparingly.
Troubleshooting
Used disallowed intentsat startup — the Server Members Intent toggle is off. Turn it on and restart. See Bot appears offline.- Console logs "Missing Permissions" — the bot lacks Manage Roles, or its role is below the auto-role. Fix both in Server Settings → Roles.
- New members get no role and there's no error — the intent isn't really enabled, or
AUTOROLE_IDis wrong. Re-copy the role ID. - It worked for a while then stopped — someone moved a role above the bot's, or the auto-role above the bot's. Check the role order again.