Issues with autorole using Quick.db (Discord.js) - javascript

I'm trying to make an autorole code using quick.db, but it returns the error: UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes
My "setautorole" command:
const role = message.mentions.roles.first() || message.guild.roles.cache.get(args[0])
if(!role) return message.channel.send('I couldnt find the role')
db.set(`autorole`, role)
message.channel.send('The process worked fine!')
This is on index of bot:
client.on("guildMemberAdd", (member) => {
let few = db.get(`autorole_${member.guild.id}`)
if(few === null) return;
member.roles.add(few)
})
Well, I don't know what to do to fix this error, I need a little help

It's better just to save the role ID in Database
BTW you're doing it all wrong. It should be like
setautorole.js
const role = message.mentions.roles.first() || message.guild.roles.cache.get(args[0]);
if(!role){
return( message.channel.send('I couldnt find the role') );
}
db.set(`autorole_${message.guild.id}`, role.id);
message.channel.send('The process worked fine!');
index.js
client.on("guildMemberAdd", (member) => {
let roleID = db.get(`autorole_${member.guild.id}`)
if(!roleID) return;
role = member.guild.roles.find(roleID);
if(!role){
console.log("That role dosen't exist");
return (false);
}
member.roles.add(role)
})

Thanks for the idea Akio, but, i i did something like:
client.on("guildMemberAdd", (member) => {
let roleID = db.get(`autorole_${member.guild.id}`)
if(!roleID) return;
let role = member.guild.roles.cache.find(r => r.id === roleID);
if(!role){
console.log("That role dosen't exist");
return (false);
}
member.roles.add(role)
})
and worked, thanks for help :)

Related

Bot assigns user a role, even if user doesn't own role

Sorry for the confusing title, I'll clarify. I'm trying to make the bot check if a user has a certain role in their quick.db inventory, and if they do, it'll equip that role. The problem I'm having is that even with the role in the inventory, it returns the error that the role isn't owned. I have a feeling that the problem is the if (db.has(message.author.id + '.hot rod red')) line, as I'm not too sure how to format checking for a role with quick.db. Sorry for the messy code, if anyone knows how to fix this let me know, thanks!
if (db.has(message.author.id + '.hot rod red')) {
if (message.member.roles.cache.some(role => role.name === 'hot rod red')) {
let embed = new Discord.MessageEmbed().setDescription('You already have this role equipped!');
return message.channel.send(embed);
} else {
await message.guild.members.cache.get(user.id).roles.add('733373020491481219');
let embed = new Discord.MessageEmbed().setDescription(`You now have the ${message.guild.roles.cache.get('733373020491481219')} role!`);
message.channel.send(embed);
user.roles.remove(user.roles.highest);
}
} else {
let embed = new Discord.MessageEmbed().setDescription('You do not own this role!'); // ERROR HERE; GIVES ROLE EVEN WITHOUT OWNING
return message.channel.send(embed);
}
Probably try something like
let check = db.get(message.author.id+'.hot rod red')
and check if it is true/false, I'd say to use string rather then boolean as you can use if(check === 'false/true'){}. You can also do
if(!check || check === 'false'){ return
let embed = new Discord.MessageEmbed().setDescription('You do not own this role!');
return message.channel.send(embed);
}
So the final code would be:
let check = await db.get(message.author.id + '.hot rod red');
let rejectEmbed = new Discord.MessageEmbed()
.setDescription('You do not own this role!');
if(!check){
return message.channel.send(rejectEmbed)
}
if(check === 'true'){
if (message.member.roles.cache.some(role => role.name === 'hot rod red')) {
let embed = new Discord.MessageEmbed().setDescription('You already have this role!');
return message.channel.send(embed);
} else {
await message.guild.members.cache.get(user.id).roles.add('733373020491481219');
let embed = new Discord.MessageEmbed().setDescription(`You now have the ${message.guild.roles.cache.get('733373020491481219')} role!`);
message.channel.send(embed);
user.roles.remove(user.roles.highest); // I'm unsure why this is here, this could be causing a potential error
}
}
If there are any more questions, please comment!! I hope this has helped.

Discord JS self nickname command

hello guys iam trying to make self role and nickname change command but its not working i dont know where is the problem i earn the role but name not changing can someone tell me what is wrong on my code
client.on('message', (message,member )=> {
if (message.content.toLowerCase() === '*Test') {
if(!message.channel.guild) return;
message.member.addRole(message.guild.roles.find(role => role.name === "Test"));
let member = message.member; //message.guild.members.cache.get(user.id);
let nick = "[PRO] "
// message.guild.member(r=>r.setNickname(nick + r.user.username));
member.setNickname(nick + member.user.username);
}
});
To set Discord nickname. You can use message.member.setNickname("new member") .
as example :
if you want to create #setnick [nickname] and [nickname] is your args.
and you can use this example with role for member
client.on('message', async message => {
let messageArray = message.content.split(" ");
let args = messageArray.slice(1);
var argresult = message.content.split(` `).slice(1).join(' ');
if(message.channel.type === "dm" || message.author.bot) return;
if(message.content.toLowerCase().startsWith('#setnick')) {
if(!message.guild.me.hasPermission('MANAGE_NICKNAMES')) return message.reply('I dont have Permission to do this action.!');
try {
if(!args[0]) {
message.member.setNickname(message.author.username)
} else {
message.member.setNickname(argresult, "Member wants to change nickname")
}
} catch(error) {
return console.error('[ SET_NICKNAME ] Error')
}
}
})

UserInfo Discord Bot Command. TypeError: Cannot read property 'roles' of null

My command: it does what it has to do. I can mention a user or use his id
let args = message.content.split(' ');
if (args.length > 2) return message.channel.send('Only mention one user!');
if (!args[1]) return message.channel.send('Mention someone!');
if (args[1]) {
let member = message.guild.member(
message.mentions.members.first() || message.guild.members.cache.get(args[1])
);
let roles = member.roles.cache
.filter((r) => r.name !== '#everyone')
.map((role) => role.name)
.join('\n');
if (roles.length === 0) roles = '-';
if (member) {
let embed = new Discord.MessageEmbed()
.setColor(tesseract.Constants.COLORS.TEAL)
.setTitle('User Info')
.setThumbnail(member.user.displayAvatarURL())
.setAuthor(
`${member.user.tag} (${member.id})`,
member.user.displayAvatarURL()
)
.addField('**Username:**', `${member.user.username}`, true)
.addField('**Discriminator:**', `${member.user.discriminator}`, true)
.addField('**ID:**', `${member.user.id}`, true)
.addField('**Status:**', `${member.user.presence.status}`, true)
.addField('**Joined On:**', `${member.joinedAt.toLocaleString()}`, true)
.addField(
'**Created On:**',
`${member.user.createdAt.toLocaleString()}`,
true
)
.setDescription(roles)
.setFooter(
`© ${message.guild.me.displayName}`,
this.client.user.displayAvatarURL()
);
message.channel.send(embed);
} else {
message.channel.send(`Could not find that member`);
}
}
Now i want to add that i can search for a user outside the discord. (Bot is added to 2 discords) So i want to do the user command on the second discord and search for a user from the first discord. i get this done with client.users.cache.get('user id') but due to the roles function i get a TypeError: Cannot read property 'roles' of null. If i remove the role funcion it works fine..
How can I ignore the role function when its searching for client.users.cache.get('user id')
The error is telling you that member is null. This happens when args[1] is not a valid ID or the user is not in the guild.
Check that a valid member was supplied before getting the member's roles:
// You don't need to wrap this in a message.guild.member()
let member = message.mentions.members.first() || message.guild.members.cache.get(args[1]);
if (member) {
let roles = member.roles.cache
.filter((r) => r.name !== '#everyone')
.map((role) => role.name)
.join('\n');
// rest of code...
}

Discord.js how to add role to a specific user ID?

I'm trying to create a bot for my friends and I only so I hardcoded our discord ID's. Now I'm trying to make the bot assign us roles using our IDs.
Here's what I have so far and it's giving me an error.
bot.on('ready', () => {
const guild = bot.guilds.get("OUR SERVER ID");
let mcRole = guild.roles.find(x => x.name === "Minecraft Players");
let johnMember = guild.members.get("id", "JOHN'S ID")
johnMember.addRole(mcRole);
})
Error says:
johnMember.addRole(mcRole);
^
TypeError: Cannot read property 'addRole' of undefined
I do bot.on('message', message Because ready Not Working. I hope it works
bot.on('message', message =>{
if(!message.guild) return;
if(!message.guild.member(bot.user).hasPermission('MANAGE_ROLES')) return;
let johnMember = message.guild.members.get('user ID');
let mcRole = message.guild.roles.find('name', "Minecraft Players");
if(!mcRole) return;
if(!johnMember) return;
if(johnMember.roles.has(mcRole.id)) return;
johnMember.addRole(mcRole.id);
});
You need to send any message to run this command.

First name mentions in a text using discord.js

I am trying to make some sort of kick system. I would like to know how I would get the first name mentioned in a text.
client.on("message", (message) => {
if (message.member.hasPermission(["KICK_MEMBERS"],["BAN_MEMBERS"])){
if(message.content == "!kick"){
let member = message.mentions.members();
console.log(member)
member.kick("You have been kicked").then ((member) => {
message.channel.send( member.displayName + " has been Kicked!");
})
}
}
});
No error is thrown that I know of.
First off, if you want to check multiple permissions in GuildMember.hasPermission(), you need to pass an array. The way your code is written now, you're passing an array with "KICK_MEMBERS" as the permissions to check and an array with "BAN_MEMBERS" for the explicit parameter.
Solution: message.member.hasPermission(["KICK_MEMBERS", "BAN_MEMEBRS"])
Secondly, you're declaring member as a Collection, when it should be a GuildMember.
Solution: const member = message.mentions.members.first()
client.on("message", async message => {
if (message.content === "!kick" && message.member.hasPermission(["KICK_MEMBERS", "BAN_MEMBERS"])) {
try {
const member = message.mentions.members.first();
if (!member) return await message.channel.send(`No user mentioned.`);
await member.kick(`Kicked by ${message.author.tag}`);
await message.channel.send(`${member.user.tag} has been kicked.`);
} catch(err) {
console.error(err);
}
}
});

Categories