How do I highlight a role or any role in an embedded message?
I've tried this:
if (message.content.toLowerCase() === 'role test') {
let = message.guild.roles.find(role => role.name === "ADMIN");
let uEmbed = new Discord.MessageEmbed()
.setDescription(`pong ${role}`);
message.channel.send({embed: uEmbed})
};
It gave me this error:
UnhandledPromiseRejectionWarning: ReferenceError: role is not defined
This line: let = message.guild.roles.find(role => role.name === "ADMIN");
See the mistake? What variable would contain that info?
Related
I'm very new/bad at discord js. But I want to make a command with "allowed roles" so only admin, owner and mod can use it. But I don't know how.
I have tried this:
client.on('message', message => {
if(message.content === 'test') {
let allowedRole = message.guild.roles.cache.find(r => r.name === "owner");
let allowedRole1 = message.guild.roles.cache.find(r => r.name === "mod");
let allowedRole2 = message.guild.roles.cache.find(r => r.name === "admin");
if (message.member.roles.cache.has(allowedRole.id) && message.member.roles.cache.has(allowedRole1.id) && message.member.roles.cache.has(allowedRole2.id)) {
message.channel.send('hello');
} else {
message.channel.send('bad');
}
}});
in your example, only users with all three roles are accepted (due to the logical AND operator).
Replace && with || and it should work as expected.
client.on('message', async message => {
if (message.content === 'test') {
let allowedRole = await message.guild.roles.cache.find(
r => r.name === 'owner'
);
let allowedRole1 = await message.guild.roles.cache.find(
r => r.name === 'mod'
);
let allowedRole2 = await message.guild.roles.cache.find(
r => r.name === 'admin'
);
if (
message.member.roles.cache.has(allowedRole.id) ||
message.member.roles.cache.has(allowedRole1.id) ||
message.member.roles.cache.has(allowedRole2.id)
) {
message.channel.send('hello');
} else {
message.channel.send('bad');
}
}
});
As Christoph said, you can use their code but I'd recommend to use role IDs intead because role name can be changed at any time:
if (!message.member.roles.cache.has(<1st_role_id>) || !message.member.roles.cache.has(<2nd_role_id>) || !message.member.roles.cache.has(<3rd_role_id>))
return message.reply("You can't execute this command!")
// your code
WARNING
It's only a solution if you are running private bot and not public, which is for a lot of servers
I have a ban command where you can either mention a user or use their id.
When mentioning the user in the embed it comes up with undefined has been banned! I want it to come up with user#0001 that was banned when mentioning the user or by banning them by their id.
Current code:
const switchc = bot.emojis.cache.find((emoji) => emoji.name === "switchcancel");
const switche = bot.emojis.cache.find((emoji) => emoji.name === "switch");
if (!message.member.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"]))
return message.channel.send(
`${switchc} **You do not have the permissions to complete this command!**`
);
let banMember =
message.mentions.members.first() ||
(await bot.users.fetch(args[0]).catch((err) => console.log(err)));
if (!banMember)
return await message.channel.send(
`${switchc} **Please supply a user to be banned!**`
);
let reason = args.slice(1).join(" ");
if (!reason) reason = "No reason was provided!";
if (!message.guild.me.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"]))
return message.channel.send(
`${switchc} **I do not have permission to complete this command!**`
);
message.guild.members
.ban(banMember, { days: 1, reason: reason })
.catch((err) => console.log(err));
const bEmbed = new MessageEmbed()
.setColor("RANDOM")
.setDescription(`**${banMember.tag}** has been banned!`);
message.channel.send(bEmbed);
Is there something I am doing wrong?
As you do not need a member object, the simplest fix would be:
let banMember = message.mentions.users.first() || await bot.users.fetch(args[0]).catch(err => console.log(err))
Using users instead of members in the mention getter.
Okay, I figured out my own issue. What I did was instead of using
${banMember.tag}
I instead defined tag by doing
let tag = banMember.tag || banMember.user.tag
The new code is as follows
const switchc = bot.emojis.cache.find(emoji => emoji.name === "switchcancel");
const switche = bot.emojis.cache.find(emoji => emoji.name === "switch");
if(!message.member.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send(`${switchc} **You do not have the permissions to complete this command!**`)
let banMember = message.mentions.members.first() || await bot.users.fetch(args[0]).catch(err => console.log(err))
if(!banMember) return await message.channel.send(`${switchc} **Please supply a user to be banned!**`)
let tag = banMember.tag || banMember.user.tag
let reason = args.slice(1).join(" ")
if(!reason) reason = "No reason was provided!"
if (!message.guild.me.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send(`${switchc} **I do not have permission to complete this command!**`)
message.guild.members.ban(banMember, { days: 1 , reason: reason}).catch(err => console.log(err))
const bEmbed = new MessageEmbed()
.setColor("RANDOM")
.setDescription(`**${tag}** has been banned!`)
message.channel.send(bEmbed)
}
}
This will allow for the users tag to show up when baning by ID or mention.
i would like to create a reaction role function to my bot (there IS a message, then if any member reacts to the message, he gets a role. Like in MEE6).
I've tried this...
client.on('messageReactionAdd', (messageReaction, user) => { if(user.bot) return; const { message, emoji } = messageReaction; if(emoji.name === "✅") { if(message.id === "713071578174193745") {
var role = message.guild.roles.find(role => role.name === "Nem hitelesített 2")
user.addRole(role)
var role = message.guild.roles.find(role => role.name === "Nem hitelesített 1")
user.removeRole(role)
}}},)
client.on("messageReactionAdd", (reaction, user) => {
console.log("1");
if (reaction.message.channel.id !== "713066051969220631" && reaction.emoji.name !== '✅') {
return;
}
client.guilds.get(yourguildid).member(user).addRole('713065834771120172')
}
but i didn't reach anything. Can you help me please?
Note: I'm on v11. By another man, it worked, he was on v11 too. The ÍTRIGGER isn't working, if i put console.log before if, and i'm reacting, no answer.
const role = Discord.guildMember.roles.find(role => role.name === 'test');
message.author.guildMember.roles.add(role);
My Code İs This But I get A Error Says ;
TypeError: member.addRole is not a function
It Shows This Line ;
const role = Discord.guildMember.roles.find(role => role.name === 'test');
is broken but ı can't figure out what is the problem
discord.js v12
You try to find a role off the user, you could try to use this to find the role in the guild that the message was sent in.
let role = message.guild.roles.find(r => r.name === 'test');
For discord v12 you need use ....roles.cache.find( and message.author has no property member or guildMember, message.author its a user object.
let role = message.guild.roles.cache.find(r => r.name === 'test');
if(role) message.member.roles.add(role);
Code:
client.on('message', message => {
if (message.content.startsWith(`${prefix2}red`)){
if (message.member.roles.cache.some(role => role.name === 'Red')) return message.channel.send(`You already have the role!`)
let role = message.guild.roles.find(r => r.name === "Red");(r => r.name === "Red");
let member = message.member;
message.delete(1)
member.addRole(role).catch(console.error)
}
})
I am trying to make a command that will give the user that role.
Error:
let role = message.guild.roles.find(r => r.name === "Red");
^
TypeError: message.guild.roles.find is not a function
Use message.guild.roles.cache.find.