wolfcountBot/handler/index.js

144 lines
4.2 KiB
JavaScript
Raw Normal View History

2023-04-28 07:48:43 -04:00
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');
2023-05-02 09:29:51 -04:00
const express = require("express");
const app = express();
const port = 40005
const cwd = process.cwd()
2023-04-28 07:48:43 -04:00
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);
});
2023-05-02 09:29:51 -04:00
// 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);
2023-04-28 07:48:43 -04:00
2023-05-02 09:29:51 -04:00
// Slash Commands Register
client.on("ready", async () => {
cron.schedule('*/2 * * * *', async () => {
console.log(`${moment(Date.now()).format('DD/MM/YY H:mm:ss')} Starting count refresh...`);
2023-04-28 07:48:43 -04:00
// Find a document by validation
wolfcount.findOne({ validation: 'wolfcount' }).then((countdocument) => {
console.log('');
//console.log(countdocument);
console.log(`Refreshed count`)
2023-05-02 09:29:51 -04:00
let basecount = countdocument.count;
2023-04-28 07:48:43 -04:00
console.log('');
2023-05-02 09:29:51 -04:00
return basecount;
}).catch((error) => {
console.error(error);
});
console.log(`${moment(Date.now()).format('DD/MM/YY H:mm:ss')} Refreshed wolfcount on cron`);
2023-04-28 07:48:43 -04:00
});
// 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);
});
2023-05-02 09:29:51 -04:00
app.set('view engine', 'ejs');
app.use(express.static(`${cwd}/views/images`));
// The root of the webapp
app.get("/", (req, res) => {
res.render('index', {basecount});
});
app.get("/info", (req, res) => {
res.render('info');
});
app.get("/privacy", (req, res) => {
res.render('privacy');
});
app.get("/tos", (req, res) => {
res.render('tos');
});
// redirect /invite to the discord invite
app.get('/invite', async (req, res) => {
res.redirect(`https://discord.com/oauth2/authorize?client_id=${client.user.id}&permissions=327744&scope=bot%20applications.commands`);
});
app.listen(port, () => {
console.log(`Webapp listening on port ${port}`)
});
2023-04-28 07:48:43 -04:00
};