Give role in specific message sent - discord

So I want to give a role for sending specific number of message
Like if they manage to send 1k messages they will get a specific role
Can anyone help me on that?

So if you know the role id then
roleID = "100000000000000001";
let member = message.mentions.members.first();
if(memberHasEnoughMessage && !member.roles.get(roleID)){
let role = Guild.roles.cache.get(roleID);
if(member && role){
member.roles.add(role);
}
}
if you don't know the role id and only know the name then
const role = Guild.roles.cache.find(role => role.name === '10k message');
If you want to create a new role (let's say with permission to ping everyone) then
Guild.createRole( {name:"10k", color: "#f6f61", permissions:["MENTION_EVERYONE"] } );
So the final code should be like
roleID = "100000000000000001";
let member = message.mentions.members.first();
if(memberHasEnoughMessage){
/*
If you already know the role ID
let role = Guild.roles.cache.get(roleID);
*/
let role = Guild.roles.cache.get(roleID);
ler role = Guild.roles.cache.find(r=> r.name === '1k');
if(!role){
await Guild.createRole( {name:"Mentioners", color: "#ff0000", permissions:["MENTION_EVERYONE"] } ).then( (newRole) =>
role = rnewRole;
}).catch(console.log);
}
if(member && role){
member.roles.add(role);
}
}

Related

discord.js#12.5.3 add role command

I'm trying to make a add role and remove role command, I've tried before and given up since I could never get my head around it. I want the command to allow someone to mention a user then mention a role and give/take that role to/from the user. The command doesn't give the role and it seems like content.replace doesnt work either. Here is my code for the give role command:
module.exports = {
name: "addrole",
description: "Give a role to a user",
aliases: ["+role", "giverole", "give-role"],
cooldown: 3,
permissions: "MANAGE_ROLES",
execute(msg, args){
const Discord = require('discord.js');
require("discord-reply")
const { colour } = require('../config.json')
const { version } = require('../config.json')
const { prefix } = require('../config.json')
let user = msg.mentions.users.first() || msg.author
//v e.g $ addrole #user test role = test role
let r1 = msg.content.replace(`${prefix} ${args[2]} ${user} `, ``)//removes everything before role name
//^ content.replace doesn't seem to work
let role = msg.guild.roles.cache.find(r => r.name === r1);
if (!r1) { //checks for anything after the user
msg.channel.send("You need to give a role name")
} else if (!role) {
msg.channel.send("That role doesn't exist")
} else return
user.roles.add(role).catch(console.error);
console.log(`Made it this far`)
}
}
In my modest experience I think your args[2] does not exist because, according to your snippet, you should've already created a messageCreate event and defined command. So try using:
let r1 = msg.content.replace(`${prefix}role ${user}`, ``)
// Or ${prefix}addrole/+role ${user}
let role = msg.guild.roles.cache.find(r => r.name === r1);

How could i use user id instead of mention on discord.js to give role?

This is my code for mentions but I want to use id instead
if (message.member.roles.cache.some(r => r.name === "Mod")) {
if (message.mentions.members.first()) {
let role = message.guild.roles.cache.find(r => r.name === "Muted");
let member = message.mentions.members.first();
member.roles.add(role);
message.channel.send(member);
}
}
Here is what I say for mention: moder! Mute #bigboitest
What I want to do: moder! Mute 862743465594191882
Simply, to get the user from their ID you'll need to find the ID in a message and then search for it in the clients users.
Example:
let args = <Message>.content.split(/ +/g); // Define message arguments
let member = <Message>.mentions.users.first() || <Message>.guild.members.fetch(args[2]); // Get the user from a mention or ID
Full example:
if (message.member.roles.cache.some(r => (r.name).toLowerCase() === "mod")) { // Search for the role "Mod"
let role = message.guild.roles.cache.find(r => (r.name).toLowerCase() === "muted"); // Search for the "Muted" role
let args = message.content.split(/ +/g); // Define message arguments
let member = message.mentions.members.first() || message.guild.members.fetch(args[2]); // Get the user from a mention or ID
if (!role) return message.channel.send('I cannot find a muted role'); // If the role doesn't exist, notify the user
if (!member) return message.channel.send(`<#!${message.author.id}>, ${member} is not in this server`); // If the member doesn't exist
member.roles.add(role); // Mute the member by adding the muted role
message.channel.send(`<#!${member.user.id}> was muted by <#!${message.author.id}`); // Send a response
} else {
message.channel.send('You are not a mod'); // Notify the user that they are not able to use the command
}

How do I compare the ID of the new members with the ones in the database?

I am trying to make a jail command when the members who has this role leave the server and rejoin again the role stay with them ,i want when new members enter the server to compare their ID with the one in the database, if is it true it gives a role again. my code:
client.on("guildMemberAdd",async (member)=>{
let injail = await db.fetch(`ja_${member.guild.id}`)
let role = msg.guild.roles.cache.find(n => n.name === 'Jail');
if(member.guild.id = injail){
member.roles.remove(user.roles.cache)
member.roles.add(role)
}
});
client.on('message', msg => {
if(msg.content.startsWith(prefix + 'jail')){
if(!owners.includes(msg.author.id)) return msg.channel.send("**You Dont Have Perms 📛**")
if(!msg.channel.guild) return;
var logChannel = msg.guild.channels.cache.find(channel => channel.id === logID)//LOG
let jailRole = msg.guild.roles.cache.find(n => n.name === 'Jail');
let user = msg.mentions.members.first() //You can change this to an ID
let args = msg.content.split(" ").slice(1).join(" ")
if(!args[0]) return msg.channel.send('**:x: Please Mention A User**')
if(user.hasPermission("ADMINISTRATOR")) return msg.channel.send("**Im NOT Allowed To Do This 📛**")
let there = db.get(`jl_${msg.guild.id}_${user}`, user.id)
if (there) return msg.channel.send('**This User AlREADY In Jail â›”**')
msg.channel.send(`**ADDED ${user} to the Jail! ✅**`)
db.set(`jl_${msg.guild.id}_${user}`, user.id)
user.roles.remove(user.roles.cache)
user.roles.add(jailRole)
let embed = new Discord.MessageEmbed()
.setTitle('Jail System')
.setAuthor(msg.author.tag,msg.author.avatarURL({dynamic:true}))
.addField("Status",`JOINED THE JAIL 🔴`)
.addField("User",`<#${user.id}> (ID: ${user.id})`)
.addField("By",`<#${msg.author.id}> (ID: ${msg.author.id})`)
.setTimestamp()
logChannel.send(embed)
}
});
Whenever people gets "jail" role, save "yes" to database.
and check condition as you did above.
here I'll give sample code:
// when people gets jail role, add following code:
db.set(`ja_${msg.guild.id}_${user}`, "yes") // or you can simply use boolean operator
// when guildMemberAdd event triggers check condition:
let condition = db.fech(`ja_${member.guild.id}_${member.user.id}`)
if(condition === "yes"){
// write code to add "jail" role to the member
}
else{
// your other code
}
Also don't forget to set "no" in the database whenever you remove jail role from member.

How to know if a user already has a role in an addrole command

I want to send an error message saying that the user already has the role if they do in an addrole command.My current command is:
const { MessageEmbed } = require("discord.js");
module.exports = {
name: "addrole",
description: "adds a role to a member",
execute: async (message , args) => {
if (!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send("Invalid Permissions")
let member = message.mentions.members.first();
let role = message.mentions.roles.first();
if (!member)
return message.reply("Please mention a valid member of this server");
if (!role) return message.reply("please mention a valid role of this server");
const highest = message.member.roles.highest;
if(highest.comparePositionTo(role) <= 0)
return message.channel.send(`You cannot add roles equal/higher to that member's highest role`)
let addroleembed = new MessageEmbed()
.setTitle(`:white_check_mark: ${role.name} has been added to ${member.user.tag}`)
.setFooter(`Action performed by ${message.author.tag}`)
.setColor('77B255')
addroleembed.setTimestamp();
member.roles.add(role);
message.channel.send(addroleembed)
}}
I have tried using
if(!message.member.roles.cache.some(r=>[`${role.name}`].includes(r.name)))
and
if (member.roles.has(role.id) {
But neither of these seem to work, Anyone know the reason behind this? If I can't use either of these methods, Can you suggest me a solution?
You are so close!
All you need is cache when using has()
member.roles.cache.has(<RoleID>);
Learn More Here
You need to combine your two tries.
if(message.member.roles.cache.has("role ID") {
//rest of your code
}

Discord.js problem searching in the roles of a user

I am trying to create a command, in this case it is activated with / attack, the mechanism I am looking for is that if the target user (mentioned) has the role (Lavander) which is a kind of shield, send an embed message saying defended and remove the role from you (break the shield) and if the target user (mentioned) does not have the shield role, just send a different message saying attacked. This is the code that I have been doing but it does not work for me even if it does not give errors, simply when using it, it ignores the role detection and sends both messages for some reason that I do not know, can someone help me?
if (message.content.startsWith('/attack')) {
let Lavander = message.guild.roles.cache.find(role => role.name == "Lavander");
let member = message.mentions.members.first();
if (message.member.roles.cache.has(Lavander)) return
member.roles.remove(Lavander);
message.channel.send(new Discord.MessageEmbed()
.setColor("GOLD")
.setTitle(message.author.username)
.setDescription("Defended"))
message.channel.send(new Discord.MessageEmbed()
.setColor("GOLD")
.setTitle(message.author.username)
.setDescription("Attacked"))
}
For me it seems like let Lavander = message.guild.roles.cache.find(role => role.name == "Lavander"); might be supposed to be let Lavander = message.guild.roles.cache.find(role => role.name === 'Lavander'); but without the info about the glitches and/or errors, I can't tell you anything else.
method collection.has require id as property. So you need somethink like this:
bot.on('message', (message) => {
if (message.content.startsWith('/attack')) {
let lavander = message.guild.roles.cache.find((role) => role.name === 'Lavander');
let member = message.mentions.members.first();
if (!member || !lavander) return message.reply('No role or member');
if (message.member.roles.cache.has(lavander.id)) {
member.roles.remove(lavander);
let embed = new Discord.MessageEmbed()
.setColor('GOLD')
.setTitle(message.author.username)
.setDescription('Defended');
} else {
let embed = new Discord.MessageEmbed()
.setColor('GOLD')
.setTitle(message.author.username)
.setDescription('Attacked');
message.channel.send(embed);
}
}
});

Resources