this is my role command.
I'm trying to use startsWith to give roles: if the arg's first letters are the same as the role, it should be assigned to the user.
exports.run = async function (msg,args) {
if(!msg.member.hasPermission('MANAGE_ROLES')) return;
const member = msg.mentions.members.first();
const name = msg.mentions.users.size ? args.replace(/ +/, ' ').split(' ')[1] : args;
const role = msg.guild.roles.find(r => r.name.toLowerCase().startsWith(name.toLowerCase()));
if (name && !role.size) return msg.channel.send("Role Not found");
if (name && role.size > 1) return msg.channel.send("There is similar roles , Please supply more letters");
await member.addRole(role);
msg.channel.send(`**Role \`${role}\` Given to **${member} Succesflly`);
};
Trying to use : $role #member ad
ad is first letter for admin role as an example .
args usually is an array.
Try args[1] instead of args. For educational purposes, you could also try console.log(args);.
Related
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);
V12 Code
I want to make this command can be used by anyone having a specific role or manage channel perm but it not works, it only allows people having manage channel perm not the people having specific role.
Problem Code
if (!message.member.roles.cache.has('845453361008476190') || !message.member.hasPermission('MANAGE_CHANNELS')) return message.channel.send("sorry, you do not have permission to use command.")
Complete Code
const prefix = process.env.PREFIX;
module.exports = {
name: 'vip',
category: 'moderation',
aliases: ['v'],
description : 'Used give VIP to a User ',
usage: `${prefix}vip <#user>`,
run : async(client, message, args) => {
if (!message.member.roles.cache.has('845453361008476190') || !message.member.hasPermission('MANAGE_CHANNELS')) return message.channel.send("sorry, you do not have permission to use command.")
const guild = client.guilds.cache.get("842019142118014996");
const role = guild.roles.cache.get("845453369564856361");
const aUser = message.mentions.users.first();
if (!aUser) return message.channel.send("Can't find user!");
const member = await guild.members.fetch(aUser.id);
if (member.roles.cache.get(role.id)) {
return (
member.roles.remove(role),
message.channel.send(`Removed VIP role from ${aUser.tag}`)
);
} else {
await member.roles.add(role),
message.channel.send(`${aUser.tag} Sucessfully got VIP role.`);
}
}
};
This checks if the user doesn't have the required role, or doesn't have the required perms. Change the OR operator (||) to an AND operator (&&) so that if checks if the user doesn't have the required role, and doesn't have the required perms, return early
if (!message.member.roles.cache.has('845453361008476190') && !message.member.hasPermission('MANAGE_CHANNELS'))
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.
i'm trying to do an Autorole command, then it gives the role the user wants to every new members.
my code :
main.js :
const mentionedRole = require("./cmds/autorole")
bot.on('guildMemberAdd', member => {
member.roles.add(mentionedRole);
})
autorole command :
module.exports.run = async (bot, message, args) => {
const mentionedRole = message.mentions.roles.first();
if (!mentionedRole) return message.channel.send(`I cannot to use the role: ${mentionedRole}`);
message.channel.send(`Autorole successfully added (${mentionedRole}).`);
}
the error it gives :
Supplied roles is not an Role, Snowflake or Array or Collection of Roles or Snowflakes.
To give a role by mention message.mentions.roles.first() you need to #testRole
So I'd recommend doing it like this:
const mentionedRole = message.guild.roles.cache.find(r => r.name === args.slice(1).join(" "));
if (!mentionedRole) return message.channel.send(`I cannot to use the role: ${mentionedRole}`);
message.channel.send(`Autorole successfully added (${mentionedRole}).`);
Now when it should look like this:
ex. ?autorole testRole
not ?autorole #testRole
this is my role command.
I'm trying to use startsWith to give roles: if the arg's first letters are the same as the role, it should be assigned to the user.
exports.run = async function (msg,args) {
if(!msg.member.hasPermission('MANAGE_ROLES')) return;
const member = msg.mentions.members.first();
const name = msg.mentions.users.size ? args.replace(/ +/, ' ').split(' ')[1] : args;
const role = msg.guild.roles.find(r => r.name.toLowerCase().startsWith(name.toLowerCase()));
if (name && !role.size) return msg.channel.send("Role Not found");
if (name && role.size > 1) return msg.channel.send("There is similar roles , Please supply more letters");
await member.addRole(role);
msg.channel.send(`**Role \`${role}\` Given to **${member} Succesflly`);
};
I updated the current code but still can not give roles depending or first letters on args !
Using the function version of .find() was a good idea, but your mistake is in the way you use .startsWith(): it's not a property, it's a method.
const role = msg.guild.roles.find(r => r.name.toLowerCase().startsWith(args.split(/ +/g).slice(1).join(' ').toLowerCase()));