2024-07-24 22:39:27 -03:00
|
|
|
const { Telegraf } = require('telegraf');
|
2024-08-30 21:12:38 -03:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2024-07-24 23:16:39 -03:00
|
|
|
const Config = require('./props/config.json');
|
2024-07-28 13:31:04 -03:00
|
|
|
const { isOnSpamWatch } = require('./plugins/lib-spamwatch/spamwatch.js');
|
2024-07-24 22:39:27 -03:00
|
|
|
|
2024-07-24 23:16:39 -03:00
|
|
|
const bot = new Telegraf(Config.botToken);
|
2024-08-30 21:12:38 -03:00
|
|
|
const MAX_RETRIES = 5;
|
|
|
|
let restartCount = 0;
|
|
|
|
|
2024-07-24 22:39:27 -03:00
|
|
|
|
|
|
|
const loadCommands = () => {
|
|
|
|
const commandsPath = path.join(__dirname, 'commands');
|
|
|
|
|
2024-08-30 21:12:38 -03:00
|
|
|
try {
|
|
|
|
fs.readdirSync(commandsPath).forEach((file) => {
|
|
|
|
try {
|
|
|
|
const command = require(path.join(commandsPath, file));
|
|
|
|
if (typeof command === 'function') {
|
|
|
|
command(bot, isOnSpamWatch);
|
|
|
|
}
|
|
|
|
} catch (fileError) {
|
|
|
|
console.error(`Failed to load command file ${file}: ${fileError.message}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (dirError) {
|
|
|
|
console.error(`Failed to read commands directory: ${dirError.message}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const sendMessage = async (ctx, text, options = {}) => {
|
|
|
|
try {
|
|
|
|
await ctx.reply(text, options);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error sending message:', error.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Função para iniciar o bot
|
|
|
|
const startBot = async () => {
|
|
|
|
try {
|
|
|
|
await bot.launch();
|
|
|
|
console.log('Bot is running...');
|
|
|
|
restartCount = 0;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to start bot:', error.message);
|
|
|
|
if (restartCount < MAX_RETRIES) {
|
|
|
|
restartCount++;
|
|
|
|
console.log(`Retrying to start bot... Attempt ${restartCount}`);
|
|
|
|
setTimeout(startBot, 5000);
|
|
|
|
} else {
|
|
|
|
console.error('Maximum retry attempts reached. Exiting.');
|
|
|
|
process.exit(1);
|
2024-07-28 13:31:04 -03:00
|
|
|
}
|
2024-08-30 21:12:38 -03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleShutdown = (signal) => {
|
|
|
|
console.log(`Received ${signal}. Stopping bot...`);
|
|
|
|
bot.stop(signal).then(() => {
|
|
|
|
console.log('Bot stopped.');
|
|
|
|
process.exit(0);
|
2024-07-24 22:39:27 -03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-08-30 21:12:38 -03:00
|
|
|
process.once('SIGINT', () => handleShutdown('SIGINT'));
|
|
|
|
process.once('SIGTERM', () => handleShutdown('SIGTERM'));
|
2024-07-24 22:39:27 -03:00
|
|
|
|
2024-08-30 21:12:38 -03:00
|
|
|
process.on('uncaughtException', (error) => {
|
|
|
|
console.error('Uncaught Exception:', error.message);
|
|
|
|
console.error(error.stack);
|
2024-07-24 22:39:27 -03:00
|
|
|
});
|
|
|
|
|
2024-08-30 21:12:38 -03:00
|
|
|
process.on('unhandledRejection', (reason, promise) => {
|
|
|
|
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
|
|
});
|
|
|
|
|
|
|
|
loadCommands();
|
|
|
|
startBot();
|