-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
220 lines (184 loc) · 6.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const axios = require('axios');
require('dotenv').config();
const TelegramBot = require('node-telegram-bot-api');
const Promise = require('bluebird');
const fs = require('fs');
const path = require('path');
const express = require('express');
const CommandHandler = require('./handlers/commandHandler');
const ModerationTools = require('./handlers/moderationTools');
const EventReminder = require('./handlers/eventReminder');
const AutomatedResponses = require('./handlers/automatedResponses');
const GroupManager = require('./handlers/groupManager');
const AutoReactHandler = require('./handlers/autoReactHandler');
const Database = require('./utils/database');
const config = require('./config');
const OwnerHandler = require('./handlers/ownerHandler');
const PORT = process.env.PORT || 3000;
const URL = process.env.URL || `url_here`;
const UPTIME_URL = process.env.UPTIME_URL;
const botBanner = `
░█─── ░█─░█ ░█▀▄▀█ ▀█▀ ░█▄─░█ ─█▀▀█
░█─── ░█─░█ ░█░█░█ ░█─ ░█░█░█ ░█▄▄█
░█▄▄█ ─▀▄▄▀ ░█──░█ ▄█▄ ░█──▀█ ░█─░█
Bot Name: Lumina
Description: Intelligent Telegram Bot
Author: JohnDev19
Version: 1.1.0
`;
Promise.config({
cancellation: true
});
class LuminaBot {
constructor() {
this.botInfo = {
name: 'Lumina',
author: 'JohnDev19',
version: '1.1.0',
description: 'An intelligent and user-friendly Telegram bot'
};
this.initialize();
}
async initialize() {
try {
if (!config.BOT_TOKEN) {
throw new Error('Telegram Bot Token not provided. Please check your .env file.');
}
const app = express();
app.use(express.json());
this.bot = new TelegramBot(config.BOT_TOKEN, {
webHook: {
port: PORT,
host: '0.0.0.0'
}
});
await this.bot.setWebHook(`${URL}/bot${config.BOT_TOKEN}`);
app.post(`/bot${config.BOT_TOKEN}`, (req, res) => {
this.bot.processUpdate(req.body);
res.sendStatus(200);
});
app.get('/health', (req, res) => {
res.status(200).json({
status: 'healthy',
timestamp: new Date().toISOString()
});
});
app.get('/keep-alive', (req, res) => {
res.status(200).json({ status: 'Bot is alive', timestamp: new Date().toISOString() });
});
const botInfo = await this.bot.getMe();
this.bot.botInfo = botInfo;
console.log(`Bot initialized: @${botInfo.username}`);
console.log(`Webhook server running on port ${PORT}`);
await this.initializeComponents();
this.startAutoLeaveCheck();
if (UPTIME_URL) {
setInterval(() => this.pingUptimeUrl(), 5 * 60 * 1000); // Ping every 5 minutes
console.log('Uptime pinger initialized');
}
console.log('All components initialized successfully');
} catch (error) {
console.error('Initialization Error:', error);
process.exit(1);
}
}
async initializeComponents() {
this.db = new Database();
this.commandHandler = new CommandHandler(this.bot, this.db);
this.moderationTools = new ModerationTools(this.bot, this.db);
this.eventReminder = new EventReminder(this.bot, this.db);
this.automatedResponses = new AutomatedResponses(this.bot, this.db);
this.groupManager = new GroupManager(this.bot, this.db);
this.autoReactHandler = new AutoReactHandler(this.bot, this.db);
this.ownerHandler = new OwnerHandler(this.bot, this.db);
this.setupEventListeners();
this.loadCommands();
this.setupErrorHandling();
this.eventReminder.startEventChecking();
}
setupEventListeners() {
this.bot.on('message', async (msg) => {
try {
const text = msg.text || msg.caption;
if (text) {
await this.autoReactHandler.handleMessageReaction(msg);
if (text.startsWith('/setreminder')) {
const reminderModule = require('./commands/setreminder');
const args = text.split(' ').slice(1);
await reminderModule.execute(
this.bot,
msg,
args,
this.db
);
} else if (text.startsWith('/')) {
await this.commandHandler.handleCommand(msg);
} else {
await this.automatedResponses.handleMessage(msg);
}
}
} catch (error) {
console.error('Error handling message:', error);
}
});
this.bot.on('new_chat_members', async (msg) => {
try {
await this.groupManager.handleNewMember(msg);
} catch (error) {
console.error('Error handling new member:', error);
}
});
this.bot.on('left_chat_member', async (msg) => {
try {
await this.groupManager.handleLeftMember(msg);
} catch (error) {
console.error('Error handling left member:', error);
}
});
this.bot.on('my_chat_member', async (chatMemberUpdated) => {
if (chatMemberUpdated.new_chat_member.status === 'member' &&
chatMemberUpdated.old_chat_member.status === 'left') {
await this.groupManager.joinGroup(chatMemberUpdated.chat.id);
} else if (chatMemberUpdated.new_chat_member.status === 'left' &&
chatMemberUpdated.old_chat_member.status === 'member') {
await this.groupManager.leaveGroup(chatMemberUpdated.chat.id);
}
});
}
loadCommands() {
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(path.join(commandsPath, file));
if (command.name !== 'setreminder') {
this.commandHandler.addCommand(command.name, command.execute, command.owner === true);
}
}
}
setupErrorHandling() {
this.bot.on('error', (error) => {
console.error('Bot Error:', error);
});
}
async pingUptimeUrl() {
if (UPTIME_URL) {
try {
await axios.get(UPTIME_URL);
} catch (error) {
}
}
}
startAutoLeaveCheck() {
setInterval(() => this.groupManager.checkAutoLeave(), 60 * 60 * 1000); // Check every hour
}
}
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
});
process.on('unhandledRejection', (error) => {
console.error('Unhandled Rejection:', error);
});
console.log(botBanner);
const luminaBot = new LuminaBot();
console.log('Lumina Bot is running...');
module.exports = LuminaBot;