|
| 1 | +import { SlashCommandBuilder, EmbedBuilder } from 'discord.js'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | + |
| 6 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 7 | +const configPath = path.join(__dirname, '../../config.json'); |
| 8 | + |
| 9 | +export const data = new SlashCommandBuilder() |
| 10 | + .setName('todayleetcode') |
| 11 | + .setDescription('Get a reminder of today\'s LeetCode challenge'); |
| 12 | + |
| 13 | +export async function execute(interaction) { |
| 14 | + try { |
| 15 | + const config = JSON.parse(fs.readFileSync(configPath, 'utf8')); |
| 16 | + const problem = config.dailyProblem; |
| 17 | + const problemDate = config.dailyProblemDate; |
| 18 | + |
| 19 | + if (!problem) { |
| 20 | + await interaction.reply({ |
| 21 | + content: '❌ No LeetCode problem has been set for today yet. Try again later!', |
| 22 | + ephemeral: true |
| 23 | + }); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // Check if the saved problem is from today |
| 28 | + const today = new Date().toISOString().split('T')[0]; |
| 29 | + if (problemDate !== today) { |
| 30 | + await interaction.reply({ |
| 31 | + content: '⏰ The daily problem will be sent at 12:00 PM. Check back then for a fresh challenge!', |
| 32 | + ephemeral: true |
| 33 | + }); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const embed = new EmbedBuilder() |
| 38 | + .setColor('#FFA500') |
| 39 | + .setTitle('🎯 Today\'s LeetCode Challenge') |
| 40 | + .addFields( |
| 41 | + { name: 'Problem', value: `**${problem.title}**`, inline: false }, |
| 42 | + { name: 'Difficulty', value: problem.difficulty, inline: true }, |
| 43 | + { name: 'Problem ID', value: `#${problem.id}`, inline: true }, |
| 44 | + { name: 'Link', value: `[Solve on LeetCode](${problem.link})`, inline: false } |
| 45 | + ) |
| 46 | + .setFooter({ text: 'Good luck! 🍀' }) |
| 47 | + .setTimestamp(); |
| 48 | + |
| 49 | + await interaction.reply({ embeds: [embed] }); |
| 50 | + } catch (error) { |
| 51 | + console.error('Error retrieving today\'s LeetCode challenge:', error); |
| 52 | + await interaction.reply({ |
| 53 | + content: '❌ Failed to retrieve today\'s problem. Please try again.', |
| 54 | + ephemeral: true |
| 55 | + }); |
| 56 | + } |
| 57 | +} |
0 commit comments