member.roles.add is not a function - javascript

I don't know why but I get this error in the console when someone joins the server and doesn't want to give it a role
Error:
TypeError: member.roles.add is not a function
let roleID = "1005089670629175439";
client.on("guildMemberAdd", (member, roleID) => {
member.roles.add(roleID);
console.log("Mistic BOT | Added role for new user");
});

First check if you enabled privileged intents on discord developer portal.
This is how you do it
Make sure you're logged on to the Discord website.
Navigate to the application page.
Click on the bot you want to enable privileged intents for.
Navigate to the bot tab on the left side of the screen.
Scroll down to the “Privileged Gateway Intents” section and enable server members intent.
On your code you don't need to pass the roleID if its in the same file just simply use something like this
client.on("guildMemberAdd", (member) => {
let roleID = "1005089670629175439";
member.roles.add(roleID);
console.log("Mistic BOT | Added role
for new user");
});

Related

discord bot is not sending welcome massage

I want to send a welcome message to a new user but the code doesn't work. I tried defiant articles and video tutorials and already asked questions for help but it's not working. I already checked ( Privileged Gateway Intents > PRESENCE INTENT ) and ( Privileged Gateway Intents > SERVER MEMBERS INTENT ) here
here's my code
// Instantiate a new client with some necessary parameters.
const client = new Client(
{ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }
)
const channelId = "969129834682929212";
const rulesChannel = "969129988299304960";
client.on("guildMemberAdd", (member) => {
console.log(member);
const message = `Welcome <#${
member.id
}> to our server! Be sure to check out our ${member.guild.channels.cache
.get(rulesChannel)
.toString()}`;
const channel = member.guild.channels.cache.get(channelId);
channel.send(message);
});
I think you are facing this issue is because of recent gateway changes of the Discord Api that you need to enable the intents. Here is a fix for you –
Head over to Discord Developers Portal
Choose your application
Inside the bot section if you scroll a little bit down , you will see a section named Privileged Gateway Intents
Enable the SERVER MEMBERS INTENT and restart the bot and your bot will start recieving the guildMemberAdd event!
Learn more about intents at Discord.js

Why am I getting an undefined when trying to get a Guild in Discord.js?

I'm pretty new to working on Discord bots, but I'm trying to create a bot for Discord that gets all the members in a guild, so they can be used later on. But everytime I try to get the guild with its ID it gives an undefined back, even though I know I put in the right ID.
const Discord = require("discord.js");
const client = new Discord.Client();
const members = client.guilds.cache.get("guildID");
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
console.log(members);
});
client.login('token');
Am I doing something wrong?
You aren't being very clear. You stated you want your bot to get all members in a guild, but you proceed to get a guild ID instead.
Assuming you want to get all members in a guild. First thing you need is Privileged Gateway Intents enabled in your bot and implemented in your code.
Step 1: Enable Intents
Go to your Bot App Dev
Select your bot
Head to the Bot section of your bot and scroll down till you see "Privileged Gateway Intents" and select both "PRESENCE INTENT" and "SERVER MEMBERS INTENT
"
Example
Step 2: Implementing In Code
const Discord = require('discord.js');
const client = new Discord.Client({ ws: { intents: new Discord.Intents(Discord.Intents.ALL) }});
^^^^^^^
This is what is making you access all server members within a guild
Code for getting all server members:
// If prefix not set: do this: const prefix = "!"; whatever you want
const prefix = "!";
// Usernames
const members = message.guild.members.cache.map(member => member.user.tag);
// ID's
const members = message.guild.members.cache.map(member => member.user.id);
if(message.content.startsWith(prefix + "test")){ // Gets all members usernames within the guild this message was sent on.
console.log(members) // Logs all each members username
}
In depth but I hope it helps.
I believe it's just:
client.guilds.get("guildID");
and not:
client.guilds.cache.get("guildID");
Try using this instead:
client.guilds.fetch("guildID");
Also, I recommend you put that line in your ready event or somewhere where you are sure the client has fully logged in when ran to make sure that it is logged in when you run that.
Before client is ready, use client.guilds.fetch('<guild id>') which returns a promise. Note that before the ready event is triggered many things on the client are uninitialized such as client.user
Put the const members = client.guilds.cache.get("guildID"); inside the ready event.
Try:
const members = client.guilds.cache.get("guildID");
list.members.cache.forEach(member => console.log(member.user.username));

code to console log members joining isnt working in discord.js

I have been half following a tutorial, half working it out on my own how to code a discord bot in discord.js. I am up to the point where i want to add a welcome message, and after doing the same thing as the person making the tutorial the code isn't console logging a joining member to the server. Here is the code that i have done, and in the tutorial the code would send a welcome message to the selected channel and console log the member.
there is no error message
module.exports = client => {
const channelId = '790887807127650304' // welcome channel
client.on('guildMemberAdd', member => {
console.log(member)
const message = `welcome <#${member.id} to the server!`
const channel = member.guild.channels.cache.get(channelID)
channel.send(message)
})
}
also, for reference, here is the tutorial i have been following;
video
Make sure the gateway intents are enabled in your dev portal for your bot, it's in the bot tab (the same page where you copy the bot's token). This is a change made after the tutorial you linked.
If you want to know why this is required, read this
Also there are a few errors in your code. You typed channelID two different ways and you didnt put '>' after <#${member.id}.
Here it is fixed.
module.exports = client => {
const channelID = '790887807127650304' // welcome channel
client.on('guildMemberAdd', member => {
console.log(member)
const message = `welcome <#${member.id}> to the server!`
const channel = member.guild.channels.cache.get(channelID)
channel.send(message)
})
}

Discord.js Bot Welcomes Member, Assign a Role and send them a DM

So When A New Member Joins The Guild [the discord server]. The Bot Should Send A Message In a certain Channel (ID = 766716351007686696), Send Them A Direct Message, And Then Add A Role (Human Bean). This Is The code I Have Now and it isn't working, error at the bottom
client.on('guildMemberAdd', member =>{
const channel = message.guild.channels.cache.find(c => c.id === "766716351007686696")
const channelwelcomeEmbed = new Discord.MessageEmbed()
.setColor('#ffd6d6')
.setTitle('Welcome!')
.setDescription(`${member} just joined the discord! Make sure to read #rules!`)
.setTimestamp();
channel.send(channelwelcomeEmbed);
const dmwelcomeEmbed = new Discord.MessageEmbed()
.setColor('#ffd6d6')
.setTitle('Welcome!')
.setDescription("For Help Using #Pro Bot#7903, Send The Command `!help` In Server")
.setTimestamp();
member.send(dmwelcomeEmbed);
let role6 = message.guild.roles.cache.find(role => role.name == "Human Bean"); //BASIC ROLE, EVERYONE GETS IT
if(!role6) return message.reply("Couldn't find that Role .")
member.roles.add(role6);
});
Error Message is;
const channel = message.guild.channels.cache.find(c => c.id === "766716351007686696")
^
ReferenceError: message is not defined
Your code looks fine, the problem is that the event isn't triggered. Thats because discord turned off "privileged intents" by default.
Some intents are defined as "Privileged" due to the sensitive nature of the data. Those intents are:
GUILD_PRESENCES
GUILD_MEMBERS
One effect of that is what you are experiencing, the not working guildMemberAdd event.
The good news is that you can fix this with one easy step. Simply enable Privileged Gateway Intents in the discord developer portal and it should work just fine.
If you want to read more about it
Discord.js Official Guide - Gateway Intents
Discord Developer Documentation - Gateway Intents
Gateway Update FAQ
Discord API Github - Issue 1363 - Priviledged Intents
Discord Blog - The Future of Bots on Discord
None of my discord.js guildmember events are emitting, my user caches are basically empty, and my functions are timing out?
Fix: const channel = member.guild.channels.cache.get('CHANNEL ID')
You need to use member instead of message. Because guildMemberAdd function using member.
client.on('guildMemberAdd', member => {

Checking if my bot has permissions to manage roles

So, the title briefly explains what I want.
When a member joins my Discord server, I want the bot to assign them the User role automatically. Before that happens, I want to verify if the bot has permission to manage roles and so far I have the following in my code.
/** Event will trigger when a user joins the server **/
bot.on("guildMemberAdd", function(member) {
if (!bot.user.hasPermission("MANAGE_ROLES")) {
var embed2 = new discord.RichEmbed()
.setTitle("Error")
.setDescription("The bot doesn't have the appropriate permissions to assign new members roles automatically.\nTo resolve this problem, go to **Server Settings** and then navigate to the **Roles** option. Next, click on **Bots**, and then click on the slider next to **Manage Roles** to activate it.")
.setColor("#3937a5")
bot.channels.get("466878539980079105").send(embed2);
} else {
member.addRole(member.guild.roles.find("name", "User"));
}
});
If the bot doesn't have the permissions, it will get the attention of staff by posting an alert to the #alert channel.
However, I attempt to join with a new Discord account, it reports in the console that hasPermission isn't a function, and then stops.
Would there be any suggestions on how to resolve this code?
On Discord.js we have users and members. Users is a user of Discord. Member is a member of a Discord Server. The user object doesn't have information about roles, nicknames, permissions. Members do, because they are related to that server.
So if you want to grab the bot as the member of the guild you need to use something like this:
<guild>.me
This will return the member object of the bot from the selected guild.
When a member is added to the guild you get the GuildMember object, and with that you also get the guild. So you can use this:
member.guild.me
And finally to check the bot has permissions:
if(member.guild.me.hasPermission("MANAGE_ROLES"))
Note:
hasPermission() and hasPermissions() will be deprecated in DiscordJS v13 and replaced with permissions.has()
e.g : member.permissions.has(Permissions.FLAGS.SEND_MESSAGES);
the error message is correct, user has no function called hasPermission. but the role property does.
https://discord.js.org/#/docs/main/stable/class/Role?scrollTo=hasPermission

Categories