So when bot sends a certain message, it reacts to it too. Now m tryin to check a user who reacted to that reaction has a certain role or not but so far I haven't been able to do so.
bot.on('messageReactionAdd', (reaction, user) => {
//var member = reaction.message.guild.members.cache.find(member => member.id = user.id);
//if(message.member.roles.cache.some(role => role.name === 'Bronze')) return(message.channel.send("you dont have role."));
if (reaction.message.members.roles.cache.has('729452091168260188')) return(message.channel.send("you have the role"));
else { console.log("you dont have role"); }
})
You made a mistake in the following line of code:
reaction.message.members.roles.cache.has('729452091168260188')
The Message object has no property members, it should be member:
reaction.message.member.roles.cache.has('729452091168260188')
Related
I'm attempting to create a more complex reaction role function.
Basically I want to achieve that if a user has 2 roles, it should have access to some channels.
So the way I'm trying to do this is by checking if the user has one of the roles when clicking on a reaction that grants the member the second role. If the user has the other role, he should get a third role that unlocks these channels.
But, I haven't been able to figure out how to look at the roles of the person reacting to the message.
client.on('messageReactionAdd', async (reaction, user) => {
const message = await reaction.message.fetch(true);
const channelAccess = '918617193988649002';
const foodSweden = message.guild.roles.cache.find(role => role.name === 'π²πΈπͺ');
const foodCategoryReaction = 'π²';
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id === channelAccess) {
if (reaction.emoji.name === foodCategoryReaction && message.member.roles.has('πΈπͺ')) {
await reaction.message.guild.members.cache.get(user.id).roles.add(foodSweden);
} else {
return;
}
}
});
So this is the code. I'm uncertain how message.member.roles.has('πΈπͺ') should be coded.
I've tried reaction.message.member.roles.has('πΈπͺ'), reaction.member.roles.has('πΈπͺ'), user.roles.has('πΈπͺ') and shifted out member for user.
I'm uncertain how message.member.roles.has('πΈπͺ') should be coded. I've tried reaction.message.member.roles.has('πΈπͺ'), reaction.member.roles.has('πΈπͺ'), user.roles.has('πΈπͺ') and shifted out member for user.
None of those are existing methods. member.roles does not have a .has() method. And users represent general discord users outside of guilds; users do not have roles, only guild members have roles. The .has() method of any manager in discord.js also usually utilizes IDs (which is what they are mapped to in the underlying Collection cache), not names or other properties. See the docs.
Here is how you check if the member who reacted has a specific role:
const hasRole = reaction.message.member.roles.cache.some(role => role.name == "πΈπͺ");
The role cache for guild members is almost always up-to-date, so you don't really need to do any fetching. You can rely on the role cache here.
I'm trying do create a bot that can add specific role to the user who react to the message with the emoji listed.
With the code below, I can check who reacted to the message and i can also check what emoji they react with, but when I am trying to add role to them, error pops up say user.addRole is not a function is there any way to solve this problem? Thanks a lot!
Code that create an embed message for user to react
let args = message.content.substring(PREFIX.length).split(" ");
if(message.content.toLowerCase() === '?roles' && message.author.id === 'adminId' && message.channel.id === 'channel id'){
const role = new Discord.MessageEmbed()
.setTitle("ROLES")
.setColor('#6a0dad')
.setDescription('π₯ - ROLE1\nβοΈ - ROLE2\nπ§ - ROLE3')
message.channel.send(role).then(re => {re.react('π₯'),re.react('βοΈ'),re.react('π§')});
message.awaitReactions().then(collected=>{
const reaction = collected.first();
})
}
Code that get the react user and trying to add role
const bot = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
bot.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) {
try {
await reaction.fetch();
} catch (error) {
console.log('Something went wrong when fetching the message: ', error);
return;
}
}
if(reaction.message.id === 'id of that embed message sent'){
if(reaction.emoji.name === "π₯"){
//console.log('ROLE1');
user.addRole('id of role 1');
}
if(reaction.emoji.name === 'βοΈ')
//console.log('ROLE2');
user.addRole('id of role 2');
if(reaction.emoji.name === 'π§')
//console.log('ROLE3');
user.addRole('id of role 3');
}
});
Looks like you're trying to add a role to a User. When you should be adding the role to a GuildMember. As you can see here: messageReactionAdd returns a User. However Users don't have a .roles only GuildMembers do. However you have two ways you can get the GuildMember easily:
This way you have to make sure the message is from a TextChannel not a DMchannel.
if(reaction.message.type === "text") let member = reaction.message.member;
OR
This way allows the user to react to ANY message the bot has cached.
let member = bot.guilds.get('<id of the server>').members.get(user.id);
Then you do what #Syntle said: member.roles.resolve('<id of role>');
The choice of how to get the member is up to you.
user.addRole() needs to be replaced with member.roles.add.
if (message.content.startsWith(`${prefix2}red`)){
if (message.member.roles.some(role => role.name === 'Red')) return message.channel.send(`You already has that role `)
let role = message.guild.roles.find(r => r.name === "Red");
let member = message.member;
message.delete(1)
member.addRole(role).catch(console.error)
}
})
What do I need to change? for it to work?
the error is
if (message.member.roles.some(role => role.name === 'Red')) return message.channel.send(`You already has that role `)
TypeError: message.member.roles.some is not a function
I am assuming you are using discord.js v12 and that's why your code won't work.
Try using message.member.roles.cache.some(role => role.name === 'Red') instead of message.member.roles.some(role => role.name === 'Red')
It seems that message.member is undefined, you might want to check if this is done in a guild or not. If it is in a guild it will return the member property, while if it isn't, it won't. What you want to do is check if the message was sent from a guild or not, try the code below:
client.on("message", message => {
// `!` means `non-existent` or `is not`, and if the user sends the message from a guild
// this will not be triggered, since we know they are in, rather than not in, but, if
// it was sent outside of a guild, say a DM, then it will return the command, not trigerring
// any errors or such.
if (!message.guild) return;
// This will not allow this command to be triggered by the bot itself, since it may
// return a loop.
if (message.author === client.user) return;
// If the author of the message is a bot, then return, since bots can be used to spam
// and this will also spam your bot's API request. Webhooks work the same way.
// `||` means `or` if you didn't know.
if (message.author.bot || message.webhookID) return;
// Checks if the member has the role "ROLE NAME", and if they do, return.
if (message.member.roles.cache.some(role => role.name == "ROLE NAME")) return;
// code...
});
I need it so that a bot will send a message and if anyone on the server reacts to that message with a reaction it will give them a role.
I Have already tried multiple times, different code samples which allow reactions anywhere in the guild but i want it specific to one channel.
client.on("messageReactionAdd", (reaction, user) => {
if (user.bot) return;
const member = reaction.message.member
switch (reaction.name) {
case "π":
member.addRole("597011179545690121").then((res) => {
reaction.message.channel.send(`You've been given the \`${res.name}\` role!`)
}).catch(console.error);
break;
case "π΅":
member.addRole("597011179545690121").then((res) => {
reaction.message.channel.send(`You've been given the \`${res.name}\` role!`)
}).catch(console.error);
};
})
client.on("messageReactionRemove", (reaction, user) => {
if (user.bot) return;
const member = reaction.message.member
switch (reaction.name) {
case "emoji_name_1":
member.removeRole("roleID").then((res) =>
reaction.message.channel.send(`You've been removed from the \`${res.name}\` role!`)
}).catch(console.error);
break;
case "emoji_name_2":
member.removeRole("someOtherRole").then((res) => {
reaction.message.channel.send(`You've been removed from the \`${res.name}\` role!`)
}).catch(console.error);
};
})
I Want the outcome to be that the person reacts with say a smiley face and they get a certain role but they have to react to a certain message sent by the bot in its own dedicated channel same as pre-made bots such as Reaction Roles bot.
First we have to fetch our Message.
let channel_id = "ChanelID of message";
let message_id = "ID of message";
client.on("ready", (reaction, user) => {
client.channels.get(channel_id).fetchMessage(message_id).then(m => {
console.log("Cached reaction message.");
}).catch(e => {
console.error("Error loading message.");
console.error(e);
});
Then we'll check if someone reacted to our message and give them the appropriate Role
client.on("messageReactionAdd", (reaction, user) => {
if(reaction.emoji.id == "EMOJI ID HERE" && reaction.message.id === message_id)
{
guild.fetchMember(user) // fetch the user that reacted
.then((member) =>
{
let role = (member.guild.roles.find(role => role.name === "YOUR ROLE NAME HERE"));
member.addRole(role)
.then(() =>
{
console.log(`Added the role to ${member.displayName}`);
}
);
});
}
}
I advise against using this code for multiple Roles at once as that would be rather inefficient.
const member = reaction.message.member
Problem: This is defining member as the GuildMember that sent the message, not the one that added the reaction.
Solution: Use the Guild.member() method, for example...
const member = reaction.message.guild.member(user);
switch (reaction.name) {...}
Problem: name is a not a valid property of a MessageReaction.
Solution: What you're looking for is ReactionEmoji.name, accessed via reaction.emoji.name.
...[the users] have to react to a certain message...
Problem: Your current code isn't checking anything about the message that's been reacted to. Therefore, it's triggered for any reaction.
Solution: Check the message ID to make sure it's the exact one you want. If you'd like to allow reactions on any message within a certain channel, check the message's channel ID.
Consider these examples:
if (reaction.message.id !== "insert message ID here") return;
if (reaction.message.channel.id !== "insert channel ID here") return;
Problem: The messageReactionAdd event is not emitted for reactions on uncached messages.
Solution: Use the raw event to fetch the required information and emit the event yourself, as shown here. Alternatively, use Caltrop's solution and fetch the message when the client is ready via TextChannel.fetchMessage().
I'm trying to make a discord.js bot for my server I made for people in my school. I'm trying to make a #classes channel and if you react to certain messages it gives you a role (which gives you access to the text channel for that class).
client.on('messageReactionAdd', (reaction, user) => {
console.log("client.on completed.")
if (message.channel.name === 'classes') {
console.log("if(message) completed.")
if (reaction.emoji.name === "reminder_ribbon") {
console.log("emoji test completed.")
const guildMember = reaction.message.guild.members.get(user.id);
const role = reaction.message.guild.roles.find(role => role.name === "FACS");
guildMember.addRole(role);
}
}
});
This is what I have tried so far, however, it does not give me/the other people reacted to it the role nor does it return an error message.
P.S. Also, how would I be able to make it so when they unreact it removes the role?
Edit: It seems it only gets reactions from cached messages/messages sent after bot startup. Also, message is not defined on the first if(message.channel.id) message.
Try to use the following code:
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id === '552708152232116224') {
if (reaction.emoji.name === "reminder_ribbon") {
const guildMember = reaction.message.guild.members.get(user.id);
const role = reaction.message.guild.roles.get('552709290427940875');
guildMember.addRole(role);
}
}
});
First of all, reaction.users is an Object with all the users that reacted on the message, so you first have to define to which user you want to assign the role. I fixed this fetching the guildMember with user.id.
The second mistake was that you tried to assign a role ID to a guildMember although you first have to fetch the role and then assign the role Object to the guildMember.