Twan07 commited on
Commit
7930560
·
verified ·
1 Parent(s): 463d22e

Update index.ts

Browse files
Files changed (1) hide show
  1. index.ts +17 -16
index.ts CHANGED
@@ -1,17 +1,17 @@
1
- import config from './config.json' with { type: 'json' }; // Bun yêu cầu cú pháp này cho JSON
2
  import { Client, GatewayIntentBits, Partials } from 'discord.js';
3
  import { readdirSync } from 'fs';
4
  import { join } from 'path';
5
  import type { Command } from './types';
6
  import { MusicQueue } from './utils/MusicQueue';
7
- import { SocksProxyAgent } from 'socks-proxy-agent'; // Cài đặt gói này: bun add https-proxy-agent
8
 
9
- const commandsDir = join(import.meta.dir, 'Commands'); // Bun sử dụng import.meta.dir thay vì __dirname
10
  const commands: Command[] = readdirSync(commandsDir)
11
  .filter(file => file.endsWith('.ts') || file.endsWith('.js'))
12
  .map(file => {
13
  try {
14
- const commandModule = require(join(commandsDir, file)); // Bun hỗ trợ require, nhưng ESM import tốt hơn
15
  return commandModule.default;
16
  } catch (error) {
17
  console.error(`❌ Failed to load command ${file}:`, error);
@@ -21,30 +21,33 @@ const commands: Command[] = readdirSync(commandsDir)
21
  .filter((cmd): cmd is Command => cmd !== undefined);
22
 
23
  const clients: Client[] = [];
24
- const queues = new Map<string, MusicQueue>(); // Quản lý hàng đợi nhạc theo guildId
25
 
26
  config.tokens.forEach((token: string, index: number) => {
27
- const proxy = 'socks5://admin1:[email protected]:80'; // Thay bằng thông tin proxy của bạn
28
- const agent = new SocksProxyAgent(proxy);
29
-
30
  const client = new Client({
31
  intents: [
32
  GatewayIntentBits.Guilds,
33
  GatewayIntentBits.GuildMessages,
34
  GatewayIntentBits.MessageContent,
35
  GatewayIntentBits.DirectMessages,
36
- GatewayIntentBits.GuildVoiceStates, // Thêm intent cho voice
37
  ],
38
  partials: [Partials.Channel],
39
- agent: agent,
 
 
 
 
40
  });
41
 
42
  client.once('ready', () => {
43
- console.log(`✅ Client ${index + 1} - ${client.user?.username} is ready!`);
44
  clients.push(client);
45
  });
46
 
47
- // Xử lý prefix commands
48
  client.on('messageCreate', async message => {
49
  if (message.author.bot || !message.content.startsWith(config.PREFIX)) return;
50
 
@@ -68,7 +71,6 @@ config.tokens.forEach((token: string, index: number) => {
68
  }
69
  });
70
 
71
- // Auto-stop khi người dùng rời voice channel
72
  client.on('voiceStateUpdate', (oldState, newState) => {
73
  const queue = queues.get(oldState.guild.id);
74
  if (!queue || !queue.connection) return;
@@ -87,7 +89,7 @@ config.tokens.forEach((token: string, index: number) => {
87
  queue.connection.destroy();
88
  queue.connection = null;
89
  }
90
- console.log(`Auto-stopped music in guild ${oldState.guild.name} - no users in voice channel`);
91
  }
92
  }
93
  }
@@ -96,10 +98,9 @@ config.tokens.forEach((token: string, index: number) => {
96
  client.login(token).catch(error => {
97
  console.error(`❌ Failed to login Client ${index + 1}:`, error);
98
  });
99
-
100
  });
101
 
102
- export { clients, commands, queues }; // Xuất queues để sử dụng trong các lệnh
103
 
104
  process.on('unhandledRejection', error => {
105
  console.error('❗ Unhandled promise rejection:', error);
 
1
+ import config from './config.json' with { type: 'json' };
2
  import { Client, GatewayIntentBits, Partials } from 'discord.js';
3
  import { readdirSync } from 'fs';
4
  import { join } from 'path';
5
  import type { Command } from './types';
6
  import { MusicQueue } from './utils/MusicQueue';
7
+ import { SocksProxyAgent } from 'socks-proxy-agent'; // bun add socks-proxy-agent
8
 
9
+ const commandsDir = join(import.meta.dir, 'Commands');
10
  const commands: Command[] = readdirSync(commandsDir)
11
  .filter(file => file.endsWith('.ts') || file.endsWith('.js'))
12
  .map(file => {
13
  try {
14
+ const commandModule = require(join(commandsDir, file));
15
  return commandModule.default;
16
  } catch (error) {
17
  console.error(`❌ Failed to load command ${file}:`, error);
 
21
  .filter((cmd): cmd is Command => cmd !== undefined);
22
 
23
  const clients: Client[] = [];
24
+ const queues = new Map<string, MusicQueue>();
25
 
26
  config.tokens.forEach((token: string, index: number) => {
27
+ const proxyUrl = 'socks5h://admin1:[email protected]:80'; // 'socks5h' để resolve DNS qua proxy
28
+ const agent = new SocksProxyAgent(proxyUrl);
29
+
30
  const client = new Client({
31
  intents: [
32
  GatewayIntentBits.Guilds,
33
  GatewayIntentBits.GuildMessages,
34
  GatewayIntentBits.MessageContent,
35
  GatewayIntentBits.DirectMessages,
36
+ GatewayIntentBits.GuildVoiceStates,
37
  ],
38
  partials: [Partials.Channel],
39
+ http: {
40
+ agent: {
41
+ https: agent, // Áp dụng agent cho kết nối HTTPS
42
+ },
43
+ },
44
  });
45
 
46
  client.once('ready', () => {
47
+ console.log(`✅ Client ${index + 1} - ${client.user?.tag} is ready!`);
48
  clients.push(client);
49
  });
50
 
 
51
  client.on('messageCreate', async message => {
52
  if (message.author.bot || !message.content.startsWith(config.PREFIX)) return;
53
 
 
71
  }
72
  });
73
 
 
74
  client.on('voiceStateUpdate', (oldState, newState) => {
75
  const queue = queues.get(oldState.guild.id);
76
  if (!queue || !queue.connection) return;
 
89
  queue.connection.destroy();
90
  queue.connection = null;
91
  }
92
+ console.log(`⏹️ Auto-stopped music in guild ${oldState.guild.name} - no users in voice channel`);
93
  }
94
  }
95
  }
 
98
  client.login(token).catch(error => {
99
  console.error(`❌ Failed to login Client ${index + 1}:`, error);
100
  });
 
101
  });
102
 
103
+ export { clients, commands, queues };
104
 
105
  process.on('unhandledRejection', error => {
106
  console.error('❗ Unhandled promise rejection:', error);