Skip to content

Commit

Permalink
Collect lost votes that are cast while the bot is shut down.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Oct 29, 2024
1 parent 3e25a2e commit f690b4b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
34 changes: 33 additions & 1 deletion presidential-election.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,35 @@ function IsElectionStarted(users) {

async function ProcessLostVotes() {
console.log('ProcessLostVotes');
const guild = await DiscordUtil.GetMainDiscordGuild();
const channel = await guild.channels.fetch(channelId);
if (!channel) {
console.log('Failed to find the president-vote channel.');
return;
}
const lostVotes = [];
const messages = await channel.messages.fetch();
for (const [messageId, message] of messages) {
await message.fetch();
for (const [reactionId, reaction] of message.reactions.cache) {
let reactions;
try {
reactions = await reaction.users.fetch();
} catch (error) {
console.log('Warning: problem fetching votes!');
reactions = [];
}
for (const [voterId, voter] of reactions) {
if (!voter.bot) {
lostVotes.push({ reaction, voter });
}
}
}
}
for (const vote of lostVotes) {
await CheckReactionForPresidentialVote(vote.reaction, vote.voter, false);
break; // Stop after processing 1 vote for now in case there's a lot of lost votes.
}
}

function CalculateUnixTimestampOfElectionEndForThisMonth() {
Expand Down Expand Up @@ -124,7 +153,7 @@ function GetCandidateByMessageId(messageId) {
return null;
}

async function CheckReactionForPresidentialVote(reaction, discordUser) {
async function CheckReactionForPresidentialVote(reaction, discordUser, notifyVoter) {
if (discordUser.bot) {
return;
}
Expand All @@ -145,6 +174,9 @@ async function CheckReactionForPresidentialVote(reaction, discordUser) {
await reaction.users.remove(discordUser);
const firstVote = voter.presidential_election_vote ? false : true;
await voter.setPresidentialElectionVote(candidate.commissar_id);
if (!notifyVoter) {
return;
}
const guild = await DiscordUtil.GetMainDiscordGuild();
const voterMember = await guild.members.fetch(voter.discord_id);
if (voterMember) {
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ async function Start() {
await cu.seenNow();
await Ban.HandlePossibleReaction(messageReaction, user, true);
await zerg.HandleReactionAdd(messageReaction, user);
await PresidentialElection.CheckReactionForPresidentialVote(messageReaction, user);
await PresidentialElection.CheckReactionForPresidentialVote(messageReaction, user, true);
});

discordClient.on('messageReactionRemove', async (messageReaction, user) => {
Expand Down

0 comments on commit f690b4b

Please sign in to comment.