Skip to content

Commit

Permalink
telegram next 5 contests
Browse files Browse the repository at this point in the history
  • Loading branch information
dashroshan committed Sep 29, 2023
1 parent 434e265 commit b65a67e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
5 changes: 5 additions & 0 deletions database/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ module.exports.getContestsStartingSoon = async function () {
return contests;
}

module.exports.getNextXContests = async function (x) {
let contests = await contestSchema.find({ notified: false }).sort({ start: 1 }).limit(x);
return contests;
}

// Return an array of contests which start in coming X days
module.exports.getContestsStartingInXDays = async function (days) {
let contests = await contestSchema
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const contestsScrapingLoop = require('./loops/contests scraping');
const contestsMessageLoop = require('./loops/contests message');
const problemMessageLoop = require('./loops/problem message');
const joiningMessage = require('./utility/joining message');
const { tokenTest, tokenProd, mongourlTest, mongourlProd, isProduction, tokenTelegram } = require('./config.json');
const { tokenTest, tokenProd, mongourlTest, mongourlProd, isProduction, tokenTelegramProd, tokenTelegramTest, channelTelegramProd, channelTelegramTest } = require('./config.json');
const { Client, Collection, GatewayIntentBits, EmbedBuilder, PermissionFlagsBits, ActivityType } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const TelegramBot = require('node-telegram-bot-api');

const tokenTelegram = (isProduction) ? tokenTelegramProd : tokenTelegramTest;
client.channelTelegram = (isProduction) ? channelTelegramProd : channelTelegramTest;
client.telegramBot = new TelegramBot(tokenTelegram, { polling: true });

// Initilize mongoDB connection
Expand Down
47 changes: 35 additions & 12 deletions loops/contests message.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { EmbedBuilder } = require('discord.js');
const { channelTelegram } = require("../config.json")

// Data about the different supported platforms
const platforms = {
Expand All @@ -9,32 +8,56 @@ const platforms = {
'codeforces': 'CodeForces',
'atcoder': 'AtCoder',
'geeksforgeeks': 'Geeksforgeeks',
'codingninjas': 'Coding Ninjas'
'codingninjas': 'CodingNinjas',
'hackerearth': 'HackerEarth',
}

const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

function formatAMPM(date) {
let hours = date.getHours();
let minutes = date.getMinutes();
let ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0' + minutes : minutes;
let strTime = hours.toString().padStart(2, "0") + ':' + minutes.toString().padStart(2, "0") + ampm;
return strTime;
}
// Send notifications for the contests starting soon
async function notify(client) {
const contests = await client.database.getContestsStartingSoon();
if (contests.length === 0) return; // No contests starting soon

const nextContests = await client.database.getNextXContests(5);

// Format the information about the contests starting soon for the embed body
let respStr = "";
let telegramStr = "🔥 Coding contest in an hour 🔥";
let telegramBtns = [];
for (let i = 0; i < contests.length; i++) {
let contestData = contests[i];
let telegramStr = `Coding contest${contests.length == 1 ? "" : "s"} starting soon.\nGood luck everyone 💪`;
for (let i = 0; i < contests.length + nextContests.length; i++) {
let contestData;
if (i == contests.length) telegramStr += `\n\n❇️❇️ NEXT ${nextContests.length} CONTESTS ❇️❇️`;
if (i >= contests.length)
contestData = nextContests[i - contests.length];
else
contestData = contests[i];

let hours = Math.floor(contestData['duration'] / 3600);
let mins = Math.floor((contestData['duration'] / 60) % 60);
let time = `${hours} ${hours === 1 ? 'hour' : 'hours'}${mins === 0 ? '' : (' and ' + mins + ' minutes')}`;
telegramStr += `\n\n❇️ ${contestData['name']}\n🎲 ${platforms[contestData['platform']]}\n⏰ ${time}`;
telegramBtns.push({ text: `${platforms[contestData['platform']]} Contest`, url: contestData['url'] });
respStr += `**[${contestData['name']}](${contestData['url']})**\n:dart: **Platform:** ${platforms[contestData['platform']]}\n:calendar: **Start:** <t:${contestData['start']}:R>\n:stopwatch: **Duration:** ${time}`;
if (i !== contests.length - 1) respStr += "\n\n";
let date = new Date(contestData['start'] * 1000);
let formattedDate = `${date.getDate()} ${months[date.getMonth()]} ${date.getFullYear()} at ${formatAMPM(date)} IST`
telegramStr += `\n\n[${contestData['name']}](${contestData['url']})\n🎲 ${platforms[contestData['platform']]}\n📅 ${formattedDate}\n⏰ ${time}`;

if (i < contests.length) {
respStr += `**[${contestData['name']}](${contestData['url']})**\n:dart: **Platform:** ${platforms[contestData['platform']]}\n:calendar: **Start:** <t:${contestData['start']}:R>\n:stopwatch: **Duration:** ${time}`;
if (i !== contests.length - 1) respStr += "\n\n";
}
}

// Send message on telegram channel
client.telegramBot.sendMessage(channelTelegram, telegramStr, {
parse_mode: 'Markdown', reply_markup: { inline_keyboard: [telegramBtns] }
client.telegramBot.sendMessage(client.channelTelegram, telegramStr, {
parse_mode: 'Markdown', disable_web_page_preview: true
});

// Create the embed to be sent to all channels
Expand Down

0 comments on commit b65a67e

Please sign in to comment.