Skip to content

Commit

Permalink
Merge branch 'main' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
ag21o9 authored Oct 8, 2024
2 parents d5c09c3 + c787831 commit bac7786
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 5 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/greetings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Greetings

on: [pull_request_target, issues]

jobs:
greeting:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- uses: actions/first-interaction@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
issue-message: "👋 Thank you for raising an issue! We appreciate your effort in helping us improve. Our DDoL team will review it shortly. Stay tuned!"
pr-message: " 🎉 Thank you for your contribution! Your pull request has been submitted successfully. A maintainer from DDoL team will review it as soon as possible. We appreciate your support in making this project better"
Binary file modified README.md
Binary file not shown.
103 changes: 98 additions & 5 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,36 @@ const client = new Client({
]
});

function calculateStreak(submissionCalendar) {
submissionCalendar = JSON.parse(submissionCalendar);
const datesWithSubmissions = Object.entries(submissionCalendar)
.filter(([ts, count]) => count > 0)
.map(([ts]) => new Date(ts * 1000))
.sort((a, b) => a - b);

if (datesWithSubmissions.length === 0) return 0;

let currentStreak = 0;
let hasSolvedToday = false;
const currentDate = new Date();
const lastSubmissionDate = datesWithSubmissions[datesWithSubmissions.length - 1];
// Check if the last submission was today
if (lastSubmissionDate.setHours(0, 0, 0, 0) === currentDate.setHours(0, 0, 0, 0)) {
hasSolvedToday = true;
currentDate.setDate(currentDate.getDate() + 1);
}
// Calculate streak
for (let i = datesWithSubmissions.length - 1; i >= 0; i--) {
currentDate.setDate(currentDate.getDate() - 1);
if (datesWithSubmissions[i].setHours(0, 0, 0, 0) === currentDate.setHours(0, 0, 0, 0)) {
currentStreak += 1;
} else {
break;
}
}
return {currentStreak, hasSolvedToday};
}

async function fetchLeetCodeProblems() {
try {
const response = await axios.get('https://leetcode.com/api/problems/all/');
Expand Down Expand Up @@ -143,6 +173,7 @@ client.on('messageCreate', async (message) => {
message.channel.send('Sorry, I could not fetch a random LeetCode question.');
}


}

// LeetCode User Info
Expand All @@ -157,6 +188,57 @@ client.on('messageCreate', async (message) => {
}
const responseMessage = `username : **${username}**\nContest : ${contestInfo.userContestRanking.attendedContestsCount}\nRating : ${Math.round(contestInfo.userContestRanking.rating)}\nTop : ${contestInfo.userContestRanking.topPercentage}%\nBadge : ${contestInfo.userContestRanking.badge ? contestInfo.userContestRanking.badge : 'No badge'}`;
message.channel.send(responseMessage);

} else if (command === ';user' && args.length === 2) {
const username = args[1];
try {
const [userInfo, contestInfo] = await Promise.all([
lc.user(username),
lc.user_contest_info(username)
]);

if (!userInfo.matchedUser) {
message.channel.send(`User "${username}" not found.`);
return;
}

const user = userInfo.matchedUser;
const profile = user.profile;
const submitStats = user.submitStats;

const embed = new EmbedBuilder()
.setColor('#FFD700') // Gold color for the embed
.setTitle(`LeetCode Profile: **${username}**`)
.setThumbnail(profile.userAvatar)
.addFields(
{ name: '👤 Real Name', value: profile.realName || '*Not provided*', inline: true },
{ name: '🏆 Ranking', value: profile.ranking ? profile.ranking.toString() : '*Not ranked*', inline: true },
{ name: '🌍 Country', value: profile.countryName || '*Not provided*', inline: true },
{ name: '🏢 Company', value: profile.company || '*Not provided*', inline: true },
{ name: '🎓 School', value: profile.school || '*Not provided*', inline: true },
{ name: '\u200B', value: '⬇️ **Problem Solving Stats**', inline: false },
{ name: '🟢 Easy', value: `Solved: ${submitStats.acSubmissionNum[1].count} / ${submitStats.totalSubmissionNum[1].count}`, inline: true },
{ name: '🟠 Medium', value: `Solved: ${submitStats.acSubmissionNum[2].count} / ${submitStats.totalSubmissionNum[2].count}`, inline: true },
{ name: '🔴 Hard', value: `Solved: ${submitStats.acSubmissionNum[3].count} / ${submitStats.totalSubmissionNum[3].count}`, inline: true },
{ name: '📊 Total', value: `Solved: ${submitStats.acSubmissionNum[0].count} / ${submitStats.totalSubmissionNum[0].count}`, inline: true }
);

// Add contest info
if (contestInfo.userContestRanking) {
embed.addFields(
{ name: '🚩 **Contest Info**', value: `\`\`\`Rating: ${Math.round(contestInfo.userContestRanking.rating)}\nRanking: ${contestInfo.userContestRanking.globalRanking}\nTop: ${contestInfo.userContestRanking.topPercentage.toFixed(2)}%\nAttended: ${contestInfo.userContestRanking.attendedContestsCount}\`\`\`` }
);
}

// Add badges if any
if (user.badges && user.badges.length > 0) {
const badgeNames = user.badges.map(badge => badge.displayName).join('\n•');
embed.addFields({ name: '🏅 Badges', value: "•"+badgeNames, inline: false });
}

message.channel.send({ embeds: [embed] });


} catch (error) {
console.error('Error fetching user info:', error);
message.channel.send('Sorry, I could not fetch the user info.');
Expand Down Expand Up @@ -219,13 +301,24 @@ client.on('messageCreate', async (message) => {
} else if (command === ';streak' && args.length === 2) {
const username = args[1];
try {
const streakInfo = await lc.user(username);
const user = await lc.user(username);
let streakInfo = 0;
let hasSolvedToday = false;
if(user.matchedUser) {
({ currentStreak: streakInfo, hasSolvedToday } = calculateStreak(user.matchedUser.submissionCalendar));
}

let streakMessage;
if (streakInfo.consecutiveDays > 0) {
streakMessage = `**${username}** has solved a problem for ${streakInfo.consecutiveDays} consecutive days! Keep it up!`;
if (streakInfo > 0) {
if (hasSolvedToday) {
streakMessage = `🎉 **${username}** has solved a problem for ${streakInfo} consecutive days! Great work, keep it up! 💪`;
} else {
streakMessage = `⚠️ **${username}** has solved a problem for ${streakInfo} consecutive days! Solve today's problem to maintain your streak and prevent it from resetting! 🔄`;
}
} else {
streakMessage = `**${username}** does not have a streak yet. Start solving problems to build your streak!`;
}
streakMessage = `❌ **${username}** does not have a streak yet. Start solving problems today to build your streak! 🚀`;
}

message.channel.send(streakMessage);
} catch (error) {
console.error('Error fetching streak info:', error);
Expand Down

0 comments on commit bac7786

Please sign in to comment.