Deleting message on reaction | Discord.JS - javascript

My problem is I want to delete d_msg after the reaction of the user. But it forgets what d_msg is and can't delete it. How can I fix it?
function English(thechannel, message) {
client.channels.cache.get(thechannel).send("Do u speak English?").then(d_msg => {
d_msg.react("✅");
d_msg.react("❌");
client.on('messageReactionAdd', async (reaction, user) => {
if (user.tag === theuser) {
if (reaction.emoji.name === '✅') {
var role = message.guild.roles.cache.find(role => role.name === "English");
message.member.roles.add(role);
}
}
d_msg.delete();
});
});
}

const Filter = (reaction, user) => {return (reaction.emoji.name == "✅" || reaction.emoji.name == "❌") && user.id == message.author.id};
message.reply("Do you speak English? \n\n *This prompt will disappear in 30 seconds.*").then(async messageSent => {
await messageSent.react("✅");
await messageSent.react("❌");
messageSent.awaitReactions(Filter, {max: 1, time: 30000, errors: ["time"]}).then(collected => {
messageSent.reactions.removeAll();
if (collected.first().emoji.name == "✅") {
messageSent.edit("You do speak English!");
} else if (collected.first().emoji.name == "❌") {
messageSent.edit("You do not speak English!");
}
}).catch(e => {
messageSent.edit("This prompt expired.");
messageSent.reactions.removeAll();
});
});

Related

"Cannot read property 'id' of undefined" when trying to get a user id

during the development of my bot I encountered the following error:
Cannot read property 'id' of undefined
This is my code so far:
const msg = message.channel.send(gvg).then(async msg => {
await msg.react('🟢');
await msg.react('🟠');
await msg.react('🔴');
await msg.react('⚙️');
const filter = (reaction) => ['🟢', '🟠', '🔴', '⚙️'].includes(reaction.emoji.name);
const collector = msg.createReactionCollector(filter,{ time: 30000 }, {dispose : true });
collector.on('collect', async (reaction, user ) => {
const member = message.guild.members.cache.get(user.id);
if (reaction.emoji.name === '🟢' ) {
member.roles.add('845380394513596446');
}
if (reaction.emoji.name === '🟠') {
member.roles.add('845380453597708299')
}
if (reaction.emoji.name === '🔴') {
member.roles.add('845380484964024330')
}
if (reaction.emoji.name === '⚙️') {
member.roles.remove('845380484964024330')
member.roles.remove('845380453597708299')
member.roles.remove('845380394513596446')
}
});
collector.dispose('remove', async (reaction, user, ) => {
const member = message.guild.members.cache.get(user.id);
if (reaction.emoji.name === '🟢' ) {
member.roles.remove('845380394513596446');
}
if (reaction.emoji.name === '🟠') {
member.roles.remove('845380453597708299')
}
if (reaction.emoji.name === '🔴') {
member.roles.remove('845380484964024330')
}
});
})
I hereby try to achieve that when retracting a reaction, the role of the user is removed again.

Undefined error with await message reaction in discord.js

I was trying to make it so when you choose a specific reaction it sends a message to a channel about you reaction. The reactions are added but when I react it returns an error saying that that emoji is undefined. When I console logged the collected.first() returned as undefined.
message.awaitReactions((reaction, user) => user.id == message.author.id && (reaction.emoji.name == '1️⃣' || reaction.emoji.name == '2️⃣' || reaction.emoji.name == '3️⃣' || reaction.emoji.name == '4️⃣' || reaction.emoji.name == '5️⃣'),
{ max: 1, time: 10000 }).then(collected => {
console.log(collected.first())
if (collected.first().emoji.name == '1️⃣') {
const channel = client.channels.cache.get(`764829720729747486`);
message.channel.send(`Successfully bought ${one}`)
channel.send(`<#${user.id}> Bought ${one}`).then(message => {
message.react('✅').then(r => {
message.react('❎');
});
message.awaitReactions((reaction, user) => user.id === '610294687701532691' && (reaction.emoji.name === '✅' || reaction.emoji.name === '❎'),
{ max: 1, time: 86400000 }).then(collected => {
if (collected.first().emoji.name === '✅') {
user.send('Order Accepted')
} else if (collected.first().emoji.name === '❎') {
user.send('Order Declined')
}
})
})
} else if (collected.first().emoji.name == '2️⃣') {
const channel = client.channels.cache.get(`764829720729747486`);
message.channel.send(`Successfully bought ${tow}`)
channel.send(`<#${user.id}> Bought ${tow}`).then(message => {
message.react('✅').then(r => {
message.react('❎');
});
message.awaitReactions((reaction, user) => user.id === '610294687701532691' && (reaction.emoji.name === '✅' || reaction.emoji.name === '❎'),
{ max: 1, time: 86400000 }).then(collected => {
if (collected.first().emoji.name === '✅') {
user.send('Order Accepted')
} else if (collected.first().emoji.name === '❎') {
user.send('Order Declined')
}
})
})
} else if (collected.first().emoji.name == '3️⃣') {
const channel = client.channels.cache.get(`764829720729747486`);
message.channel.send(`Successfully bought ${tho}`)
channel.send(`<#${user.id}> Bought ${tho}`).then(message => {
message.react('✅').then(r => {
message.react('❎');
});
message.awaitReactions((reaction, user) => user.id === '610294687701532691' && (reaction.emoji.name === '✅' || reaction.emoji.name === '❎'),
{ max: 1, time: 86400000 }).then(collected => {
if (collected.first().emoji.name === '✅') {
user.send('Order Accepted')
} else if (collected.first().emoji.name === '❎') {
user.send('Order Declined')
}
})
})
} else if (collected.first().emoji.name == '4️⃣') {
const channel = client.channels.cache.get(`764829720729747486`);
message.channel.send(`Successfully bought ${fo}`)
channel.send(`<#${user.id}> Bought ${fo}`).then(message => {
message.react('✅').then(r => {
message.react('❎');
});
message.awaitReactions((reaction, user) => user.id === '610294687701532691' && (reaction.emoji.name === '✅' || reaction.emoji.name === '❎'),
{ max: 1, time: 86400000 }).then(collected => {
if (collected.first().emoji.name === '✅') {
user.send('Order Accepted')
} else if (collected.first().emoji.name === '❎') {
user.send('Order Declined')
}
})
})
} else if (collected.first().emoji.name == '5️⃣') {
const channel = client.channels.cache.get(`764829720729747486`);
message.channel.send(`Successfully bought ${fif}`)
channel.send(`<#${user.id}> Bought ${fif}`).then(message => {
message.react('✅').then(r => {
message.react('❎');
});
message.awaitReactions((reaction, user) => user.id === '610294687701532691' && (reaction.emoji.name === '✅' || reaction.emoji.name === '❎'),
{ max: 1, time: 86400000 }).then(collected => {
if (collected.first().emoji.name === '✅') {
user.send('Order Accepted')
} else if (collected.first().emoji.name === '❎') {
user.send('Order Declined')
}
})
})
}
}).catch(err => {
message.reply(' Error')
console.log(err)
})
The problem is that you are not treating time as an error. As it is currently, after 10 seconds, the promise will resolve with a Collection regardless of how many reactions were collected. You're getting this error because Collection.prototype.first() will obviously return undefined on an empty collection.
If you'd like the promise to reject after the time is up (execute the catch function), you have to add an additional option to the Message.prototype.awaitReactions() function.
Also, you can compact your filter function by simply creating an array of possible emoji names and checking it against the current emoji name once.
message
.awaitReactions(
(reaction, user) =>
user.id == message.author.id &&
['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣'].includes(reaction.emoji.name),
{ max: 1, time: 10000, errors: ['time'] } // errors: ['time'] - reject after time is up
)
.then('...')
.catch('...');

discord.js awaitReactions filter to check if the user is in the same channel like bot

my filter doesn't work. I want, filter OUT if the bot reacts and if the channel isn't same as bot's, but it filters out all other reactions. If other member (without bot) is in the channel and probably isn't bot, it will filter out the reaction.
I hope you find what you need in my code, and help me :)
pauseFalse(True)Reactions is array, serverQueue is JSON object, song is string
var filter = (reaction, user) => {
return pauseFalseReactions.includes(reaction.emoji.name) && message.guild.member(user.id).voice.channel == message.guild.member(botID).voice.channel && user.id != botID && user != bot.user;
};
awaitReact(message, filter, 1, Number(song.secs), pauseFalseReactions, serverQueue);
function awaitReact(message, filter, max, time, reactions, serverQueue) {
message.awaitReactions(filter, { max: max, time: time, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name == '⏸' && reactions.includes('⏸')) {
Guild.findOneAndUpdate({
guildID: message.guild.id
}, {
musicBotPaused: true,
musicBotPlaying: false
}, function (err, result) {
if (err) {
return console.error(err);
}
});
serverQueue.connection.dispatcher.pause();
var newFilter = (reaction, user) => {
return pauseTrueReactions.includes(reaction.emoji.name) && message.guild.member(user.id).voice.channel == message.guild.member(botID).voice.channel && user.id != botID && user != bot.user;
};
message.reactions.removeAll().catch(error => console.error(error));
addReactions(message, pauseTrueReactions);
awaitReact(message, newFilter, max, (time + 250), pauseTrueReactions, serverQueue);
} else if (reaction.emoji.name == '▶️' && reactions.includes('▶️')) {
Guild.findOneAndUpdate({
guildID: message.guild.id
}, {
musicBotPaused: false,
musicBotPlaying: true
}, function (err, result) {
if (err) {
return console.error(err);
}
});
serverQueue.connection.dispatcher.pause();
var newFilter = (reaction, user) => {
return pauseFalseReactions.includes(reaction.emoji.name) && message.guild.member(user.id).voice.channel == message.guild.member(botID).voice.channel && user.id != botID && user != bot.user;
};
message.reactions.removeAll().catch(error => console.error(error));
addReactions(message, pauseFalseReactions);
awaitReact(message, newFilter, max, (time + 250), pauseFalseReactions, serverQueue);
} else if (reaction.emoji.name == '⏭') {
if (!serverQueue) {
const Embed = new Discord.MessageEmbed()
.setColor('#ff9745')
.setURL('https://discord.js.org/')
.setAuthor('Zigger', 'https://cdn.discordapp.com/app-icons/763859712340262972/010d590a764af0c59b999ad024cf89cb.png?size=128')
.setDescription('Ve frontě nic není.');
return message.channel.send(Embed);
}
message.channel.send(':fast_forward: **Přeskočeno** :thumbsup:');
serverQueue.connection.dispatcher.end();
} else if (reaction.emoji.name == '⏹') {
if (serverQueue) {
serverQueue.connection.dispatcher.end();
queue.delete(message.guild.id);
}
message.channel.send(':stop_button: **Zastaveno!** :thumbsup:');
}
})
.catch(user => {
if (user.id != botID || user != bot.user) {
return;
} else {
message.reactions.removeAll().catch(error => console.error(error));
}
});
}
This will always be false:
message.guild.member(user.id).voice.channel == message.guild.member(botID).voice.channel
Use
message.guild.member(user.id).voice.channelID == message.guild.member(botID).voice.channelID

Bot responds to its own reactions

someone can help?
5️⃣ = 5️⃣
var embed1 = new Discord.RichEmbed()
.setTitle("hjgsadgv")
message.channel.send(embed9)
.then(function (message) {
message.react("5️⃣")
.then(() => message.react("4️⃣"))
.then(() => message.react("3️⃣"))
.then(() => message.react("2️⃣"))
.then(() => message.react("1️⃣"))
const filter = (reaction, user) => {
return ['5️⃣', '4️⃣', '3️⃣', '2️⃣', '1️⃣'].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 === '5️⃣') {
message.reply('123');
}
else {
message.reply('321');
}
var embed2 = new Discord.RichEmbed()
.setTitle("uysygadk")
message.channel.send(embed10)
})
})
Bot responds to its own reactions
You can ignore bots by changing your filter to this:
const filter = (reaction, user) => {
return ['5️⃣', '4️⃣', '3️⃣', '2️⃣', '1️⃣'].includes(reaction.emoji.name) && user.id === message.author.id && !user.bot;
}
It basically checks the bot property of user and if it's true then the filter blocks it.

is there can delete message when click this reaction emoji DIscord.js

How to fix this ? i want to delete message when user click reaction X
client.on('message', async message => {
if (message.channel.id === emojiChannelID) {
try {
await message.react('✅');
await message.react('✖');
} catch(err) {
console.error(err);
}
}
});```
There's an message.awaitReaction() in discord.js, that will return reactions from users
// Filter for only
const filter = function(reaction, user) {
return reaction.emoji.name === '✅' || reaction.emoji.name === '✖';
}
// {...}
let reactionMessage = await message.react('✅');
// Make sure to set max: 1 so that the promise returns after the first reaction
let reactionCollection = await reactionMessage.awaitReactions(filter, { max: 1});
// reactionCollection is a Collection<string, MessageReaction>
// Use first() to get the first (and only)
let reaction = reactionCollection.first();
Kian here,
This code should work for you,
if you would like I can go through and explain each line :)
Have a good day chief!
async function emojiMessage(message, validReactions) {
for (const reaction of validReactions) await message.react(reaction);
const filter = (reaction, user) => validReactions.includes(reaction.emoji.name) && (!user.bot)
return message
.awaitReactions(filter, {
max: 1,
time: 42000
})
.then(collected => collected.first() && collected.first().emoji.name);
}
async function deleteMessage(message) {
const emoji = await emojiMessage(message, ["✅", "❌"]);
console.log(emoji)
// if the emoji is a tick:
if (emoji === "✅") {
// delete their message
console.log("tick")
if (message.deletable == true) {
console.log("can delete")
console.log("attempting to delete")
message.delete()
}
if (!message.deletable == false) {
"cannot delete"
}
} else if (emoji === "❌") { // if the emoji is a cross
/*
* do something else
*/
return;
}
}
client.on('message', message => {
if (message.channel.id === emojiChannelID) {
// runs the function
deleteMessage(message)
}
/*
* do something else
*/
})
Note:
First upload 🎉
I've tried my best to make the code understandable/work , if there is any issues feel free to comment, I'll fix it :)
Example Usage:
const m = await message.channel.send('hi!');
reactionDelete(m, message, 20000); // assuming 'message' is the actual sent message
async function reactionDelete (botMessage, playerMessage, timeout) {
const filter = (reaction, user) => {
return ['🗑️'].includes(reaction.emoji.name) && user.id === playerMessage.author.id;
};
botMessage.react('🗑️');
botMessage.awaitReactions(filter, { max: 1, time: timeout})
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '🗑️') {
botMessage.delete();
}
})
.catch(collected => {
if (botMessage.deletable) botMessage.reactions.removeAll();
});
};

Categories

Resources