Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
How can my Discord Bot assign a Role to new Users?

First im new to Coding, that means i mostly Copie and Paste things.

To my problem: i do exactly things like in Videos, switch on "SERVER MEMBERS INTENT" in the Discord Dev portal and so on. But my Bot won't assign a Role after someone Joins my DC.

My code looks like a mess BUT! the function i wanted first that the Bot reply "pong" after i type !ping worked finaly after many hours of re-coding stuff.

here my code:

global.Discord = require('discord.js')
const { Client, Intents } = require('discord.js');
const { on } = require('events');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = "!";
const fs = require('fs');

const token = "my token";

client.commands = new Discord.Collection();
client.events = new Discord.Collection();

client.on('guildMemberAdd', guildMember =>{
    let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'anfänger');     

    guildMember.roles.add(welcomeRole);
    guildMember.guild.channels.cache.get('930264184510361670').send(`Welcome <${guildMember.user.id}> to out Server!`)
});

client.once("ready", () => {
    console.log(`Ready! Logged in as ${client.user.tag}! Im on ${client.guilds.cache.size} guild(s)!`)
    client.user.setActivity({type: "PLAYING", name: "Learning Code"})
});

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);
 
    client.commands.set(command.name, command);
}

client.on('messageCreate', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.split(' ').slice(1);
    const command = message.content.split(' ')[0].slice(prefix.length).toLowerCase();

    if(command === 'is'){
        client.commands.get('is').execute(message, args, Discord);
    }
});
 
client.login(token);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Aha! there it is you spelled guildMember as guildmember

client.on('guildMemberAdd', async guildMember => {
    var i = "930263943597928508";
    let role = guildMember.guild.roles.cache.find(r => r.id === i);
    guildmember.roles.add(role); //here replace guildmember with guildMember
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!