-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
270 lines (235 loc) · 8.42 KB
/
main.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { Client, GatewayIntentBits, Partials, InteractionType, EmbedBuilder } from 'discord.js';
import { config } from 'dotenv';
import mongoose from 'mongoose';
import ProxyManager from './src/utils/proxy_manager.js';
import Logger from './src/utils/logger.js';
import { initializeDatabase } from './src/database.js';
// Create Discord client with necessary intents
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
partials: [
Partials.Channel,
Partials.Message,
Partials.User,
Partials.GuildMember,
]
});
// Handle interactions (slash commands)
client.on('interactionCreate', async interaction => {
try {
if (!interaction.isCommand()) return;
Logger.info(`Received command: ${interaction.commandName}`);
if (interaction.commandName === 'ping') {
const latency = client.ws.ping;
const embed = new EmbedBuilder()
.setColor('#00ff00')
.setTitle('🏓 Pong!')
.addFields(
{ name: 'Bot Latency', value: `${latency}ms`, inline: true },
{ name: 'Status', value: '✅ Online', inline: true }
)
.setTimestamp();
await interaction.reply({ embeds: [embed] });
Logger.info(`Ping command responded with ${latency}ms latency`);
}
if (interaction.commandName === 'test') {
await interaction.deferReply();
Logger.info('Running system check...');
const checks = await runSystemCheck();
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('System Status Check')
.addFields(
{
name: 'Proxy System',
value: checks.proxy ? '✅ Working' : '❌ Failed',
inline: true
},
{
name: 'Discord Connection',
value: checks.discord ? '✅ Connected' : '❌ Disconnected',
inline: true
},
{
name: 'Database',
value: checks.database ? '✅ Connected' : '❌ Disconnected',
inline: true
},
{
name: 'Bot Latency',
value: `${client.ws.ping}ms`,
inline: true
},
{
name: 'Uptime',
value: `${Math.floor(client.uptime / 60000)} minutes`,
inline: true
}
)
.setFooter({ text: 'Vinted Bot Status' })
.setTimestamp();
await interaction.editReply({ embeds: [embed] });
Logger.info('System check completed and results sent');
}
} catch (error) {
Logger.error('Error handling interaction:', error);
if (!interaction.replied && !interaction.deferred) {
await interaction.reply({
content: 'There was an error processing your command!',
ephemeral: true
});
}
}
});
// Regular message handling
client.on('messageCreate', async (message) => {
Logger.info(`Received message: ${message.content}`);
if (message.author.bot) return;
if (message.content === '!ping') {
try {
await message.reply('Pong! 🏓');
Logger.info('Successfully sent ping response');
} catch (error) {
Logger.error('Error sending ping response:', error);
}
}
if (message.content === '!test') {
try {
await message.reply('Bot is working! ✅');
Logger.info('Successfully sent test response');
} catch (error) {
Logger.error('Error sending test response:', error);
}
}
});
// Discord ready event
client.once('ready', async () => {
try {
Logger.info(`Logged in as ${client.user.tag}!`);
Logger.info(`Bot is in ${client.guilds.cache.size} servers`);
// Register slash commands
const commands = [
{
name: 'ping',
description: 'Check bot latency and status'
},
{
name: 'test',
description: 'Run a complete system check'
}
];
Logger.info('Starting command registration...');
Logger.info(`Commands to register: ${commands.map(cmd => cmd.name).join(', ')}`);
try {
const registeredCommands = await client.application.commands.set(commands);
Logger.info(`Successfully registered ${registeredCommands.size} commands:`);
registeredCommands.forEach(cmd => {
Logger.info(`- /${cmd.name}: ${cmd.description}`);
});
} catch (registerError) {
Logger.error('Failed to register commands:', registerError);
throw registerError;
}
} catch (error) {
Logger.error('Error in ready event:', error);
Logger.error('Stack trace:', error.stack);
}
});
async function initialize() {
try {
// Initialize database first
Logger.info('Initializing database connection...');
const dbConnected = await initializeDatabase();
if (!dbConnected) {
throw new Error('Failed to connect to database');
}
Logger.info('Database initialized successfully');
// Initialize proxy system
await ProxyManager.loadProxies();
Logger.info(`Proxy system initialized successfully`);
// Test proxy
const proxyTest = await testProxy();
if (proxyTest) {
Logger.info('Proxy test passed successfully!');
} else {
Logger.error('Proxy test failed!');
}
// Login to Discord
await client.login(process.env.DISCORD_TOKEN);
Logger.info('Discord bot is now online!');
} catch (error) {
Logger.error('Initialization error:', error);
process.exit(1);
}
}
async function testProxy() {
try {
const proxy = ProxyManager.getNextProxy();
Logger.info(`Testing proxy: ${proxy.host}:${proxy.port}`);
const response = await fetch('https://api.ipify.org?format=json', {
proxy: `http://${proxy.username}:${proxy.password}@${proxy.host}:${proxy.port}`
});
const data = await response.json();
Logger.info(`Proxy test successful! IP: ${data.ip}`);
return true;
} catch (error) {
Logger.error('Proxy test failed:', error);
return false;
}
}
async function runSystemCheck() {
Logger.info('Starting system check...');
const checks = {
proxy: false,
discord: false,
database: false
};
// Test proxy
try {
Logger.info('Testing proxy system...');
const proxy = ProxyManager.getNextProxy();
if (proxy) {
checks.proxy = true;
Logger.info('Proxy check passed');
}
} catch (error) {
Logger.error('Proxy check failed:', error);
}
// Test Discord
try {
Logger.info('Testing Discord connection...');
checks.discord = client.ws.ping !== undefined;
Logger.info(`Discord check ${checks.discord ? 'passed' : 'failed'}`);
} catch (error) {
Logger.error('Discord check failed:', error);
}
// Test Database (if you're using MongoDB)
try {
Logger.info('Testing database connection...');
if (mongoose.connection) {
checks.database = mongoose.connection.readyState === 1;
Logger.info(`Database check ${checks.database ? 'passed' : 'failed'}`);
}
} catch (error) {
Logger.error('Database check failed:', error);
}
Logger.info('System check completed');
return checks;
}
// Error handling
client.on('error', (error) => {
Logger.error('Discord client error:', error);
});
// Debug logging
client.on('debug', (info) => {
Logger.debug('Discord debug:', info);
});
// Start the bot
initialize();