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);
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
})