AI_botter/index.js
2024-06-01 16:50:50 -04:00

119 lines
4 KiB
JavaScript

const Discord = require('discord.js');
const chalk = require('chalk');
const fs = require('node:fs');
const config = require('./configs/config.json');
const path = require('node:path');
const winston = require('winston');
// Discord Client Constructor
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.MessageContent,
Discord.GatewayIntentBits.DirectMessages
]
});
// Event Handler
console.log(chalk.bold.yellowBright('Loading Events'));
const events = fs.readdirSync(`./events/`).filter(file => file.endsWith('.js'));
for (const file of events) {
const event = require(`./events/${file}`);
client.on(file.split('.')[0], event.bind(null, client));
delete require.cache[require.resolve(`./events/${file}`)];
};
// Message Command Handler
console.log(chalk.bold.yellowBright('Loading Message Commands'));
client.MessageCommands = new Discord.Collection();
const messageCommands = fs.readdirSync(`./commands/messages/`).filter(files => files.endsWith('.js'));
for (const file of messageCommands) {
const command = require(`./commands/messages/${file}`);
client.MessageCommands.set(command.name.toLowerCase(), command);
delete require.cache[require.resolve(`./commands/messages/${file}`)];
};
// Slash Command Handler
console.log(chalk.bold.yellowBright('Loading Slash Commands'));
client.SlashCommands = new Discord.Collection();
const slashCommands = fs.readdirSync(`./commands/interactions/`).filter(files => files.endsWith('.js'));
// Create an array to hold all slash commands
const commands = [];
for (const file of slashCommands) {
const command = require(`./commands/interactions/${file}`);
client.SlashCommands.set(command.data.name, command);
commands.push(command.data.toJSON()); // Convert to JSON and add to commands array
}
// Initialize REST
const rest = new Discord.REST({ version: '9' }).setToken(config.Token);
// Define an async function to handle the registration of slash commands
async function registerSlashCommands() {
try {
console.log('Started registering slash commands.');
await rest.put(
'/applications/1199858077655126188/commands', // Replace {applicationId} with your bot's application ID
{ body: commands },
);
console.log('Successfully registered slash commands.');
} catch (error) {
console.error('Error registering slash commands:', error);
}
}
// Create a Winston logger
const logger = winston.createLogger({
level: 'error',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.File({ filename: 'error.log' }),
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
)
})
],
});
// Call the async function to register slash commands
registerSlashCommands();
// Anti Crash
process.on('unhandledRejection', (reason, p) => {
logger.error('[antiCrash] :: Unhandled Rejection/Catch', { reason, p });
console.log(chalk.bold.redBright('[antiCrash] :: Unhandled Rejection/Catch'));
console.log(reason?.stack, p);
});
process.on("uncaughtException", (err, origin) => {
logger.error('[antiCrash] :: Uncaught Exception/Catch', { err, origin });
console.log(chalk.bold.redBright('[antiCrash] :: Uncaught Exception/Catch'));
console.log(err?.stack, origin);
});
process.on('uncaughtExceptionMonitor', (err, origin) => {
logger.error('[antiCrash] :: Uncaught Exception/Catch (MONITOR)', { err, origin });
console.log(chalk.bold.redBright('[antiCrash] :: Uncaught Exception/Catch (MONITOR)'));
console.log(err?.stack, origin);
});
// Optional: Add more specific error handling
process.on('warning', (warning) => {
logger.warn('[antiCrash] :: Warning detected', { warning });
console.log(chalk.bold.yellowBright('[antiCrash] :: Warning detected'));
console.log(warning?.stack);
});
// Discord Client login
client.login(config.Token);