I'm trying to make a bot that will delete messages with a certain amount of thumbsdown reactions. I'm having trouble identifying the count of a specific reaction on a message.
Basically, I've created a command that waits for messages and adds them to my msgarray. After each message, I want to go through the array and delete any messages with the specified amount of reactions.
This is what I have so far:
var msgarray = [];
const msgs = await message.channel.awaitMessages(msg => {
msgarray.push(msg);
for (i = 0; i < msgarray.length; i++) {
// I'm not sure where to go from here, I want to make an if statement that checks
// for a certain amount of thumbsdown reactions on the message
if (msgarray[i].reactions) {
// incomplete
}
}
});
This is my first time programming in javascript, so I apologize if this code doesn't make much sense.
TextChannel.awaitMessages() resolves with a Collection, so the msg parameter you're using is not a single message, but a Collection of multiple messages.
Also, it would be better to check messages only when they get a reaction, using the messageReactionAdd event, that fires every time someone adds a reaction.
It should look like this:
// this code is executed every time they add a reaction
client.on('messageReactionAdd', (reaction, user) => {
let limit = 10; // number of thumbsdown reactions you need
if (reaction.emoji.name == '👎' && reaction.count >= limit) reaction.message.delete();
});
Related
I am trying to test whether a message (string) sent from the user/client is containing a word and then it will pick 1 of 2 random responses and it works, however it sends the message way too many times.
client.on("messageCreate", message => {
if(message.content.includes("dream")) {
var msgnumber= (Math.floor((Math.random() * 2) + 1));
console.log(msgnumber);
if (msgnumber===1) {
message.channel.send("did someone say dream!?");
} else if (msgnumber===2) {
message.channel.send("why we talkin' about dream... huh!?")
}
}
})
It does pick a random message if a message sent contains the keyword, one problem is it sends way too many times.
The output message
Your bot is activating itself. Whenever it posts a message containing "dream", it sees that message, and decides to post a reply, creating an infinite loop. Try adding the line if (msg.author.bot) return; right above where you check for if the message contains "dream". This will exit the function early and avoid the loop.
I tried to copy this piece of code over from a previous bot I have made on the same server with a different command name, and now it isn't deleting the messages and I can't figure out why not.
bot.on('message', msg =>{
if(!msg.author.bot && msg.content.startsWith(PREFIX))
{
let args = msg.content.substring(PREFIX.length).split(" ");
switch(args[0])
{
case 'ClearMessages':
if (msg.member.roles.cache.some(r=>["RNGesus"].includes(r.name)))
{
msg.channel.bulkDelete(args[1], true).catch(err=> {
console.error(err)
msg.channel.send("An error has occured while trying to delete messages")
});
msg.channel.send(`${args[1]} messages have been deleted`)
.then(msg =>{
msg.delete({timeout: 3000})
})
.catch(console.error)
}
break;
It also doesn't give any error messages in the console when the bot is supposed to be deleting the messages. If anybody can help at all it would be appreciated, I am fairly new to discord.js and one of my friends helped me make this. I tried to ask them for help but they couldn't figure it out either.
The first parameter of TextChannel.bulkDelete() is supposed to be an array/collection of messages or number of messages to delete. Obviously you're trying to give a number of messages to delete, however you're actually providing a string.
You need to first convert args[0] to a number using the unary plus operator (+), then round it to an integer using Math.floor() (or using a double bitwise NOT operator (~) if you want to be fancy and more efficient). As a suggestion, you should also put in a check to make sure the number is valid.
if (msg.member.roles.cache.some((r) => ['RNGesus'].includes(r.name))) {
if (+args[1] < 2 || +args[1] > 100)
return message.channel.send('The number must be at least 2 and at most 100');
msg.channel.bulkDelete(Math.floor(+args[1]), true).catch((err) => {
console.error(err);
msg.channel.send('An error has occured while trying to delete messages');
});
msg.channel
.send(`${args[1]} messages have been deleted`)
.then((msg) => {
msg.delete({ timeout: 3000 });
})
.catch(console.error);
}
How can i, after a time x, send me a message with the amount of emojis of a message. for example
5 reactions to the emoji: 😀
6 reactions to the emoji: 😍
You can use something like this:
message.channel.send('message').then(async (msg) => { // send the message
setTimeout(() => { // use the setTimeout function to add a delay before executing
switch (msg.reactions.cache.size) { // get the number of emojis reacted
case 1: { // if there was 1 emoji:
message.channel.send('something');
break;
}
case 2: { // if there was 2:
message.channel.send('something2');
break;
}
case 3: { // if there was 3:
message.channel.send('something3');
break;
}
// etc.
}
console.log(`${msg.reactions.cache.size} emojis were reacted.`);
}, 10000); // execute after whatever time (in milliseconds) you prefer, I'm using 10000 (10 seconds) as an example
});
After you get the number of emojis reacted (<message>.reactions.cache.size), you can do anything you want with it, I was just using the switch statement as an example.
You also don't have to use the setTimeout function, it's just the easiest way to trigger something after a delay. For example, you could trigger after an event, or anything else you might want.
I am assuming things right now cause for one I don't exactly get what you are trying to say and two, you are being a bit vague without giving any already existing code.
If the bot should send you the message only when a message with emojis is sent then all you have to do is, in your message event, check if the message contains an emoji. A simple regular expression would do: const emojiRegex = /<:[a-zA-Z0-9]{0,}:\d{18}>/ig;. Then check if the message contains any match.
const matches = message.content.match(emojiRegex);
if (matches) {
const owner = client.users.cache.get('user_id'); // enter your user id
owner.send(`There have been ${matches.length} emojis in message \`${message.id}\` at ${message.url}`);
}
First, Calculate the number of reactions on your message.
If the number of reactions on your message reaches 5, then tell your bot to send you a message!
I am working on a discord bot. I want to implement a feature like Tatsumaki's t!prune 5 (deletes 5 messages from the history).
Okay, lemme show you what I mean one way or another:
msg.channel.delete(2); // 2 is the number of messages being deleted.
// this is not a real function, just an example
Is there such a thing like what I showed?
Try to use the following
const fetchedMessages = await msg.channel.fetchMessages();
const amount = 50 // number of messages that should be deleted (max 50 otherwise you have to change the option limit property for .fetchMessages())
for (let i = 0; i < amount; i++) {
await fetchedMessages[i].delete()
}
You could also use the .bulkDelete() function:
await msg.channel.bulkDelete(AMOUNT)
I am new to discord.js but learned that I can delete my messages using bulkDelete and it will delete them all, even if they are older than 2 weeks. I clear my messages in a server I moderate manually once a month and needless to say it takes forever. I was wondering if anyone would be able to help me make a command that will do this automatically whenever I call it?
Thanks,
K
I would set up a recursive function that checks if there are messages in the channel (100 max every time): if there are no messages it stops otherwise it deletes them and restarts.
function clean(channel, limit = 100) {
return channel.fetchMessages({limit}).then(async collected => {
let mine = collected.filter(m => m.author.id == 'your_id_here'); // this gets only your messages
if (mine.size > 0) {
await channel.bulkDelete(mine, true);
clean(channel);
} else channel.send("The channel is now empty!").delete(5000); // this message is deleted after 5 s
});
}
You can adapt this idea to your existing command parser or, if you don't know how to implement this, try:
client.on('message', msg => {
if (msg.author.bot || msg.author != YOU) return;
// with YOU i mean your User object, to check permissions
let command = 'clean', // the name of your command
args = msg.content.split(' ');
if (args[0].toLowerCase() == command)
clean(msg.channel, !isNaN(args[1]) ? args[1] : undefined); //<-- THIS is how to use the function
// used a ternary operator to check if the other arg is a number
});
This is just a very basic implementation, there are a lot of better ways to detect commands.
I've just found a way to filter messages.
You can fetch messages and then check for each message if its yours
await message.channel.fetchMessages({
limit: 100
}).then((msgCollection) => {
msgCollection.forEach((msg) => {
if(msg.author.id == message.author.id) {
msg.delete();
}
})
});