remove message.author reaction - javascript

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);
});

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!!');
}
});
};

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

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

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`);
});
})
}

missing .then's but idk where to put

So i think i quite forgot some .then's beause the bot sends the B emoji message instanntly without a reaction from the user and even when i would provide a "suggestion" then it wouldnt send it to the specific channel, but idk where i have to put the missing .then's. Can someone help me please? I tried to figure it out myself and tested some but it didn't make anything better.
execute(message, client, args) {
const Discord = require('discord.js');
let Embed = new Discord.MessageEmbed()
.setColor('0x0099ff')
.setDescription(`Suggestion categories`)
.addField(`For what you want to suggest something?`, `\nA: I want to suggest something for the Website/Servers/Discord Server\nB: I want to suggest something for the CloudX Bot \n\nPlease react to this message with A or B`)
message.channel.send(Embed).then(function (message) {
message.react("🇦").then(() => {
message.react("🇧")
const filter = (reaction, user) => {
return ['🇦', '🇧'].includes(reaction.emoji.name) && user.id;
}
message.awaitReactions(filter, { max: 1 })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '🇦') {
const filter = m => m.author.id === message.author.id;
message.channel.send(`Please provide a suggestion for the Website/Servers/Discord Server or cancel this command with "cancel"!`).then(() => {
message.channel.awaitMessages(filter, { max: 1, })
.then(async (collected) => {
if (collected.first().content.toLowerCase() === 'cancel') {
message.reply("Your suggestion has been cancelled.")
}
else {
let embed1 = new Discord.MessageEmbed()
.setColor('0x0099ff')
.setAuthor(message.author.tag)
.addField(`New Suggestion:`, `${collected.first().content}`)
.setFooter(client.user.username, "attachment://CloudX.png")
.setTimestamp();
const channel = await client.channels.fetch("705781201469964308").then(() => {
channel.send({embed: embed1, files: [{
attachment:'CloudX.png',
name:'CloudX.png'
}]})
message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`)
})
}
})
})
}
if (reaction.emoji.name === '🇧') {
const filter = m => m.author.id === message.author.id;
message.channel.send(`Please provide a suggestion for the CloudX Bot or cancel this command with "cancel"!`).then(() => {
message.channel.awaitMessages(filter, { max: 1, })
.then(async (collected) => {
if (collected.first().content.toLowerCase() === 'cancel') {
message.reply("Your suggestion has been cancelled.")
}
else {
let embed2 = new Discord.MessageEmbed()
.setColor('0x0099ff')
.setAuthor(message.author.tag)
.addField(`New Suggestion:`, `${collected.first().content}`)
.setFooter(client.user.username, "attachment://CloudX.png")
.setTimestamp();
const channel = await client.channels.fetch("702825446248808519").then(() => {
channel.send({embed: embed2, files: [{
attachment:'CloudX.png',
name:'CloudX.png'
}]})
message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`)
})
}
})
})
}
})
})
})
},
I would suggest learning await/async functions.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
This will clean up your code and keep things steady without five thousand .then()
async execute(message, client, args) {
const { MessageEmbed } = require('discord.js');
const embed = new MessageEmbed()
.setColor('#0099ff')
.setDescription(`Suggestion categories`)
.addField(`For what you want to suggest something?`, `\nA: I want to suggest something for the Website/Servers/Discord Server\nB: I want to suggest something for the CloudX Bot \n\nPlease react to this message with A or B`)
const question = message.channel.send(embed)
await question.react("🇦")
await question.react("🇧")
const filter = (reaction, user) => {
return ['🇦', '🇧'].includes(reaction.emoji.name) && user.id;
}
This is just part of it but you should be able to get the gist...

How to delete all user inputs except one command in specific channel?

I was working on a discord bot and for a verification channel. I want users to type only the /verify command: every message or command except /verify they type should get deleted automatically. How can I do this?
Current code:
if (command === "verify") {
if (message.channel.id !== "ChannelID") return;
let role = message.guild.roles.find(rol => rol.name === 'Member')
const reactmessage = await message.channel.send('React with 👌 to verify yourself!');
await reactmessage.react('👌');
const filter = (reaction, user) => reaction.emoji.name === '👌' && !user.bot;
const collector = reactmessage.createReactionCollector(filter, {
time: 15000
});
collector.on('collect', async reaction => {
const user = reaction.users.last();
const guild = reaction.message.guild;
const member = guild.member(user) || await guild.fetchMember(user);
member.addRole(role);
message.channel.send(`Verification Complete.. ${member.displayName}. You have got access to server. `)
});
message.delete();
}
You can add a check at the top of your client.on('message') listener:
client.on('message', message => {
let verified = !!message.member.roles.find(role => role.name == 'Member');
// ... command parsing ect...
if (!verified && command == 'verify') {...}
else if (verified) {
// other commands...
}
});

Categories