Skip to content

Commit

Permalink
Added Difficulty Parameter in Topics Command
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh7923 committed Oct 13, 2024
1 parent ac3f3f9 commit cbbe09b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
18 changes: 15 additions & 3 deletions interactions/buttons/topic.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { EmbedBuilder } from "discord.js";

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'
];

export default {
name: 'topic',
run: async (interaction, lc = interaction.client.lc) => {
const selectedTopic = interaction.customId.replace('topic_', '');
await interaction.deferReply();

const [,topicIndex,difficultyIndex] = interaction.customId.split("_")

const difficulty = difficultyIndex === '0' ? null : difficultyIndex === '1' ? 'EASY' : difficultyIndex === '2' ? 'MEDIUM' : 'HARD'
const topic = topics[topicIndex]


const topicQuestions = await lc.problems({
categorySlug: '',
skip: 0,
limit: 300000,
filters: { tags: [selectedTopic] }
filters: { tags: [topic.toLowerCase().replace(/\s+/g, '-')], difficulty }
});

if (topicQuestions.questions.length === 0) {
Expand All @@ -22,7 +34,7 @@ export default {

const questionLink = `https://leetcode.com/problems/${randomQuestion.titleSlug}/`;
const embed = new EmbedBuilder()
.setTitle(`Random ${selectedTopic.replace(/-/g, ' ')} Question: ${randomQuestion.title}`)
.setTitle(`Random ${topic} Question: ${randomQuestion.title}`)
.setURL(questionLink)
.setColor(0x0099FF)
.addFields(
Expand Down
27 changes: 22 additions & 5 deletions interactions/commands/topics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
import { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
import LeetCodeUtility from "../../utility/LeetCode.js";

const topics = [
Expand All @@ -11,21 +11,38 @@ const topics = [
export default {
data: new SlashCommandBuilder()
.setName('topics')
.setDescription('Shows a list of LeetCode topics to choose from'),
.setDescription('Shows a list of LeetCode topics to choose from')
.addStringOption(
(option) => option
.setName('difficulty')
.setDescription('Difficulty of the random question found based on topic')
.addChoices(
{ name: 'random', value: '0' },
{ name: 'easy', value: '1' },
{ name: 'medium', value: '2' },
{ name: 'hard', value: '3' }
)
.setRequired(false)
),
run: async (interaction) => {
const difficulty = interaction.options.getString('difficulty') || '0'
const chunkedTopics = LeetCodeUtility.chunkArray(topics, 5);

const rows = chunkedTopics.map(chunk =>
new ActionRowBuilder().addComponents(
chunk.map(topic =>
chunk.map((topic) =>
new ButtonBuilder()
.setCustomId(`topic_${topic.toLowerCase().replace(/\s+/g, '-')}`)
.setCustomId(`topic_${topics.indexOf(topic)}_${difficulty}`)
.setLabel(topic)
.setStyle(ButtonStyle.Secondary)
)
)
);

return interaction.reply({ content: 'Choose a topic to get a random question:', components: rows })
const embed = new EmbedBuilder()
.setDescription('**Choose a topic to get a random question**')
.setColor(0xfea116)

return interaction.reply({ embeds: [ embed ], components: rows })
}
}

0 comments on commit cbbe09b

Please sign in to comment.