Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Difficulty in Topics Command #51

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })
}
}
Loading