Skip to content

Commit

Permalink
Merge pull request #3 from MastiderMast/cronjobs
Browse files Browse the repository at this point in the history
Added Cronjobs
  • Loading branch information
MastiDev authored Jun 14, 2023
2 parents 74d2dab + 779561e commit 6f361a3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
39 changes: 36 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discord.js-advanced-command-handler",
"version": "2.2.0",
"version": "3.1.0",
"description": "🤖 Discord.js V14 Command and Event Handler",
"main": "src/index.js",
"type": "module",
Expand All @@ -22,6 +22,7 @@
},
"dependencies": {
"chalk": "^5.1.2",
"discord.js": "^14.9.0"
"discord.js": "^14.9.0",
"node-cron": "^3.0.2"
}
}
17 changes: 17 additions & 0 deletions src/cron/activity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default {
cron: '*/15 * * * * *',
async execute(client) {
let activityList = [
`with ${client.guilds.cache.size} servers`,
`with ${client.users.cache.size} users`,
`with ${client.channels.cache.size} channels`,
`with ${client.emojis.cache.size} emojis`,
];

let activity = activityList[Math.floor(Math.random() * activityList.length)];
client.user.setActivity(activity, {
name: activity,
type: 1
});
}
};
14 changes: 14 additions & 0 deletions src/handlers/cronjobs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fs from 'fs';
import cron from 'node-cron';
import chalk from 'chalk';

export async function loadCronJobs(folderPath, client) {
let files = fs.readdirSync(folderPath);

for (let i = 0; i < files.length; i++) {
folderPath = folderPath.replace('src/', '');
let cronjob = await import (`../${folderPath}/${files[i]}`);
cron.schedule(cronjob.default.cron, () => cronjob.default.execute(client));
console.log(chalk.greenBright(`[CRONJOB] Loaded ${(chalk.yellow(files[i]))}`));
}
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import config from './data/config.js';
import loadEvents from './handlers/events.js';
import loadInteractions from './handlers/handler.js';
import registerApplicationCommands from './handlers/application.js';
import { loadCronJobs } from './handlers/cronjobs.js';

const client = new Discord.Client({
intents: [
Expand All @@ -28,6 +29,7 @@ client.interaction = new Discord.Collection();
await loadEvents(client);
await loadInteractions('./src/interactions', client);
await registerApplicationCommands(client);
await loadCronJobs('./src/cron', client);

process.on('uncaughtException', function (err) {
console.error(err);
Expand Down

0 comments on commit 6f361a3

Please sign in to comment.