AI_botter/index.js

150 lines
4.7 KiB
JavaScript
Raw Normal View History

2024-03-01 11:09:57 +00:00
const Discord = require('discord.js');
const chalk = require('chalk');
const fs = require('node:fs');
const config = require('./configs/config.json');
2024-05-26 14:51:40 +01:00
const simpleGit = require('simple-git');
const path = require('node:path');
2024-05-31 21:41:02 +01:00
const { exec } = require('child_process');
2024-03-01 11:09:57 +00:00
// Discord Client Constructor
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.MessageContent,
Discord.GatewayIntentBits.DirectMessages
2024-03-01 11:09:57 +00:00
]
});
// 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}`)];
2024-05-27 01:21:21 +01:00
};
2024-03-01 11:09:57 +00:00
// 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}`)];
2024-05-27 01:21:21 +01:00
};
2024-03-01 11:09:57 +00:00
// 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(
2024-05-27 01:21:21 +01:00
'/applications/1199858077655126188/commands', // Replace {applicationId} with your bot's application ID
2024-03-01 11:09:57 +00:00
{ 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();
// Function to update the bot from GitHub repository
2024-03-01 11:09:57 +00:00
async function updateBotFromGitHub() {
2024-05-26 14:51:40 +01:00
const git = simpleGit(path.resolve(__dirname));
2024-03-01 11:09:57 +00:00
try {
console.log('Checking for updates from GitHub...');
2024-05-26 14:51:40 +01:00
if (!await git.checkIsRepo()) {
2024-05-29 21:39:59 +01:00
await git.init().addRemote('origin', 'https://git.shadowhosting.xyz/shadowvr/AI_botter.git').fetch();
2024-05-26 14:51:40 +01:00
}
2024-03-01 11:09:57 +00:00
await git.pull('origin', 'main');
2024-03-01 11:09:57 +00:00
2024-05-31 21:40:10 +01:00
console.log('Bot updated from GitHub.');
2024-05-27 01:21:21 +01:00
2024-03-01 11:09:57 +00:00
} catch (error) {
console.error('Error updating bot from GitHub:', error);
}
}
2024-05-31 21:40:10 +01:00
function startBot() {
console.log('Starting the bot...');
const botProcess = exec('node bot.js', {
cwd: path.resolve(__dirname),
});
botProcess.stdout.on('data', (data) => {
console.log(data.toString());
});
botProcess.stderr.on('data', (data) => {
console.error(data.toString());
});
botProcess.on('exit', (code) => {
console.log(`Bot process exited with code ${code}`);
});
}
async function updateAndStartBot() {
await updateBotFromGitHub();
startBot();
}
2024-03-01 11:09:57 +00:00
// Function to check for updates at regular intervals
function checkForUpdates() {
2024-05-27 01:21:21 +01:00
// Set an interval to check for updates (e.g., every 24 hours)
const interval = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
2024-05-31 21:40:10 +01:00
setInterval(updateAndStartBot, interval);
2024-03-01 11:09:57 +00:00
// Perform initial check for updates on bot startup
2024-05-31 21:40:10 +01:00
updateAndStartBot();
2024-03-01 11:09:57 +00:00
}
// Call the function to check for updates
checkForUpdates();
// 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) => {
2024-05-27 01:21:21 +01:00
console.log(chalk.bold.redBright('[antiCrash] :: ncaught Exception/Catch'));
2024-03-01 11:09:57 +00:00
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
2024-05-27 01:21:21 +01:00
client.login(config.Token);