Skip to content

Commit

Permalink
fix: fix vote timestamp not getting set properly
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed May 27, 2024
1 parent 24f46cc commit 343b568
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ model userData {
// username is only guarenteed to be set and/or used for blacklisted users
username String?
locale String?
lastVoted Int?
lastVoted DateTime?
voteCount Int @default(0)
blacklistedFrom hubBlacklist[]
// if user has seen the welcome message when they first use the network
Expand Down
8 changes: 3 additions & 5 deletions src/managers/VoteManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export class VoteManager extends EventEmitter {
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() } },
});
const expiredVotes = await db.userData.findMany({ where: { lastVoted: { lt: new Date() } } });
for (const vote of expiredVotes) {
this.emit('voteExpired', vote.userId);
await this.removeVoterRole(vote.userId);
Expand All @@ -50,7 +48,7 @@ export class VoteManager extends EventEmitter {
}

async incrementUserVote(userId: string, username?: string) {
const lastVoted = new Date().getTime();
const lastVoted = new Date();
return await db.userData.upsert({
where: { userId },
create: {
Expand Down Expand Up @@ -88,7 +86,7 @@ export class VoteManager extends EventEmitter {
`,
)
.setFooter({ text: `This is your ${voteCount}${ordinalSuffix} time voting!` })
.setColor('Orange'),
.setColor('Green'),
],
});
}
Expand Down
10 changes: 7 additions & 3 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,14 @@ export const hasVoted = async (userId: Snowflake): Promise<boolean> => {
};

export const userVotedToday = async (userId: Snowflake): Promise<boolean> => {
const res = await db.userData.findFirst({ where: { userId } });
const res = await db.userData.findFirst({
where: {
userId,
lastVoted: { gte: new Date(Date.now() - 60 * 60 * 24 * 1000) },
},
});

const oneDay = Date.now() - 60 * 60 * 24 * 1000;
return (res?.lastVoted && res.lastVoted > oneDay) === true;
return Boolean(res?.lastVoted);
};

export const yesOrNoEmoji = (option: unknown, yesEmoji: string, noEmoji: string) => {
Expand Down

0 comments on commit 343b568

Please sign in to comment.