I should know how to do this. But the code below does not work.
The goal is to remove roles when the user adds a reaction to a message.
client.on('messageReactionAdd', async (reaction, user) => {
const message = await reaction.message.fetch(true);
const channelStockSettings = '961958948976607243';
const fundamentalPlays = 'βοΈ';
const fundamentalPlaysRoleSE = message.guild.roles.cache.find(role => role.name === 'βπΈπͺ');
const fundamentalPlaysRoleUS = message.guild.roles.cache.find(role => role.name === 'βπΊπΈ');
const longTerm = 'β°';
const longTermRoleSE = message.guild.roles.cache.find(role => role.name === 'β°πΈπͺ');
const longTermRoleUS = message.guild.roles.cache.find(role => role.name === 'β°πΊπΈ');
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 === channelStockSettings) {
if (reaction.emoji.name === fundamentalPlays) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(fundamentalPlaysRoleSE);
await reaction.message.guild.members.cache.get(user.id).roles.remove(fundamentalPlaysRoleUS);
} else if (reaction.emoji.name === longTerm) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(longTermRoleSE);
await reaction.message.guild.members.cache.get(user.id).roles.remove(longTermRoleUS);
} else {
return;
}
}
});
This is the way I've coded this before. What the ... is wrong?
After testing your code, it appears that the issue is that you have the wrong unicode emotes, assuming you're trying to use the default Discord emotes for star and mountain. In other words, reaction.emoji.name === fundamentalPlays and reaction.emoji.name === longTerm both were returning false.
I'm not sure how this happened or where you got these unicode emotes from (perhaps an older version of discord, or from a third-party website?), but neither correctly matches the Discord unicode emote (the star emotes seem to look exactly the same but are not equal, and the mountain emotes both do not look the same and are not equal). The best way to get the unicode form of any Discord emote is to put a backslash before a Discord emote when sending it. Then, copy the unicode emote that it gives you.
Here's your code, modified to have the correct unicode emotes. I tested it, and it is working for me:
client.on('messageReactionAdd', async (reaction, user) => {
console.log("Reaction received");
const message = await reaction.message.fetch(true);
const channelStockSettings = '883731756438671391';
const fundamentalPlays = 'β';
const fundamentalPlaysRoleSE = message.guild.roles.cache.find(role => role.name === 'Member');
const fundamentalPlaysRoleUS = message.guild.roles.cache.find(role => role.name === 'Youtuber');
const longTerm = 'β°οΈ';
const longTermRoleSE = message.guild.roles.cache.find(role => role.name === 'Testor');
const longTermRoleUS = message.guild.roles.cache.find(role => role.name === 'Co-op');
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 === channelStockSettings) {
if (reaction.emoji.name === fundamentalPlays) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(fundamentalPlaysRoleSE);
await reaction.message.guild.members.cache.get(user.id).roles.remove(fundamentalPlaysRoleUS);
} else if (reaction.emoji.name === longTerm) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(longTermRoleSE);
await reaction.message.guild.members.cache.get(user.id).roles.remove(longTermRoleUS);
} else {
return;
}
}
});
Note that there could be numerous additional issues responsible for this code not working, however:
a) The messageReactionAdd event only fires for reactions added to cached messages. Therefore, it will not fire if you add a reaction to a message sent before the bot started. If you are trying to do this with such an uncached message, you will need to specifically fetch the message immediately when your bot starts up (most likely in your ready event handler).
b) Make sure you have the correct intents for receiving messages and message reactions. I assume you do, but double-check, otherwise the messageReactionAdd event will not fire at all.
I had the same problem with my bot. Can you try delete the embed message and add it again. Then try it again.
Related
I have a ReactionCollector and as soon as I click on the right arrow reaction, I want it to remove my reaction instantly; so I can react again as soon as possible. How do I go about this? Here's my code boss.
const embed = new MessageEmbed().setDescription(`test`);
const listEmbed = await message.channel.send(embed);
await listEmbed.react("β‘οΈ")
const filter = (reaction, user) => ["β‘οΈ"].includes(reaction.emoji.name) && (message.member.id == user.id)
const collector = listEmbed.createReactionCollector(filter)
collector.on("collect", async (reaction, user) => {
if (reaction.emoji.name === "β‘οΈ") {
//remove user reaction
}
})
Corrent me if I'm wrong, but I'm pretty sure it's:
reaction.users.remove(message.author.id)
You have the reaction, you take the users, and remove the reaction from message.author.id.
Ok so... I've been attempting to make a reaction roles command but it won't work. The role isn't given to the user. It gives me this error "ReferenceError: reaction is not defined"
Code:
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL","REACTION"]});
client.on('message', async message => {
if(message.content === '>reactions'){
const embed = new Discord.MessageEmbed()
.setTitle('Reaction Roles')
.setDescription('React to obtain your roles!')
.setColor('#242323')
let MessageEmbed = await message.channel.send(embed)
MessageEmbed.react('π¨βπ¦')
}
if(reaction.message.partial) await reaction.message.fetch();
if(reaction.partial) await reaction.fetch();
if(user.client) return;
if(!reaction.message.guild) return;
if(reaction.message.channel.id === "759064796611215442") {
if(reaction.emoji.name === 'π¨βπ¦'){
await reaction.message.guild.member.cache.get(user.id).roles.add('760838222057046046')
}
}
});
I want that the bot gives the reacter a role. But it doesnt work. Does anyone know where the problem is? Im coding in discord.js v12
if (user.bot) return;
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.guild.id !== "753289194738024632") return;
if (reaction.message.channel.id === "753290581412937832") {
const cs = bot.emojis.cache.find(emoji => emoji.name === "csgo");
const vl = bot.emojis.cache.find(emoji => emoji.name === "valorant");
const fn = bot.emojis.cache.find(emoji => emoji.name === "fortnite");
let role = reaction.guild.roles.cache.find(role => role.id === '753290549548679229');
member.roles.add(role.id);
Try changing reaction to message
let role = message.guild.roles.cache.find(role => role.id === '753290549548679229');
I think reaction.guild isn't a valid function
I would change it to this:
if(message.channel.type === 'dm') return message.reply('This command does not work in DMs') //checks if the command wasnt executed in DMs
let role = message.guild.roles.cache.find(role => role.id === '753290549548679229');
I'm making a discord murder mystery bot.
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("message", (message) => {
msg = message.content.toLowerCase();
if (message.author.bot) {
return;
}
mention = message.mentions.users.first();
if (msg.startsWith("kill")) {
if (mention == null) {
return;
}
message.delete();
mention.send('you are dead');
message.channel.send("now done");
}
});
client.login('my token');
What would I add to the code so after the person who was tagged got there role changed from alive to dead?
// First, make sure that you're in a guild
if (!message.guild) return;
// Get the guild member from the user
// You can also use message.mentions.members.first() (make sure that you check that
// the message was sent in a guild beforehand if you do so)
const member = message.guild.members.cache.get(mention.id);
// You can use the ID of the roles, or get the role by name. Example:
// const aliveRole = message.guild.roles.cache.find(r => r.name === 'Alive');
const aliveRole = 'alive role ID here';
const deadRole = 'dead role ID here';
// You can also use try/catch with await if you make the listener and async
// function:
/*
client.on("message", async (message) => {
// ...
try {
await Promise.all([
member.roles.remove(aliveRole),
member.roles.add(deadRole)
]);
} catch (error) {
console.error(error);
}
})
*/
Promise.all([
member.roles.remove(aliveRole),
member.roles.add(deadRole)
]).catch(console.error);
The Promise.all means that the promises for adding and removing the roles are started at the same time. A promise is an object that can resolve to a value or reject with an error, so the .catch(console.error) logs all errors. I recommend that you handle errors for message.delete(), mention.send('you are dead'), and message.channel.send("now done") as well.
For more information on member.roles.remove() and member.roles.add(), see the documentation for GuildMemberRoleManager.
I am trying to make a suggestion feature for one of my bots. I have searched online but nothing really helps with it. The suggestion part is working but I want to add a feature where if i react to the message in the log channel it sends the reaction to the original message. Here is my code:
bot.on('message', message => {
if (message.content.startsWith("/suggest")){
message.reply(`Your response has been recorded.`)
var yes = message.content
const channel1 = bot.channels.cache.get('722590953017442335');
channel1.send(`${message.author} suggests: ${yes.slice(9)}`)
if (chanell1.reaction.emoji.name === 'β
'){
const channel2 = bot.channels.cache.get('722595812462297139');
channell2.react.author.message('β
')
}
}
})
I am using v12 of node.
You can use the awaitReactions() function:
bot.on("message", (message) => {
if (message.content.startsWith("/suggest")) {
message.reply(`Your response has been recorded.`);
var yes = message.content;
bot.channels.cache
.get("722590953017442335")
.send(`${message.author} suggests: ${yes.slice(9)}`)
.then(async (msg) => {
msg
.awaitReactions((reaction) => reaction.emoji.name === "β
", {
time: 15000,
})
.then((collected) => message.react("β
"))
.catch(console.error);
});
}
});
Please read the official documentation at https://discord.js.org/#/docs/main/v12/general/welcome for v12 help. You ought to use the Client#messageReactionAdd event to track reactions - your code isn't too far off, however it is missing that key event. Please note that to track reactions you'll need persistent storage if you want the reactions to work after restart. Alternatively, you could try awaiting the reactions or using a reaction collector if only short term.
Try this instead:
const { Client } = require('discord.js');
const bot = new Client({ partials: ['REACTION', 'USER'] });
const prefix = '/';
const suggestionsCache = {};
bot.on('message', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.split(' '),
command = args.shift().slice(prefix.length);
if (command == 'suggest') {
const suggestion = args.join(' '),
suggestionMessage = `${message.author} suggests: ${suggestion}`,
suggestionChannel = bot.channels.cache.get('722590953017442335'),
logChannel = bot.channels.cache.get('722595812462297139');
if (!suggestionChannel || !logChannel) return message.reply('Oops! Something went wrong.');
try {
const suggestionMessage = await suggestionChannel.send(suggestionMessage);
const logMessage = await logChannel.send(suggestionMessage);
suggestionsCache[logMessage.id] = suggestionMessage.id; // Save the ID for later.
message.reply('Your response has been recorded.');
} catch {
message.reply('Oops! Something went wrong.');
};
};
});
bot.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) {
try {
await reaction.fetch();
} catch {}
}
const messageID = suggestionsCache[reaction.message.id];
if (!messageID || reaction.emoji.name !== 'β
') return; // If not found in cache, ignore and if the emoji isn't the check, ignore it.
try {
const channel = await client.channels.fetch('722590953017442335');
if (!channel) return;
const message = channel.messages.fetch(messageID);
if (!message) return; // Message deleted.
message.react('β
');
} catch {
return;
};
});
Please note that I am new to v12 and normally use v11! The code above is not tested and may contain bugs as a result. Please feel free to edit/update the code.