How to fix timeout error when using awaitReactions() function? - javascript

I have written this code in my Bot and it doesn't work properly. The function awaitReactions() is always timing out:
message.channel.send({ embed: hEmbed }).then(embedreacted => {
embedreacted.react('šŸ‘').then(() => embedreacted.react('šŸ‘Ž'));
const filter = (reaction, user) => {
console.log("user \n" + user)
return ['šŸ‘', 'šŸ‘Ž'].includes(reaction.emoji.name) && user.id === message.author.id;
};
embedreacted.awaitReactions(filter, { max: 1, time: 20000, errors: ['time'] })
.then(collected => {
console.log("col" + collected)
const reaction = collected.first();
if (reaction.emoji.name == 'šŸ‘') {
let hlembed = new Discord.MessageEmbed()
Embed
embedreacted.edit({ embed: hlembed });
} else if (reaction.emoji.name === 'šŸ‘Ž') {
let hfEmbed = new Discord.MessageEmbed()
Embed
embedreacted.edit({ embed: hfEmbed });
}
for (const [key, value] of Object.entries(reaction)) {
console.log(key, value);
}
})
.catch(collected => {
console.log("collected \n" + collected.keys())
});

In your filter function:
const filter = (reaction, user) => {
console.log("user \n" + user)
return ['šŸ‘', 'šŸ‘Ž'].includes(reaction.emoji.name) && user.id === message.author.id;
};
message.author.id is the bot's user ID, because the message to which the reactions are being "awaited" was created by the bot. So the condition user.id === message.author.id will only trigger when the bot's user reacts to it.
I'm not sure if this is what you want but if you want it to only trigger when a user reacts to it (and not the bot itself) just do user.id !== <Client>.user.id or even user.id !== message.author.id as a shortcut.
If you wish to receive a reaction only from the user that created a prior message to this one, you need to set that <Message> object to a variable and then use user.id === variable.author.id

Related

I can't get my bot to recognize the reaction and respond to it (Discord.js)

const Discord = require ('discord.js')
module.exports.run = async (client, message, reaction, user) => {
message.channel.send("Press **F** to pay respect!!").then(msg => {msg.react("<:pressf:861646469180948530>")})
const filter = (reaction, user) => {
return reaction.emoji.name === ' === "<:pressf:861646469180948530>' && user.id === message.author.id;
};
const collector = message.createReactionCollector( filter);
collector.on('collect', (reaction, user) => {
if (message.emoji.name === "<:pressf:861646469180948530>"){
message.channel.send("You pay respect!!")
}
})
};
The problem is you're setting up the reaction collector on the original message. Try to wait for the sent one, and accept reactions on that:
module.exports.run = async (client, message, reaction, user) => {
const sentMessage = await message.channel.send('Press **F** to pay respect!!');
sentMessage.react('<:pressf:861646469180948530>');
const filter = (reaction, user) =>
// there was an extra === " here
reaction.emoji.name === '<:pressf:861646469180948530>' &&
user.id === message.author.id;
const collector = sentMessage.createReactionCollector(filter);
collector.on('collect', (reaction, user) => {
if (reaction.emoji.name === '<:pressf:861646469180948530>') {
message.channel.send('You pay respect!!');
}
});
};

Using a Reaction Collector on a message the bot sent

Been working on this bot for a bit, but I seem to be stumped. every time I run it, it says
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'createReactionCollector' of undefined
This is caused by
const collector = message.createReactionCollector(filter, { time: 15000 });
and i dont know how else to do this. Most of the other examples are either outdated or made for a particular purpose, making it hard to implement them into my code. I really appreciate any help you can provide!
if (command === 'ping') {
const pingEmbed = new Discord.MessageEmbed()
.setColor('#03cffc')
.setTitle('Ping!')
.setDescription(`${message.author.username} is playing a game! \n \n Playing With: \n ` + isPlaying);
message.channel.send(pingEmbed)
.then(sentEmbed => {
sentEmbed.react("šŸ‘")
}).then( async message => {
const filter = (reaction, user) => {
return reaction.emoji.name === 'šŸ‘' && user.id === message.author.id;
};
const collector = message.createReactionCollector(filter, { time: 15000 });
collector.on('collect', (reaction, user) => {
console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
});
collector.on('end', collected => {
console.log(`Collected ${collected.size} items`);
});
})}
You do not need to use two then methods. Simply one would be enough for your case. Instead of having to use another then method and passing in message, you can just replace message with sentEmbed.
Code:
if (command === 'ping') {
const pingEmbed = new Discord.MessageEmbed()
.setColor('#03cffc')
.setTitle('Ping!')
.setDescription(`${message.author.username} is playing a game! \n \n Playing With: \n ` + isPlaying);
message.channel.send(pingEmbed)
.then(sentEmbed => {
sentEmbed.react("šŸ‘")
const filter = (reaction, user) => {
return reaction.emoji.name === 'šŸ‘' && user.id === message.author.id;
};
const collector = sentEmbed.createReactionCollector(filter, { time: 15000 });
collector.on('collect', (reaction, user) => {
console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
});
collector.on('end', collected => {
console.log(`Collected ${collected.size} items`);
});
})
}

How to detect more than one reaction to a message in discord.js?

I'm trying to create a dynamic help command, in the sense that the users can decide what "page" they would like to go to simply by reacting to the message. I have tried doing this, however, with my code it only detects the first reaction. I have tried setting the max for the awaitReactions method to more than 1, however once I do that it doesn't detect any reaction. Here is my code:
const Discord = require('discord.js');
const fs = require('fs');
module.exports = {
name: 'help',
aliases: ('cmds'),
description: 'Shows you the list of commands.',
usage: 'help',
example: 'help',
async execute(client, message, args, prefix, footer, color, invite, dev, devID, successEmbed, errorEmbed, usageEmbed) {
const helpEmbed = new Discord.MessageEmbed()
.setColor(color)
.setAuthor(`${client.user.username} Discord Bot\n`)
.setDescription('ā€¢ šŸ“ Prefix: ``' + prefix + '``\n' +
`ā€¢ šŸ”§ Developer: ${dev}\n\nāš™ļø - **Panel**\nšŸ‘® - **Moderation**\nā” - **Other**`);
const moderationEmbed = new Discord.MessageEmbed()
.setColor(color)
.setAuthor(`Config\n`)
.setDescription('To get more information about a certain command, use ``' + prefix +
'help [command]``.\n\nā€¢``test``, ``test2``, ``test3``.');
try {
const filter = (reaction, user) => {
return (reaction.emoji.name === 'āš™ļø' || 'šŸ‘®' || 'ā”') && user.id === message.author.id;
};
message.delete();
message.channel.send(helpEmbed).then(embedMsg => {
embedMsg.react("āš™ļø")
.then(embedMsg.react("šŸ‘®"))
.then(embedMsg.react("ā”"))
embedMsg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === 'āš™ļø') {
embedMsg.edit(helpEmbed);
} else if (reaction.emoji.name === 'šŸ‘®') {
embedMsg.edit(moderationEmbed);
} else if (reaction.emoji.name === 'ā”') {
message.reply('test.');
}
})
.catch(collected => {
message.reply('didnt work.');
});
});
} catch (e) {
console.log(e.stack);
}
}
}
Used a collector.
const collector = embedMsg.createReactionCollector(filter);
collector.on('collect', (reaction, user) => {
reaction.users.remove(user.id); // remove the reaction
if (reaction.emoji.name === 'āš™ļø') {
embedMsg.edit(helpEmbed);
} else if (reaction.emoji.name === 'šŸ› ļø') {
embedMsg.edit(utilityEmbed);
} else if (reaction.emoji.name === 'šŸ‘®') {
embedMsg.edit(moderationEmbed);
}
});

How to make a role through reactions in discord.js?

I've been at this all morning. I'm currently working on a function that effectively creates a role, with one click on a reaction, but it has been throwing up the same error for a lifetime now.
Here's my code:
client.on('message', async message => {
let AdminRole = message.member.roles.cache.find(role => role.name === "Server Moderators")
let RulerRole = message.member.roles.cache.find(role => role.name === "The Supreme Boomers")
let RatRole = message.member.roles.cache.find(role => role.name === "Rat Majesty Robin")
if (message.member.roles.cache.has(AdminRole)) {
} else if (message.member.roles.cache.has(RulerRole)) {
} else if (message.member.roles.cache.has(RatRole)) {
}
if (message.content === `${prefix}createBumpRole`) {
message.channel.send("Do you want to create a role for bumping? Click on either the check mark or cross depending on your choice.")
message.react("āœ…") | message.react("āŒ")
}
const filter = (reaction, user) => {
return ['āœ…', 'āŒ'].includes(reaction.emoji.name) && user.id === message.author.id;
};
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === 'āœ…') {
message.guild.cache.roles.create({
data: {
name: 'bumping ping',
color: 'BLUE',
},
reason: 'We needed a role for people who regularly bump the server.',
})
} else if (reaction.emoji.name === 'āŒ') {
message.reply('Thank you for the response. You have exited this program.');
}
})
});

remove message.author reaction

I wanted to remove the user's reaction when he reacts, leaving only one, the bot's own
I've already seen some tutorials on this, but I haven't found this method
const test = new Discord.MessageEmbed()
.setColor("random")
.setTitle("help")
.setDescription(`**this a test**`)
message.channel.send(test).then(msg => {
msg.react('šŸ“š').then(r => {
msg.react('šŸ“Œ').then(r => {
})
})
const hiFilter = (reaction, user) => reaction.emoji.name === 'šŸ“š' && user.id === message.author.id;
const hi2Filter = (reaction, user) => reaction.emoji.name === 'šŸ“Œ' && user.id === message.author.id;
const edit = msg.createReactionCollector(hiFilter);
const edit2 = msg.createReactionCollector(hi2Filter);
edit.on('collect', r2 => {
test.setTitle("test edited")
test.setDescription("edited")
msg.edit(test)
})
})
This code removes the reaction if the ID of the user that added the reaction is not your bot's ID. Replace <Client> with whatever you've declared your Discord client as.
edit.on('collect', (r2, user) => {
if (user.id !== <Client>.user.id) r2.remove().catch(console.log);
test.setTitle("test edited");
test.setDescription("edited");
msg.edit(test);
});

Categories