Discord bot issue with getting user roles check - node.js

So im doing a mute function where i add a muted role based on the channel they are muted from, my issue here is that it actually refuses to get the guild member by id, and ends up with hasRole erroring out because discordMember is undefined when trying with id, if i use mention way no problem it works as intended.
Anyone got a surgestion to fix this.
const discordMember = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
const channelName = message.mentions.channels.first()
var role = message.guild.roles.cache.find(role => role.name == channelName.name + "-mute");
var hasRole = discordMember.roles.cache.has(role.id);

Closing since original issue was fixed.

Related

Check if user has a role in the new discord.js version

Hey I wanted to check if a user had a role, although all other methods are quite outdated and I've tried a couple of things, none of them work.
My attempt:
var guild = client.guilds.cache.get("Guild ID")
var buyerRole = guild.roles.cache.get("Role ID")
await guild.members.fetch()
const guildMember = guild.members.cache.get(message.author.id)
if(guildMember.roles.find(buyerRole.id)){...
Well, you're going to want to use member.roles.cache.some() to achieve this.
var guild = client.guilds.cache.get("Guild ID")
var buyerRole = guild.roles.cache.get("Role ID")
const guildMember = await guild.members.fetch('User ID');
if(guildMember.roles.cache.some(x => x.id == buyerRole.id)){...
.some will always return a boolean, so it's better for you to use it like that.
After a bit of debugging, it turns out that you can use
message.author._roles instead of message.author.roles in order to get the complete list array.

Add roles to user from DMs in new discordjs

I wanted to know how I could add a role to a specific user within DMs.
With the new Discord.js update, it's tricky, and can't find a way around it.
Thanks.
My attempt:
var guild = client.guilds.cache.get("[GUILDID]")
var buyerRole = guild.roles.cache.get("[ROLE ID]")
console.log(message.author.id) // Works
var guildMember = guild.members.fetch(message.author.id)
console.log(guildMember.displayName) // Returns 'undefined'
guildMember.setNickname(guildMember.username+" | Buyer") // Error
console.log(buyerRole.color) // Works
Output: guildMember.setNickname is not a function
That's because you fetch the member but never await it. .fetch returns a promise so the right way to get guildMember is like this:
const guildMember = await guild.members.fetch(message.author.id)

How to convert your message(role) to #<role> on channel? (discord.js)

I'm writing discord bot.
When I write <role> on channel I'd like to check if this role exists and rewrite/save as var #<role> (ping all members that have this role). But I don't know how.
Thanks for help;)
I found something but it doesn't work well. It never finds role which exists on that server. Here is code
const roleArgs = msgef;
const role = msgef.guild.roles.cache.find(role => role.name === roleArgs);
if (!role) {return msessage.reply ('There is not such role!'); return; }
ping = "<#&" + role.id + ">" ;
message.channel.send(ping);
Thanks for help ;)

Check if the user adding a reaction has a role

I want to check if the user reacting to a message has a role or not, so that if the user dont have a rank the bot will ignore the rest of the command, but I dont know how.
example: if a user with the admin rank reacted with :eggsa: emoji, the bot would continue with the command, but if i only had member rank the bot would ignore me.
client.on('messageReactionAdd', reaction => {
const eggsa = client.emojis.find(emoji => emoji.name === "eggsa");
if (reaction.emoji.name === 'eggsa') {
const message = reaction.message;
const kanal = reaction.message.guild.channels.find('name', 'sitater');
var embed = new Discord.RichEmbed()
.setAuthor(reaction.message.content)
.setTimestamp()
.setFooter(reaction.message.author.username, reaction.message.author.avatarURL)
kanal.send({embed});
}
});
the code works in this stage, only everyone can use it
I would be really grateful if someone could help me >:)
If you look at the messageReactionAdd docs you can see that along with the reaction, you can also get the user which added the reaction. So instead of
client.on('messageReactionAdd', reaction => {
You'd have
client.on('messageReactionAdd', (reaction, user) => {
So then you have the user and which is of type User. Because the user isn't of type GuildMember you first need to fetch the correct Guild member before you can check his/her role.
The easiest way to do this is by getting the Message to which the reaction was added with const msg = reaction.message;. Then you can get the guild from the message with const guild = msg.guild;. Now you can access the guild members with const guildMembers = guild.members;. Lastly you'd need to find the correct member with const guildMember = guildMembers.get(user.id);.
Now that you have your Guild Member you can access his/her Roles and thus check whether he/she has or does not have a certain role

discord.js how to return all members from a current role

i am currently working on some updates for my discord bot, I am using discord.js
and trying to get all the usersnames from a current role via dm,
For example, if 3 users have the role Admin,
then the 3 usernames will be returned via message,
so far i have this
bot.on('message', msg => {
if(msg.channel instanceof Discord.DMChannel)
{
if(msg.content == prefix + "des"){
let RoleName = "Admin";
let guildid = "idwashere";
let memberWithRole =
bot.guilds.get(guildid).roles.get("name",
RoleName).members;
console.log(memberWithRole);
msg.reply("Feature coming soon");
}
}
});
i get a error
let memberWithRole =
bot.guilds.get(guildid).roles.get("name",
RoleName).members;
^
TypeError: Cannot read property 'members' of
undefined
i feel i'm close but yet not sure what im doing wrong :)
Managed to fix it
i changed memberswithrole to
let memberWithRole = bot.guilds.get(guildid).roles.find("name", RoleName).members.map(m=>m.user.username);
this then returns the username
If anyone has any better way or imrpovents please let me know.

Resources