I want to do a mute command, but when I do the command, the console output is (node:67916) UnhandledPromiseRejectionWarning: TypeError: msg.guild.roles.get is not a function
Any ideas how i can fix that?
As the error message says, msg.guild.roles.get is not a function. Its quite hard to answer a question without seeing the code, you should always share your code, but the correct way to find and add a role is the following:
const role = message.guild.roles.cache.find(role => role.name === 'Muted');
const member = message.mentions.members.first();
if(!member) member = message.author;
member.roles.add(role);
Since the update to V12, it is important that when trying to get roles, members or guilds you must include the .cache bit.
Related
I am trying to make my bot's role a pink color for the current server using the Client.on("ready") However whenever I run the bot with what I currently have the console returns:
(node:6504) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'cache' of undefined
Here is my code I am currently using. I know I need to for loop on every guild but I'm not sure how I would do that, I could use a for or just a .forEach however I can't find the correct way to do such.
const role = client.guild.roles.cache.get('Aiz Basic+');
role.edit({ name: 'Aiz Basic+', color: '#FFC0CB' });
In advance thank you to anybody who replies and helps with my message.
Client#Guild Isn‘t existing. Only Client#Guilds. You can fix this problem by looping through the cache of all your guilds with:
client.guilds.cache.forEach((g) => { })
And inside the loop you can use your code after fixing the .get function, because the get function needs a role id not a role name. You could find the role via the name by writing:
const role = <Guild>.roles.cache.find((r) => r.name === /* The role name */);
Make sure you replaced <Guild> with your guild variable.
Try this code if client.guild is not defined, it could also be because you are using bot instead of client
let role = message.guild.roles.cache.find(role => role.name === "Aiz Basic+");
I am making an addrole command for my bot, but everytime someone gets a role wrong it give an error. I don’t know how to fix it so please help
Code:
if (!message.content.startswith(prefix) || message.author.client) return;
const arguments = message.content.slice(prefix.length).split(‘‘);
const command = arguments.shift().toLowerCase();
if (command === ‘addrole’) {
const suggestedRole = arguments[1] // role from the message
const role = message.guild.roles.cache.find(role => role.name === suggestedRole) // trying to find the role
if(!role) message.channel.send(‘Role does not exist!’) // if the role doesn’t exist
const target = message.mentions.members.first() // target
if(!target) message.channel.send(‘User not found!’) // if no target
target.roles.add(role) // adds the role to the target
}
Now you might be thinking ‘What if the role has two words’, I’m working on that, I just need help so that if the role doesn’t exist it doesn’t give an error, it just sends ‘Role not found’. Please help me, thanks. Sorry for lack of detail, don’t know what else to add.
you could actually use try catch, which might seem a lot simpler to somebody who is not familiar with promises. that would look like
try{
//will attempt to run the code in this try block
const target = message.mentions.members.first() // target
if(!target) message.channel.send(‘User not found!’) // if no target
target.roles.add(role) // adds the role to the target
} catch {
//will run if an error occurs in the try block
message.channel.send(‘Role does not exist!’)
}
this works for pretty much the same reason my other solution does, but it is a bit different. i'm not going to explain it in depth like I did the last time, so instead, here's a simple explanation on try catch blocks and what they do: https://www.w3schools.com/js/js_errors.asp.
message.guild.roles.cache.find will return a promise, so you need to deal with that promise before accessing the role. one way of doing this is with .then and .catch.
message.guild.roles.cache.find(role => role.name === suggestedRole)
.then((role)=>{
//what to do if the role does exist using the role parameter
}).catch(()=>{
//what to do if the role does not exist, in your case,
message.channel.send(‘Role does not exist!’)
})
the reason why we need to do this is because as I said, cache.find() will return a promise. a promise can either return as fulfilled, or as rejected. if the role you are searching for does not exist, you will receive an unhandled promise rejection, which in some cases can crash your program. to stop this, we need to account for the different states that your promise can be in. we do this with .then and .catch. if your promise is fulfilled, the .then block is called. if it is rejected, the .catch block is called. with the .catch block, your code now knows what to do if it encounters an error, and in this case we are telling it to send a message saying "role not found.". I hope this code helps and that the explanation helped you understand some important concepts behind promises.
This code will check if the role exists, if not sends an error message, and if exists it add the role to the member and will send "Can't add role" if an error occurs.
const role = message.guild.roles.cache.find(role => role.name === 'roleName');
if(!role) return message.channel.send('Role not found.')
const member = message.member;
member.roles.add(role).catch(() => { message.channel.send('Can\'t add role.') });
Pretty much the title. Every time I try adding a role to someone using this:
const member = message.author;
member.roles.add('732727208774205460');
I end up with this TypeError:
TypeError: Cannot read property 'add' of undefined
However, if I use it like this:
const member = message.mentions.members.first();
member.roles.add('732727208774205460');
It works completely fine. Problem is, that only works if the person I'm adding a role to was mentioned/pinged by the user. I'm trying to add a role to the user himself where the user doesn't ping anyone (for censorship, mainly). Is there a way I could do this without getting the TypeError?
You are receiving an error because message.author.roles is undefined, so trying to use .add() on it causes an error.
message.author is of type User, which represents a discord user, and is not associated with any particular server.
You're looking instead for message.member which has type GuildMember, which represents a particular user's profile inside of a guild (message.member is just message.author but as a GuildMember instead of a User). It is the GuildMember type that has roles.
Solution:
const member = message.member;
member.roles.add('732727208774205460');
Official Documentation:
User,
GuildMember
im trying to make a change nickname command
if(isValidCommand(message, "changenick")){
try {
if (mention == null){return message.reply("changenick who? dumb dumb")}
nickname = message.content.slice(8 + mention);
let member = message.mentions.members.first();
member = await member.setNickname(nickname);
}
catch (e) {console.error(e);
return message.channel.send("something went wrong!");}
}
but i get the error DiscordAPIerror: missing permissions even when trying it in my own server
I would suggest the following:
Make sure the bot itself has the MANAGE_NICKNAMES permission.
A user can only change the nickname of a person below them in the role hierarchy, this applies to bots as well.
My discord bot gives the role of 'Patreon' to my patreon supporters. This role is given on my main discord bot server. So right now I'm trying to write some commands that would be only available to users who have the role 'Patreon' in the BOTS discord server, how can I accomplish this?
Like is there a way I can be like -
message.member.has('Patreon Role').in('My Discord Server)?
Let's go over the tasks you need to accomplish this.
Get the "home guild" with your users and corresponding Patreon role.
See Client.guilds and Map.get().
Find the user in the guild.
See Guild.member().
Check whether or not the user has the Patreon role.
See GuildMember.roles and Collection.find().
You can define a function to help you out with this, export it and require it where you need it (or define it within relevant scope), and then call it to check if a user is one of your Patreon supporters.
Here's what this function would look like...
// Assuming 'client' is the instance of your Discord Client.
function isSupporter(user) {
const homeGuild = client.guilds.get('idHere');
if (!homeGuild) return console.error('Couldn\'t find the bots guild!');
const member = homeGuild.member(user);
if (!member) return false;
const role = member.roles.find(role => role.name === 'Patreon');
if (!role) return false;
return true;
}
Then, as an example, using this function in a command...
// Assuming 'message' is a Message.
if (!isSupporter(message.author)) {
return message.channel.send(':x: This command is restricted to Patreon supporters.')
.catch(console.error);
}
message.member.roles.find('name', 'Patreon Role');//this returns either undefined or a role
What that does is it searches the users collection to see if the have "Patreon Role"
If the message is on the same server, otherwise you could do
client.guild.find('name','My Discord Server').member(message.author).roles.find('name', 'Patreon Role'); //this also returns undefined or a role
Clearly that second option is long, but what is basically does is searches the servers the bot is in for a server called 'My Discord Server' then it finds the GuildMember form of the message.author user resolvable, then it searches their roles for the role 'Patreon Role'
There is a chance it will crash though if they aren't on the server(the documentation doesn't say if it returns and error or undefined for some reason) so if it does crash you could instead do
client.guild.find('name','My Discord Server').members.find('id', message.author.id).roles.find('name', 'Patreon Role'); //this also returns undefined or a role
You can read more here: https://discord.js.org/#/docs/main/stable/class/User
and here
https://discord.js.org/#/docs/main/stable/class/Client
and here
https://discord.js.org/#/docs/main/stable/class/Guild
To try and give a full example, assuming this is in your message event
if (message.member.roles.find(r => r.name === 'Patreon') == undefined &&
commandIsExclusive || message.guild.id !== 'this-id-for-BOTS-server') {
// Don't allow them in here
}
Essentially, to run a command they must be a supporter, in a specific server and if it is exclusive and the other criteria aren't met, they are denied