Skip to content

Commit

Permalink
Merge pull request #38 from ParasY1724/main
Browse files Browse the repository at this point in the history
Add topic menu & topic based  ques fetching
  • Loading branch information
jinx-vi-0 authored Oct 10, 2024
2 parents c787831 + 62fb141 commit 6f16aeb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2,760 deletions.
84 changes: 80 additions & 4 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, GatewayIntentBits, Partials,EmbedBuilder } from 'discord.js';
import { Client, GatewayIntentBits, Partials, EmbedBuilder, ActionRowBuilder, ButtonBuilder,ButtonStyle } from 'discord.js';
import { LeetCode } from 'leetcode-query';
import cron from 'node-cron';
import keepAlive from './keep_alive.js';
Expand All @@ -18,6 +18,14 @@ const client = new Client({
]
});

function chunkArray(array, size) {
const chunked = [];
for (let i = 0; i < array.length; i += size) {
chunked.push(array.slice(i, i + size));
}
return chunked;
}

function calculateStreak(submissionCalendar) {
submissionCalendar = JSON.parse(submissionCalendar);
const datesWithSubmissions = Object.entries(submissionCalendar)
Expand Down Expand Up @@ -61,6 +69,13 @@ async function fetchLeetCodeProblems() {
const lc = new LeetCode();
let leetcodeProblems = []

const topics = [
'Array', 'String', 'Hash Table', 'Dynamic Programming', 'Math',
'Sorting', 'Greedy', 'Depth-First Search', 'Binary Search', 'Database',
'Breadth-First Search', 'Tree', 'Matrix', 'Two Pointers', 'Bit Manipulation',
'Stack', 'Design', 'Heap (Priority Queue)', 'Graph', 'Simulation'
];

client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);

Expand Down Expand Up @@ -234,17 +249,78 @@ client.on('messageCreate', async (message) => {
message.channel.send('Sorry, I could not fetch the streak info.');
}
}
else if (command === ';topics') {
const chunkedTopics = chunkArray(topics, 5); // Split topics into groups of 5
const rows = chunkedTopics.map(chunk =>
new ActionRowBuilder().addComponents(
chunk.map(topic =>
new ButtonBuilder()
.setCustomId(`topic_${topic.toLowerCase().replace(/\s+/g, '-')}`)
.setLabel(topic)
.setStyle(ButtonStyle.Secondary)
)
)
);

message.channel.send({
content: 'Choose a topic to get a random question:',
components: rows
});
}
else if (command === ';help') {
const helpMessage = `**Available Commands:**\n
\`;potd\` - Shows the LeetCode Daily Challenge\n
\`;random\` - Shows a random LeetCode problem\n
\`;random [difficulty]\` - Shows a random LeetCode problem (optional: specify difficulty)\n
\`;user <username>\` - Shows user Info\n
\`;streak <username>\` - Shows user Streak Info
\`;help\` - Shows help message`;
\`;streak <username>\` - Shows user Streak Info\n
\`;topics\` - Shows a list of LeetCode topics to choose from\n
\`;help\` - Shows this help message`;
message.channel.send(helpMessage);
}
});

client.on('interactionCreate', async interaction => {
if (!interaction.isButton()) return;

if (interaction.customId.startsWith('topic_')) {
const selectedTopic = interaction.customId.replace('topic_', '');
await interaction.deferReply();

try {
const topicQuestions = await lc.problems({
categorySlug: '',
skip: 0,
limit: 300000,
filters: { tags: [selectedTopic] }
});

if (topicQuestions.questions.length === 0) {
await interaction.editReply('No questions found for this topic.');
return;
}

const randomQuestion = topicQuestions.questions[Math.floor(Math.random() * topicQuestions.questions.length)];

const questionLink = `https://leetcode.com/problems/${randomQuestion.titleSlug}/`;
const embed = new EmbedBuilder()
.setTitle(`Random ${selectedTopic.replace(/-/g, ' ')} Question: ${randomQuestion.title}`)
.setURL(questionLink)
.setColor(0x0099FF)
.addFields(
{ name: 'Difficulty', value: randomQuestion.difficulty, inline: true },
{ name: 'Link', value: `[Solve Problem](${questionLink})`, inline: true },
{ name: 'Acceptance Rate', value: `${randomQuestion.acRate.toFixed(2)}%`, inline: true }
)
.setFooter({ text: 'Good luck solving this problem!' });

await interaction.editReply({ embeds: [embed], components: [] });
} catch (error) {
console.error('Error fetching topic question:', error);
await interaction.editReply('Sorry, I could not fetch a question for this topic.');
}
}
});

keepAlive();

client.login(process.env.TOKEN);
Loading

0 comments on commit 6f16aeb

Please sign in to comment.