Skip to content

Commit c787831

Browse files
authored
Merge pull request #34 from nidhi2026/dbranch
Add corrected current streak for streak command
2 parents d34ebc5 + f3d00ea commit c787831

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

bot.js

+46-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,36 @@ const client = new Client({
1818
]
1919
});
2020

21+
function calculateStreak(submissionCalendar) {
22+
submissionCalendar = JSON.parse(submissionCalendar);
23+
const datesWithSubmissions = Object.entries(submissionCalendar)
24+
.filter(([ts, count]) => count > 0)
25+
.map(([ts]) => new Date(ts * 1000))
26+
.sort((a, b) => a - b);
27+
28+
if (datesWithSubmissions.length === 0) return 0;
29+
30+
let currentStreak = 0;
31+
let hasSolvedToday = false;
32+
const currentDate = new Date();
33+
const lastSubmissionDate = datesWithSubmissions[datesWithSubmissions.length - 1];
34+
// Check if the last submission was today
35+
if (lastSubmissionDate.setHours(0, 0, 0, 0) === currentDate.setHours(0, 0, 0, 0)) {
36+
hasSolvedToday = true;
37+
currentDate.setDate(currentDate.getDate() + 1);
38+
}
39+
// Calculate streak
40+
for (let i = datesWithSubmissions.length - 1; i >= 0; i--) {
41+
currentDate.setDate(currentDate.getDate() - 1);
42+
if (datesWithSubmissions[i].setHours(0, 0, 0, 0) === currentDate.setHours(0, 0, 0, 0)) {
43+
currentStreak += 1;
44+
} else {
45+
break;
46+
}
47+
}
48+
return {currentStreak, hasSolvedToday};
49+
}
50+
2151
async function fetchLeetCodeProblems() {
2252
try {
2353
const response = await axios.get('https://leetcode.com/api/problems/all/');
@@ -180,13 +210,24 @@ client.on('messageCreate', async (message) => {
180210
} else if (command === ';streak' && args.length === 2) {
181211
const username = args[1];
182212
try {
183-
const streakInfo = await lc.user(username);
213+
const user = await lc.user(username);
214+
let streakInfo = 0;
215+
let hasSolvedToday = false;
216+
if(user.matchedUser) {
217+
({ currentStreak: streakInfo, hasSolvedToday } = calculateStreak(user.matchedUser.submissionCalendar));
218+
}
219+
184220
let streakMessage;
185-
if (streakInfo.consecutiveDays > 0) {
186-
streakMessage = `**${username}** has solved a problem for ${streakInfo.consecutiveDays} consecutive days! Keep it up!`;
221+
if (streakInfo > 0) {
222+
if (hasSolvedToday) {
223+
streakMessage = `🎉 **${username}** has solved a problem for ${streakInfo} consecutive days! Great work, keep it up! 💪`;
224+
} else {
225+
streakMessage = `⚠️ **${username}** has solved a problem for ${streakInfo} consecutive days! Solve today's problem to maintain your streak and prevent it from resetting! 🔄`;
226+
}
187227
} else {
188-
streakMessage = `**${username}** does not have a streak yet. Start solving problems to build your streak!`;
189-
}
228+
streakMessage = `❌ **${username}** does not have a streak yet. Start solving problems today to build your streak! 🚀`;
229+
}
230+
190231
message.channel.send(streakMessage);
191232
} catch (error) {
192233
console.error('Error fetching streak info:', error);

0 commit comments

Comments
 (0)