Discord Auto role when joining a server - discord.js

I am using the v12 and this is what I have tried so far:
client.on('guildMemberAdd', (guildMember) => {
guildMember.roles.add(
guildMember.guild.roles.find((r) => r.name === 'Newbie')
);
});

Discord JS v12 introduces the concept of managers, you will no longer be able to directly use collection methods such as GuildMember.roles.find. You will now have to directly ask for cache on a manager before trying to use collection methods. (GuildMember.roles.cache.find);
client.on("guildMemberAdd", guildMember => { // Detecting when a GuildMember joins the guild.
const Role = guildMember.guild.roles.cache.find(role => role.name == "Newbie"); // Getting the Role by name.
if (!Role) return console.log("Invalid Role"); // Checking if the Role exists.
guildMember.roles.add(Role.id).catch(error => console.log(error)); // Adding the Role to the GuildMember.
});

Related

DiscordJS v14 fetching server owner but cannot read properties of undefined (reading 'user')

I'm trying to get the owner ID/tag on Discord.JS v14 after adding the bot to the server using this method
module.exports = {
name: 'guildCreate',
execute: async (guild, client) => {
try {
const ownerId = guild.ownerId
const owner = guild.members.cache.get(ownerId);
console.log(owner.user.tag)
} catch (e) {
console.log(e)
}
}
but then I get this error Cannot read properties of undefined (reading 'user')
You can fetch the owner as a GuildMember using <Guild>.fetchOwner() (which returns a Promise that resolves to a GuildMember object). ex.
let owner = await guild.fetchOwner();
console.log(owner.id, owner.tag);

Discord js Cannot read properties of undefined (reading 'add')

Hey I have a little problem.
I wan't to assign a discord role when the bot starts up
I wan't it to work with id's like user id and role id but it is saying
Cannot read properties of undefined (reading 'add')
Thanks in advance Neo
My intents are enabled on discord developer portal
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS],
}); // intents stuff
enter image description here
Guild should not be a variable as it is going to change. Roles need to be fetched from the cache. And then once you have defined guild you can use it without defining it again. Try the code below and you should be good.
client.on('ready' async () => {
const guild = client.guilds.cache.get('976862525801000960')
const role = guild.roles.cache.get('978375133649592361')
const member = await guild.members.cache.get('404645579705024523')
member.roles.add(role).then(
console.log(`TOKEN: `)
)
You don't need to use async/await for getting user from guild. Use get() instead of fetch() that's probably gonna solve your problem.
Thanks Gh0st Fixed it by doing this.
const guild = client.guilds.cache.get('976862525801000960')
const role = guild.roles.cache.get('978375133649592361')
const member = await guild.members.fetch('404645579705024523')
member.roles.add(role).then(
console.log(`TOKEN: `)
)

Why will messageReactionAdd event not work

this.on("messageReactionAdd", async (reaction, user) => {
if(reaction.emoji.name === "✔️") {
console.log("User reacted")
}
})
This is my code and at the moment it will not even run the event
this is defined as client.
My tips: Try if the ready or the message event works. If it doesn't, try removing the if statement in your code. If it still doesn't work try using the starter code from the discord.js guide which is something like this:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('messageReactionAdd', (reaction, user) => {
console.log('event works!');
});
client.login('your-token-here');
If it still doesn't work your bot is most likely not logging in to discord. In this case I'd suggest you to recheck if your bot's token is the right one.

Discord.js How can I make the bot send a message whenever it gets invited to a server?

I just want it to say some simple instructions or information in the chat whenever the bot joins a server.
Anyone know how I could do that?
client.on("guildCreate", guild => {
const channels = guild.channels.cache.filter(channel => channel.type == "text");
channels.first().send("Thank you for inviting me!").catch(e => console.log(e));
});

Discord.js send message if have a role

I want to send dm to anyone who has the role of "xxx" when the bot starts. Can you help me?
You can do it with this code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
const list = client.guilds.get("guild ID");
list.members.forEach(member => {
if (member.roles.some(role => role.name === 'Your role')) {
member.send('Your message')
}
});
});
The way it works is when the bot is ready it will check each member if they have the specified role and if they do then the bot will send it a DM.
A tip, when asking questions like this provide more information on what the problem is, what you've tried and you're current code.

Resources