Better error handling + more commands + code optimization
This commit is contained in:
parent
e4c8327117
commit
96f915838d
7 changed files with 240 additions and 280 deletions
75
bot.js
75
bot.js
|
@ -1,27 +1,80 @@
|
|||
const { Telegraf } = require('telegraf');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const Config = require('./props/config.json');
|
||||
const { isOnSpamWatch } = require('./plugins/lib-spamwatch/spamwatch.js');
|
||||
|
||||
const bot = new Telegraf(Config.botToken);
|
||||
const MAX_RETRIES = 5;
|
||||
let restartCount = 0;
|
||||
|
||||
|
||||
const loadCommands = () => {
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const commandsPath = path.join(__dirname, 'commands');
|
||||
|
||||
fs.readdirSync(commandsPath).forEach((file) => {
|
||||
const command = require(path.join(commandsPath, file));
|
||||
if (typeof command === 'function') {
|
||||
command(bot, isOnSpamWatch);
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleShutdown = (signal) => {
|
||||
console.log(`Received ${signal}. Stopping bot...`);
|
||||
bot.stop(signal).then(() => {
|
||||
console.log('Bot stopped.');
|
||||
process.exit(0);
|
||||
});
|
||||
};
|
||||
|
||||
loadCommands();
|
||||
process.once('SIGINT', () => handleShutdown('SIGINT'));
|
||||
process.once('SIGTERM', () => handleShutdown('SIGTERM'));
|
||||
|
||||
bot.launch().then(() => {
|
||||
console.log('Bot está rodando...');
|
||||
process.on('uncaughtException', (error) => {
|
||||
console.error('Uncaught Exception:', error.message);
|
||||
console.error(error.stack);
|
||||
});
|
||||
|
||||
process.once('SIGINT', () => bot.stop('SIGINT'));
|
||||
process.once('SIGTERM', () => bot.stop('SIGTERM'));
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
||||
});
|
||||
|
||||
loadCommands();
|
||||
startBot();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue