I've got an issue when trying to add a role to a member programmatically. member.roles.add is throwing the following error:
(node:9420) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
I'd tried to pass in the id and name of the role to the add method but this did not work either. What am I doing wrong here? Any pointers in the right direction would be much appreciated.
Below is the functionality to apply the role itself (within its context) for better understanding of my issue:
bot.on('message', msg=>{
if(msg.content.match(/[`!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/)) {
if((msg.content.match((new RegExp(/[`!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/, "g")) || []).length) >= 30) {
msg.channel.send(`${"<#" + msg.author.id + ">"}. Your message has been deleted for containing too many special characters.`);
const guild = bot.guilds.cache.get(GUILD_ID_HERE);
let mute_role = msg.guild.roles.cache.find(role => role.name === 'spammer');
var member = guild.members.cache.get(msg.author.id);
member.roles.add(mute_role);
msg.delete();
}
}
...
Related
code:
if(command === "rusr"){
console.log((chalk.yellow)`You ran a command: rusr`)
const id = args[0]
const role = args.slice(1).join(" ")
let user = message.guild.member(message.mentions.users.first()) || message.guild.members.get(id)
user.roles.add(role)
}
error:
UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
this is strange cuz i have the EXACT same code but with nickname which works just fine. anyway to fix this?
The error is in const role = args.slice(1).join(" "). That's not the role, thats the name of the role. Instead make it something like
const role = message.guild.roles.cache.find(r => r.name == args.slice(1).join(" "));
So that finds the role with the name specified.
It'll work without, but its good practice to also add if(!role) return message.channel.send('Invalid role!'); after the line where you define role. This means if someone enters the name of a role that doesn't exist, it will stop and tell the user instead of breaking the code.
if(message.content == `${config.prefix}mods`) {
const ListEmbed = new Discord.MessageEmbed()
.setTitle('Mods:')
.setDescription(message.guild.roles.cache.get('813803673703809034').members.map(m=>m.user.tag).join('\n'));
message.channel.send(ListEmbed);
}
Hey so iam making a command which displays all the members with that role but it only seems to be sending 1 of the mods
await message.guild.roles.fetch();
let role = message.guild.roles.cache.find(role => role.id == args[0]);
if (!role) return message.channel.send('Role does not exist'); //if role cannot be found by entered ID
let roleMembers = role.members.map(m => m.user.tag).join('\n');
const ListEmbed = new Discord.MessageEmbed()
.setTitle(`Users with \`${role.name}\``)
.setDescription(roleMembers);
message.channel.send(ListEmbed);
};
Make sure your command is async, otherwise it will not run the await message.guild.roles.fetch(); - this fetches all roles in the server to make sure the command works reliably.
In Discord.jsV12 the get method was redacted and find was implemented.
Aswell as this, I would highly recommend defining variables to use in embeds and for searching since it is much more easy to error trap.
If you have many members with the role, you will encounter the maximum character limit for an embeds description. Simply split the arguments between multiple embeds.
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.
I'd like to auto set roles for new users when they join server X, problem is that this bot is on server Y as well, and server Y doesn't have the role.
client.on('guildMemberAdd', member => {
console.log('User ' + member.user.tag + ' has joined Steampunk.');
var role = member.guild.roles.find(x => x.name === "name");
member.addRole('247442955651121154');
})
I was hoping that I could do a simple check before applying the role, if user have joined server X, add the role, if else, do nothing. So far my attempts have failed.
Any input would be appreciated!
The original code is trying add a specific role you put in, and not the role you just looked up a line before that. There shouldn't be any issues with other servers the bot is in, because member.guild gives the guild of the member object, and since this member object is coming from a guildMemberAdd it means that member.guild will only return the guild that was just joined. The code in the edit has a lot of strange things going on, though.
The first issue I see is that in order to look through the channels in a guild, you have to use guild.channels.cache.find(), guild.channels.find() is old now and doesn't work in v12. Same goes for client.guild, guild.roles, etc. The part at the bottom of your edited code doesn't seem to make much sense at all. The code should get the roles of the newly joined guild (member.guild.roles.cache), look through them to find the role you want (you did this correctly with the .find()) and then apply the role to the member. Neither server nor role ever get used in your code, and guild.id is a property, not a method.
Let me know if you need help following these instructions, although it looks like you should be able to do it now. Always look at the discord.js docs first to check properties, methods, etc.
I ended up defining the guild in question by name, saved it as a const, and made a check for the specific guild.
If your bot is on several servers but you only want the greet/addRole on a specific one, all integrated into the message displayed on joining;
//Welcome message and autorole for specific server and channel.
client.on('guildMemberAdd', member => {
const = client.guilds.find(guild => guild.name === "Steampunk");
if (!guild) return;
const channel = member.guild.channels.find(ch => ch.name === "greetings");
if (!channel) return;
let botembed = new Discord.RichEmbed()
.setColor("#ff9900")
.setThumbnail(member.user.avatarURL)
.setDescription(`${member} Welcome to the Ark Steampunk Sponsored Mod Discord!\nPlease report all <#244843742568120321> and if u need any help view or ask your question in <#334492216292540417>. Have any ideas we got a section for that too <#244843792132341761>. Enjoy!:stuck_out_tongue: Appreciate all the support! <http://paypal.me/ispezz>`)
.setTimestamp()
.setFooter("Steampunk Bot", client.user.avatarURL);
channel.send(botembed);
console.log('User ' + member.user.tag + ' has joined ' + member.guild.name + '.');
var role = member.guild.roles.find(x => x.name === "name");
member.addRole('247442955651121154');
});
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.