wolfcountBot/handler/index.js

109 lines
3.3 KiB
JavaScript

require("dotenv").config();
const { glob } = require("glob");
const { promisify } = require("util");
const globPromise = promisify(glob);
const mongoose = require('mongoose');
const wolfcount = require('../models/wolfcount');
var cron = require('node-cron');
const moment = require('moment');
module.exports = async (client) => {
const uri = `mongodb+srv://${process.env.MONGODBUSER}:${process.env.MONGODBPASS}@${process.env.MONGODBCLUSTER}/${process.env.DATABASE}?retryWrites=true&w=majority`
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('');
console.log(`Connected to the database`);
}).catch((error) => {
console.log(`Failed to connect to the database`);
console.log(error);
});
// Slash Commands
const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`);
const arrayOfSlashCommands = [];
slashCommands.map((value) => {
const file = require(value);
const splitted = value.split("/");
const directory = splitted[splitted.length - 2];
if (!file?.name) return;
const properties = { directory, ...file };
client.slashCommands.set(file.name, properties);
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
arrayOfSlashCommands.push(file);
});
// Events
const eventFiles = await globPromise(`${process.cwd()}/events/*.js`);
eventFiles.map((value) => require(value));
// Find a document by validation
wolfcount.findOne({ validation: 'wolfcount' }).then((countdocument) => {
console.log('');
console.log(countdocument);
basecount = countdocument.count || 0;
console.log('');
console.log(`Starting count from ${basecount}`)
}).catch((error) => {
console.error(error);
});
// setInterval(function() {
// // Find a document by validation
// wolfcount.findOne({ validation: 'wolfcount' }).then((countdocument) => {
// console.log('');
// //console.log(countdocument);
// console.log(`Refreshed count`)
// basecount = countdocument.count
// console.log('');
// }).catch((error) => {
// console.error(error);
// });
// }, 30000);
cron.schedule('*/2 * * * *', async () => {
console.log(`${moment(Date.now()).format('DD/MM/YY H:mm:ss')} Starting count refresh...`);
// Find a document by validation
wolfcount.findOne({ validation: 'wolfcount' }).then((countdocument) => {
console.log('');
//console.log(countdocument);
console.log(`Refreshed count`)
basecount = countdocument.count
console.log('');
}).catch((error) => {
console.error(error);
});
console.log(`${moment(Date.now()).format('DD/MM/YY H:mm:ss')} Refreshed wolfcount on cron`);
});
// Slash Commands Register
client.on("ready", async () => {
// Register for a single guild
await client.guilds.cache.get("239352426451304449").commands.set(arrayOfSlashCommands);
// Register for a single guild
await client.guilds.cache.get("1075800713323610142").commands.set(arrayOfSlashCommands);
// Register for all the guilds the bot is in
// await client.application.commands.set(arrayOfSlashCommands);
});
// Error handling
client.on("error", async (error) => {
console.log('');
console.error("Something went wrong:", error);
});
};