-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (117 loc) · 4.12 KB
/
index.js
File metadata and controls
140 lines (117 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const fs = require('node:fs');
const path = require('node:path');
const { Client, Events, GatewayIntentBits, Collection, REST, Routes, EmbedBuilder } = require('discord.js');
const { token, clientId, guildId } = require('./config.json');
const axios = require('axios');
const schedule = require('node-schedule');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once('ready', async () => {
console.log('Bot is ready!');
schedule.scheduleJob('0 0 * * *', postDailyChallenge);
postDailyChallenge();
console.log(new Date());
});
client.commands = new Collection();
// Grab all the command folders from the commands directory you created earlier
function loadCommands() {
const commandFiles = fs.readdirSync(__dirname).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(__dirname, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.warn(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
// Handle command interactions
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
const errorMessage = { content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral };
if (interaction.replied || interaction.deferred) {
await interaction.followUp(errorMessage);
} else {
await interaction.reply(errorMessage);
}
}
});
// Refresh application commands in the guild
async function refreshCommands() {
try {
console.log(`Started refreshing ${client.commands.size} application (/) commands.`);
const data = await new REST().setToken(token).put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: client.commands.map(command => command.data) },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error('Error refreshing commands:', error);
}
}
async function getDailyLeetCodeChallenge() {
try {
const response = await axios.post('https://leetcode.com/graphql', {
query: `
query questionOfToday {
activeDailyCodingChallengeQuestion {
date
link
question {
title
difficulty
topicTags {
name
}
}
}
}
`
});
return response.data.data.activeDailyCodingChallengeQuestion;
} catch (error) {
console.error('Error fetching LeetCode challenge:', error);
return null;
}
}
async function postDailyChallenge() {
const challenge = await getDailyLeetCodeChallenge();
const { title, difficulty, topicTags } = challenge.question;
const link = `https://leetcode.com${challenge.link}`;
const color = {
Easy: 0x008000,
Medium: 0xFFD700,
Hard: 0xFF0000
}[difficulty] || '❓';
const tags = topicTags.map(tag => tag.name).join(', ');
const Embed = new EmbedBuilder()
.setColor(color)
.setTitle(`🌟 ${title} 🌟`)
.setDescription(difficulty)
.setFields(
{name: '🔗 URL', value: link},
{name: '🏷️ Topics', value: tags}
)
try {
const channel = await client.channels.fetch("1352311704050204793");
await channel.send(`<@&1357169483059429447>`);
await channel.send({ embeds: [Embed] });
console.log("Daily Updated.");
} catch (error) {
console.error("FAILED TO SEND MESSAGE:", error);
}
}
(async () => {
await client.login(token); // Log in the bot
loadCommands(); // Load commands dynamically
await refreshCommands(); // Refresh commands
})();