I'm making a giveaway command for my server, all from my knowledge without following a tutorial.
I'm running into an issue. I don't know how to use the collector's information to determine if there is enough reactions or not, and to get a winner.
My code:
const filter = (reaction, user) => {
return reaction.emoji.name === '🎉' && user.id === message.author.id;
};
const collector = message.createReactionCollector({ filter, time: duration });
collector.on('collect', (reaction, user) => {
console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
});
collector.on('end', collected => {
console.log(`Collected ${collected.size} items`);
});
setTimeout(() => {
if (collector <= 1) {
message.channel.send("Not enough people reacted for me to draw a winner")
return}
let winner = (m) => m.reaction.cache.get("🎉").users.cache.filter((u) => !u.bot).random();
channel.send(`Congratulations ${winner} You just won the **${prize}**!`
);
}, duration );
Any help please? Thank you!
Related
In a pervious post I was having issues getting the bot to recognise reactions and the fix worked however and then changed it to react on a message that the bot says afterwards and now it isnt working again, I have tried changing the user condition so its the original command author but that didn't seem to work
So you run the code and it makes the embed perfectly and reacts to it however it doesnt recognise when you react and makes the timeout message
exports.run = async (client, message, args) => {
message.delete()
const MINUTES = 5;
const questions = [
{ answer: null, field: 'placeholdquestion' },
{ answer: null, field: 'placeholdquestion' },
{ answer: null, field: 'placeholdquestion' },
]; //to add more questions just add another line of the above code {answes: null, field: `Question Here`}
let current = 0;
const commanduser = message.author.id
// ...
// wait for the message to be sent and grab the returned message
// so we can add the message collector
const sent = await message.author.send(
`${questions[current].field}`,
);
const filter = (response) => response.author.id === message.author.id;
// send in the DM channel where the original question was sent
const collector = sent.channel.createMessageCollector(filter, {
max: questions.length,
time: MINUTES * 60 * 1000,
});
// fires every time a message is collected
collector.on('collect', (message) => {
//if (questions > 1 && questions < 10) {
// add the answer and increase the current index HERE
questions[current++].answer = message.content;
const hasMoreQuestions = current < questions.length; //change to be an imput of how many questions you want asked
if (hasMoreQuestions) {
message.author.send(
`${questions[current].field}`,
);
}
});
// fires when either times out or when reached the limit
collector.on('end', (collected, reason) => {
if (reason === 'time') {
return message.author.send(
`I'm not saying you're slow but you only answered ${collected.size} questions out of ${questions.length} in ${MINUTES} minutes. I gave up.`,
);
}
const embed = new MessageEmbed()
.setTitle("LOA Request")
.addFields(
{ name: 'placehold', value: questions[0].answer+'/10' },
{ name: 'placehold', value: questions[1].answer+'/10' },
{ name: 'placehold', value: questions[2].answer+'/10', inline: true },)
.setColor(`#1773BA`)
.setTimestamp()
.setThumbnail("https://media.discordapp.net/attachments/772915309714735205/795378037813805126/mvg_clean_2.png")
.setFooter("request by: " + message.author.tag);
;
message.channel.send(embed)
.then(function (message) {
message.react("👍")
message.react("👎")})
const filter = (reaction, user) => {
return ['👍', '👎'].includes(reaction.emoji.name) && user.id === commanduser; //changed to try and fix it didnt work as message.author.id or this
};
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] } )
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '👍') {
message.channel.send('you reacted with a thumbs up.');
}
else {
message.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
});
});
;
}
You have a slight logic error. You need to fit the code from your second filter to the message.awaitReactions inside of your message.channel.send(embed).then(function (message)...) method. In your code, the bot is trying to check for reactions from the original message, which you already deleted (since the awaitReactions is outside the scope of your function where you send and react to the embed).
Like this:
message.channel.send(embed)
.then(function (message) {
message.react("👍")
message.react("👎")
const filter2 = (reaction, user) => {
return ['👍', '👎'].includes(reaction.emoji.name) && user.id === commanduser;
};
message.awaitReactions(filter2, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '👍') {
message.channel.send('you reacted with a thumbs up.');
}
else {
message.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
});
})
I am writing a discord bot to ping a user at a set interval. And I want to know if there is a way to compile this to not have to copy and paste the entire script to have the same thing happen for other users.
client.on('message', message => {
if(message.content === '&ping zach forever') {
setInterval(() => {
var yourchannel = client.channels.cache.get('704506336339034115');
yourchannel.send('<#UserID>');
}, "5000");
}});
client.on('message', message => {
if(message.content === '&ping maxx forever') {
setInterval(() => {
var yourchannel = client.channels.cache.get('704506336339034115');
yourchannel.send('<#UserID>');
}, "5000");
}});
I have no experience with discord bots in particular, but as general JavaScript advice:
const users = ["zach", "maxx"];
client.on('message', message => {
const splits = message.content.split(' ');
if (splits.length === 3 && splits[0] === '&ping' && splits[2] === 'forever' && users.includes(splits[1])) {
setInterval(() => {
var yourchannel = client.channels.cache.get('704506336339034115');
yourchannel.send('<#UserID>');
}, "5000");
}
});
Simply expand the users array with the required names. You can also modify the array at runtime with .push() and .splice(). If you'd like the bot to trigger even if there is text after the command, check for splits.length >= 3 instead.
PS. As a general rule, using var is frowned upon these days. Use const for values only assigned once, and let otherwise.
Edit:
Here's a bit more fleshed out example, that includes hardcoded user ID's for each name, applies the duration (in seconds), and also includes a &stop command to stop pinging a certain person. (Code is not tested to run correctly, since I don't have a discord bot account, and don't want to make one either)
const users = new Map(Object.entries({
"zach": "UserID1",
"maxx": "UserID2"
}));
const active = new Map();
client.on('message', message => {
const splits = message.content.split(' ');
if (splits.length === 3 && splits[0] === '&ping' && users.has(splits[1])) {
const channel = client.channels.cache.get('704506336339034115');
const message = `<#${users.get(splits[1])}>`;
const time = Number(splits[2]);
if (isNaN(time) && splits[2] !== 'forever') {
// Passed a time that wasn't a number - possibly send back an error message
} else {
const id = setInterval(() => channel.send(message), 5000);
active.set(splits[1], id);
if (!isNaN(time)) setTimeout(() => clearInterval(id), time * 1000);
}
}
if (splits.length === 2 && splits[0] === '&stop') {
const id = active.get(splits[1]);
if (id) {
clearInterval(id);
active.delete(splits[1]);
}
}
});
After about 20 hours of searching for answers, editing the code, and trying new methods. I've given up and decided to ask here.
I'm absolutely sick of this piece of code.
What I'm trying to do is: 1. Wait for a message to receive 2 reactions 2. After 2 reactions, post message to a separate channel
EDIT: After some users recommended removing my stupidly placed console.log before my .then this code now proceeds to not wait for a reaction on a message and go through the entire process without posting a message to the specified channel
Please bear with me, I'm still learning.
Here's the code:
client.on('message', msg => {
const filter = (reaction, user) => reaction.emoji.name === '703033480090484756' && user.id === message.author.id;
msg.awaitReactions(filter, { max: 1, time: 30000, errors: ['time'] })
.then(collected => {
const starboard = new Discord.MessageEmbed()
.setColor(0xFF0000)
.setAuthor(`${msg.author.username}`, `${msg.author.avatarURL()}`)
.setDescription(`${msg.author.content}`)
client.channels.cache.get('714842643766444122').send(starboard)})
.catch(err => console.log(err), ('Passed! Added to starboard.'));
console.log('Added to Starboard!');
If you need more details, Please ask.
client.on('message', async msg => {
let rMessage = await msg.channel.send("Test Message");
await rMessage.react('703033480090484756');
const filter = (reaction, user) => reaction.emoji.name === '703033480090484756' && user.id === msg.author.id;
rMessage.awaitReactions(filter, { max: 1, time: 30000, errors: ['time'] })
.then(collected => {
const starboard = new Discord.MessageEmbed()
.setColor(0xFF0000)
.setAuthor(`${msg.author.username}`, `${msg.author.displayAvatarURL()}`)
//.setDescription(`${msg.author.content}`)
client.channels.cache.get('714842643766444122').send(starboard)})
.catch(err => console.log(err), ('Passed! Added to starboard.'));
console.log('Added to Starboard!');
You used .then on your console.log('Woah! Passed the filter...').
I have a little question.
I'm currently in the progress of trying out creating a tickets bot, and had the idea of having so when a user types !close, it would present him with an embed, asking him if he really does want to close it using reactions (:wastebasket: for Yes, :x: for No).
If the user reacts for Yes, the channel will close. As for No, the embed message will be deleted.
I will be glad to help.
You can use something a little like this:
const prevMsg = message
message.channel.send(embed).then(message => {
const questionMessage = message;
questionMessage.react('🗑️')
.then(() => questionMessage.react('❌'));
const filter = (reaction, user) => {
return ['🗑️', '❌'].includes(reaction.emoji.name) && user.id === prevMsg.author.id;
};
questionMessage.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '🗑️') {
// closing function here
} else {
questionMessage.delete();
}
})
})
If it didn't work, please let me know!
First time I use createReactionCollector.
It doesn't work, when I react the collector gets nothing.
But when I make the bot react, I get that reaction but, not mine.
const collector = startMessage.createReactionCollector(x => {
return true
}, { time: 5000 })
collector.on('collect', (r, collector) => {
console.log("collected");
})
collector.on('end', r => {
console.log(r)
})
That's because you're not using a valid filter, you need to pass the reaction and user parameters into the filter and use them like so:
const filter = (reaction, user) => reaction.emoji.name === '👌' && user.id === 'someID';
Then add the filter to the collector like so:
const collector = message.createReactionCollector(filter, { time: 15000 });
These are from the example of createReactionCollector() on the docs