Unit_AI/index.js
2024-06-01 16:35:11 -04:00

90 lines
3.1 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');
// 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/1210474674257928203/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);
}
}
// Call the async function to register slash commands
registerSlashCommands();
// Anti Crash
process.on('unhandledRejection', (reason, p) => {
console.log(chalk.bold.redBright('[antiCrash] :: Unhandled Rejection/Catch'));
console.log(reason?.stack, p);
});
process.on("uncaughtException", (err, origin) => {
console.log(chalk.bold.redBright('[antiCrash] :: ncaught Exception/Catch'));
console.log(err?.stack, origin);
});
process.on('uncaughtExceptionMonitor', (err, origin) => {
console.log(chalk.bold.redBright('[antiCrash] :: Uncaught Exception/Catch (MONITOR)'));
console.log(err?.stack, origin);
});
// Discord Client login
client.login(config.Token);