Skip to content

Commit

Permalink
fix(voteManager): add recurring task for removing expired votes
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Mar 12, 2024
1 parent 187b94c commit 88c9251
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ clusterManager.on('clusterCreate', async (cluster) => {

const voteManager = new VoteManager(clusterManager);
voteManager.on('vote', async (vote) => {
await voteManager.incrementAndScheduleVote(
vote.user,
(await getUsername(clusterManager, vote.user)) ?? undefined,
);
const username = await getUsername(clusterManager, vote.user) ?? undefined;
await voteManager.incrementUserVote(vote.user, username);
await voteManager.addVoterRole(vote.user);
await voteManager.announceVote(vote);
});

Expand Down
27 changes: 11 additions & 16 deletions src/managers/VoteManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ export type TopggEvents = {
};

export class VoteManager extends EventEmitter {
private scheduler = new Scheduler();
private scheduler: Scheduler;
private cluster: ClusterManager;

constructor(cluster: ClusterManager) {
constructor(cluster: ClusterManager, scheduler = new Scheduler()) {
super();
this.cluster = cluster;
this.scheduler = scheduler;
this.scheduler.addRecurringTask('removeVoterRole', 60 * 60 * 1_000, async () => {
const expiredVotes = await db.userData.findMany({ where: { lastVoted: { lt: new Date().getTime() } } });
for (const vote of expiredVotes) {
this.emit('voteExpired', vote.userId);
await this.removeVoterRole(vote.userId);
}
});
}

on<K extends keyof TopggEvents>(event: K, listener: (data: TopggEvents[K]) => void): this {
Expand All @@ -34,19 +42,6 @@ export class VoteManager extends EventEmitter {
return super.once(event, listener);
}

async incrementAndScheduleVote(userId: string, username?: string) {
await this.incrementUserVote(userId, username);
await this.addVoterRole(userId);

const taskName = `voteExpired-${userId}`;
if (this.scheduler.taskNames.includes(taskName)) this.scheduler.stopTask(taskName);

this.scheduler.addTask(`voteExpired-${userId}`, 12 * 60 * 60 * 1000, async () => {
this.emit('voteExpired', userId);
await this.removeVoterRole(userId);
});
}

async getUserVoteCount(userId: string) {
const user = await db.userData.findUnique({ where: { userId } });
return user?.voteCount ?? 0;
Expand Down Expand Up @@ -91,7 +86,7 @@ export class VoteManager extends EventEmitter {
`,
)
.setFooter({ text: `This is your ${voteCount}${ordinalSuffix} time voting!` })
.setColor('Green'),
.setColor('Orange'),
],
});
}
Expand Down
9 changes: 4 additions & 5 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,18 @@ export const modifyUserRole = async (
cluster: ClusterClient<Client> | ClusterManager,
action: 'add' | 'remove',
userId: Snowflake,
guildId: Snowflake,
guildId: Snowflake = SUPPORT_SERVER_ID,
roleId: Snowflake,
) => {
await cluster.broadcastEval(
async (client, ctx) => {
const guild = client.guilds.cache.get(ctx.guildId);
const voterRole = guild?.roles.cache.find(({ id }) => id === ctx.roleId);
const role = guild?.roles.cache.find(({ id }) => id === ctx.roleId);
const member = await guild?.members.fetch(ctx.userId).catch(() => null);
if (!guild || !voterRole) return;

// add or remove role
await member?.roles[ctx.action](voterRole).catch(() => null);
if (guild && role) return await member?.roles[ctx.action](role).catch(() => null);
},
{ guildId: SUPPORT_SERVER_ID, context: { userId, roleId, guildId, action } },
{ guildId, context: { userId, roleId, guildId, action } },
);
};

0 comments on commit 88c9251

Please sign in to comment.