It seems that the issue at hand is your expectation for .then
to be triggered once the time limit is reached. In actuality, reaching the time limit is considered an "error" according to the .awaitMessages
Example:
...
// Errors: ['time'] treats hitting the time limit as an error
channel.awaitMessages(filter, { max: 4, time: 60000, errors: ['time'] })
.then(collected => console.log(collected.size))
.catch(collected => console.log(`After a minute, only ${collected.size} out of 4 voted.`));
To have your code function as intended, simply add a .catch
statement following your channel.awaitMessages
call like so:
message.channel.awaitMessages(filter, {
max: 200,
time: 30000,
errors: ['time']
})
.then(collected => {
message.channel.send(`Accountability is ${collected.size} of ${message.guild.members.size}, present or accounted for.`);
})
// The .catch will handle errors - including time's up being an error
.catch(collected => {
message.channel.send(`Accountability is ${collected.size} of ${message.guild.members.size}, present or accounted for.`);
});
Please Note: As pointed out by @Splinxyy in the comments, it's advisable to use .send
instead of the outdated .sendMessage
function.
Revision: You mentioned in the comments your interest in counting only unique users who send 'here'
in the channel. One approach would involve storing user ids in an array and checking if the id has already been counted using a filter array:
let uidHolder = [];
const filter = m => {
let id = m.author.id;
if (uidHolder.includes(id) || !m.content.startsWith('here'))
return false;
else {
uidHolder.push(id);
return true;
}
};
If you prefer another method, you could manipulate the collected
variable from .then
/ .catch
. Nevertheless, utilizing the filter
function may prove simpler.