Skip to content

Commit 6f361a3

Browse files
authored
Merge pull request #3 from MastiderMast/cronjobs
Added Cronjobs
2 parents 74d2dab + 779561e commit 6f361a3

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

package-lock.json

Lines changed: 36 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "discord.js-advanced-command-handler",
3-
"version": "2.2.0",
3+
"version": "3.1.0",
44
"description": "🤖 Discord.js V14 Command and Event Handler",
55
"main": "src/index.js",
66
"type": "module",
@@ -22,6 +22,7 @@
2222
},
2323
"dependencies": {
2424
"chalk": "^5.1.2",
25-
"discord.js": "^14.9.0"
25+
"discord.js": "^14.9.0",
26+
"node-cron": "^3.0.2"
2627
}
2728
}

src/cron/activity.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default {
2+
cron: '*/15 * * * * *',
3+
async execute(client) {
4+
let activityList = [
5+
`with ${client.guilds.cache.size} servers`,
6+
`with ${client.users.cache.size} users`,
7+
`with ${client.channels.cache.size} channels`,
8+
`with ${client.emojis.cache.size} emojis`,
9+
];
10+
11+
let activity = activityList[Math.floor(Math.random() * activityList.length)];
12+
client.user.setActivity(activity, {
13+
name: activity,
14+
type: 1
15+
});
16+
}
17+
};

src/handlers/cronjobs.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fs from 'fs';
2+
import cron from 'node-cron';
3+
import chalk from 'chalk';
4+
5+
export async function loadCronJobs(folderPath, client) {
6+
let files = fs.readdirSync(folderPath);
7+
8+
for (let i = 0; i < files.length; i++) {
9+
folderPath = folderPath.replace('src/', '');
10+
let cronjob = await import (`../${folderPath}/${files[i]}`);
11+
cron.schedule(cronjob.default.cron, () => cronjob.default.execute(client));
12+
console.log(chalk.greenBright(`[CRONJOB] Loaded ${(chalk.yellow(files[i]))}`));
13+
}
14+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import config from './data/config.js';
44
import loadEvents from './handlers/events.js';
55
import loadInteractions from './handlers/handler.js';
66
import registerApplicationCommands from './handlers/application.js';
7+
import { loadCronJobs } from './handlers/cronjobs.js';
78

89
const client = new Discord.Client({
910
intents: [
@@ -28,6 +29,7 @@ client.interaction = new Discord.Collection();
2829
await loadEvents(client);
2930
await loadInteractions('./src/interactions', client);
3031
await registerApplicationCommands(client);
32+
await loadCronJobs('./src/cron', client);
3133

3234
process.on('uncaughtException', function (err) {
3335
console.error(err);

0 commit comments

Comments
 (0)