-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
135 lines (106 loc) · 3.75 KB
/
index.js
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
// Require necessary files
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Intents, EmbedBuilder, GuildManager } = require('discord.js');
const token = process.env['TOKEN'];
const channelId = process.env['CHANNEL_ID'];
// New client instance with specified intents
const client = new Client({intents: 512 | 1});
// Read commands
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}
// Notify that the bot is online
client.once('ready', () => {
console.log(`Ready! Logged in as ${client.user.tag}`);
});
// Dynamically execute commands
client.on('interactionCreate', async function(interaction){
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
console.log('-- /' + interaction.commandName + ' --')
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
//initialize challenges into an array
const baseChallengePath = path.join(__dirname, 'dailyprogrammerchallenges');
var challengeFilePaths = [];
function readDirectory(directory){
if(directory == baseChallengePath + "/LICENSE") return;
prefix = directory.substring(directory.length-3);
console.log("Prefix is: " + prefix);
if(prefix == ".md"){
challengeFilePaths.push(directory);
return;
}
const subdirectories = fs.readdirSync(directory);
for (const subdirectory of subdirectories){
const fullSubdir = directory + "/" + subdirectory;
readDirectory(fullSubdir);
}
}
readDirectory(baseChallengePath);
function getRandomCodingChallenge(){
const randomIndex = Math.floor(Math.random()*challengeFilePaths.length);
const randomFilePath = challengeFilePaths[randomIndex];
const challengeText = fs.readFileSync(randomFilePath);
return challengeText.toString();
};
function yourFunction(){
console.log("Today is at least 1 day past var x's date!");
var day;
const exampleEmbed = {
color: 0x0099ff,
title: 'Coding Challenge of the Day!',
author: {
name: 'CodingHome bot',
icon_url: 'https://cdn.discordapp.com/avatars/1107022280023871549/d0f7f613cb6bc669255d5b2474bf22a3.jpg'
},
thumbnail: {
url: 'https://i.imgur.com/zMTXVgS.png',
},
fields: [
{
name: 'Challenge:',
value: getRandomCodingChallenge(),
}
],
timestamp: new Date().toISOString(),
footer: {
text: 'New challenges appear each day!',
},
};
client.channels.cache.get(channelId).send({embeds: [exampleEmbed]}).catch(console.error);
};
var lastDay = 0;
function checkDate() {
console.log("Checking date");
// Get today's date
var today = new Date();
// Create a previous date (replace this with your own previous date)
var x = new Date(lastDay);
// Compare today's date with x
var timeDifference = today.getTime() - x.getTime();
var daysDifference = timeDifference / (1000 * 3600 * 24);
if (daysDifference >= 1) {
// Today is at least 1 day past x
yourFunction(); // Replace `yourFunction` with the function you want to run
lastDay = today.getTime();
}
}
function dailyChallenge() {
setInterval(checkDate, 5000);
}
module.exports = dailyChallenge;
// Login to Discord
client.login(token).then(() => dailyChallenge());