Skip to content

Commit

Permalink
Added Contest Reminder
Browse files Browse the repository at this point in the history
* Implemented Contest Reminder 1 hr before

* Update ready.js

* Update ready.js

* Update ready.js
  • Loading branch information
ParasY1724 authored Oct 16, 2024
1 parent 9db6cb7 commit 2a98a3c
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "/home/pred/go",
"program": "/home/pred/go/main.cpp",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
59 changes: 59 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
92 changes: 91 additions & 1 deletion events/ready.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { EmbedBuilder, bold } from "discord.js";
import cron from 'node-cron'
import axios from "axios";


export default async (client, lc = client.lc) => {
console.log(`Logged in as ${client.user.tag}!`);
Expand Down Expand Up @@ -29,4 +31,92 @@ export default async (client, lc = client.lc) => {
scheduled: true,
timezone: "Asia/Kolkata"
})
}

// New function to fetch upcoming contests
async function getUpcomingContests() {
try {
const response = await axios.post('https://leetcode.com/graphql', {
query: `{
topTwoContests {
title
startTime
duration
}
}`
}, {
headers: {
'Content-Type': 'application/json',
}
});


return response.data.data.topTwoContests.map(contest => ({
...contest,
start_time: new Date(contest.startTime * 1000),
href: `https://leetcode.com/contest/${contest.title.toLowerCase().replace(/\s+/g, '-')}`
}));
} catch (error) {
console.error('Failed to fetch upcoming contests:', error);
return [];
}
}


// Function to schedule reminders for contests
async function scheduleContestReminders() {
const contests = await getUpcomingContests();
// console.log("Working" , contests);
contests.forEach(contest => {
const reminderTime = new Date(contest.start_time.getTime() - 60 * 60 * 1000); // 1 hour before
const now = new Date();


if (reminderTime > now) {
const delay = reminderTime.getTime() - now.getTime();
console.log("Original Delay =>" , delay);
setTimeout(() => sendContestReminder(contest), delay);
console.log(`Scheduled reminder for ${contest.title} at ${reminderTime}`);
}
});
}


// Function to send contest reminder
async function sendContestReminder(contest) {


const embed = new EmbedBuilder()
.setTitle(contest.title)
.setURL(contest.href)
.setColor(0xfea116)
.addFields(
{ name: 'Start Time', value: contest.start_time.toUTCString(), inline: true },
{ name: 'Duration', value: `${contest.duration / 60} minutes`, inline: true },
{ name: 'Link', value: `[Join Contest](${contest.href})` }
)
.setFooter({ text: 'Contest starts in 1 hour!' });


for (const guild of client.guilds.cache.values()) {
for (const channel of guild.channels.cache.values()) {
// Type 0 => Text Channel
if (channel.type === 0) {
await channel.send({
content: bold(`@everyone, Upcoming LeetCode Contest Reminder!`),
embeds: [embed]
});
break;
}
}
}

}



// Refresh contest schedule every Friday
cron.schedule('0 0 * * 5', scheduleContestReminders, {
scheduled: true,
timezone: "UTC"
});
}

0 comments on commit 2a98a3c

Please sign in to comment.