119 lines
3.9 KiB
JavaScript
119 lines
3.9 KiB
JavaScript
const { Client } = require("revolt.js");
|
|
const { Collection } = require('@discordjs/collection');
|
|
const logger = require('./functions/logger');
|
|
const checkPolls = require('./functions/checkPolls');
|
|
const color = require("./functions/colorCodes");
|
|
const Uploader = require("revolt-uploader");
|
|
const TranslationHandler = require('./handlers/translation');
|
|
const DatabaseHandler = require('./handlers/database');
|
|
// let config;
|
|
|
|
// if (process.env.NODE_ENV === "development") {
|
|
// config = require("./botconfig.dev.json");
|
|
// } else {
|
|
// config = require("./botconfig.json");
|
|
// }
|
|
const config = require("./botconfig.json");
|
|
|
|
// Validate required configuration
|
|
if (!config || !config.token || !config.mongoDB || !config.api) {
|
|
throw new Error('Missing required configuration. Please check your botconfig.json file.');
|
|
}
|
|
|
|
const { token, mongoDB, api } = config;
|
|
console.log(mongoDB)
|
|
|
|
class Bot {
|
|
constructor(config) {
|
|
this.config = config;
|
|
this.client = new Client({ baseURL: config.api });
|
|
this.initializeCore();
|
|
this.initializeCollections();
|
|
this.setupErrorHandling();
|
|
}
|
|
|
|
initializeCore() {
|
|
this.client.Uploader = new Uploader(this.client);
|
|
this.client.config = require("./config");
|
|
this.client.translate = new TranslationHandler();
|
|
this.client.logger = logger;
|
|
this.client.botConfig = this.config;
|
|
}
|
|
|
|
initializeCollections() {
|
|
const collections = ["aliases", "commands", "event", "functions"];
|
|
const maps = ["reactions", "paginate", "timeout", "polls", "used", "messageCollector", "messageEdit"];
|
|
|
|
collections.forEach(x => this.client[x] = new Collection());
|
|
maps.forEach(x => this.client[x] = new Map());
|
|
}
|
|
|
|
async initializeDatabase() {
|
|
try {
|
|
const db = new DatabaseHandler(this.config.mongoDB);
|
|
await db.connectToDatabase();
|
|
|
|
this.client.database = db;
|
|
this.client.models = db.models;
|
|
|
|
db.cacheSweeper(this.client);
|
|
db.guildSweeper(this.client);
|
|
|
|
logger.success('Database', 'Successfully connected to database');
|
|
} catch (error) {
|
|
logger.error('Database Error', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
setupErrorHandling() {
|
|
const errorTypes = {
|
|
"unhandledRejection": "Unhandled Rejection/Catch",
|
|
"uncaughtException": "Uncaught Exception/Catch",
|
|
"uncaughtExceptionMonitor": "Uncaught Exception/Catch (MONITOR)"
|
|
};
|
|
|
|
Object.entries(errorTypes).forEach(([event, message]) => {
|
|
process.on(event, (error, origin) => {
|
|
logger.error('Error Handling', `${message}: ${error.message}`);
|
|
console.log(color("%", `%4[Error_Handling] :: ${message}%c`));
|
|
console.log(error);
|
|
if (origin) console.log(origin);
|
|
});
|
|
});
|
|
}
|
|
|
|
async initializeHandlers() {
|
|
try {
|
|
["command", "event", "function"].forEach(x => require(`./handlers/${x}`)(this.client));
|
|
logger.success('Handlers', 'Successfully initialized all handlers');
|
|
} catch (error) {
|
|
logger.error('Handler Error', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
setupEventListeners() {
|
|
this.client.once("ready", async () => {
|
|
logger.success('Bot Ready', `${this.client.user.username} is ready`);
|
|
await checkPolls(this.client);
|
|
});
|
|
}
|
|
|
|
async start() {
|
|
try {
|
|
await this.initializeDatabase();
|
|
await this.initializeHandlers();
|
|
this.setupEventListeners();
|
|
await this.client.loginBot(this.config.token);
|
|
} catch (error) {
|
|
logger.error('Startup Error', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Start the bot
|
|
const bot = new Bot({ token, mongoDB, api });
|
|
bot.start();
|