Skip to content

Commit

Permalink
feat: remove oldest vote before casting new one on reaching max_votes…
Browse files Browse the repository at this point in the history
…_allowed
  • Loading branch information
MartinCupela committed Oct 23, 2024
1 parent 7774726 commit 734757c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,25 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
};

castVote = async (optionId: string, messageId: string) => {
const {max_votes_allowed, ownVotesByOptionId } = this.data;

const reachedVoteLimit =
max_votes_allowed && max_votes_allowed === Object.keys(ownVotesByOptionId).length;

if (reachedVoteLimit) {
let oldestVote = Object.values(ownVotesByOptionId)[0];
Object.values(ownVotesByOptionId).slice(1).forEach((vote) => {
if (
!oldestVote?.created_at ||
new Date(vote.created_at) < new Date(oldestVote.created_at)
) {
oldestVote = vote;
}
});
if (oldestVote?.id) {
await this.removeVote(oldestVote.id, messageId);
}
}
return await this.client.castPollVote(messageId, this.id as string, { option_id: optionId });
};

Expand Down

0 comments on commit 734757c

Please sign in to comment.