Advertisement
Guest User

Untitled

a guest
Apr 12th, 2025
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Client, GatewayIntentBits, ChannelType } from 'discord.js';
  2. import { joinVoiceChannel, VoiceConnection } from '@discordjs/voice';
  3. import { config } from './config';
  4. import readline from 'readline';
  5.  
  6. type BotConfig = {
  7.   token: string;
  8.   client: Client;
  9. };
  10.  
  11. // Track all voice connections for cleanup
  12. const voiceConnections: VoiceConnection[] = [];
  13.  
  14. const cleanup = async () => {
  15.   console.log('๐Ÿงน Cleaning up voice connections...');
  16.   for (const connection of voiceConnections) {
  17.     try {
  18.       connection.destroy();
  19.       // Wait a bit to ensure the connection is properly closed
  20.       await new Promise((resolve) => setTimeout(resolve, 100));
  21.     } catch (error) {
  22.       console.error('โŒ Error destroying voice connection:', error);
  23.     }
  24.   }
  25. };
  26.  
  27. const setupUserInput = () => {
  28.   const rl = readline.createInterface({
  29.     input: process.stdin,
  30.     output: process.stdout,
  31.   });
  32.  
  33.   console.log('\n๐Ÿ“ Type "exit" to gracefully shutdown the bots');
  34.  
  35.   rl.on('line', async (input) => {
  36.     if (input.toLowerCase() === 'exit') {
  37.       console.log('๐Ÿ‘‹ Initiating graceful shutdown...');
  38.       await cleanup();
  39.       rl.close();
  40.       process.exit(0);
  41.     } else {
  42.       console.log('๐Ÿ“ Type "exit" to gracefully shutdown the bots');
  43.     }
  44.   });
  45. };
  46.  
  47. const createBot = ({ token, client }: BotConfig): Promise<Client> => {
  48.   return new Promise((resolve, reject) => {
  49.     client.on('ready', async (bot) => {
  50.       console.log(`โœ… Bot ${bot.user.tag} is ready!`);
  51.  
  52.       try {
  53.         const channel = await bot.channels.fetch(config.VOICE_CHANNEL_ID);
  54.  
  55.         if (!channel || channel.type !== ChannelType.GuildVoice) {
  56.           throw new Error('Channel is not a voice channel');
  57.         }
  58.  
  59.         const connection = joinVoiceChannel({
  60.           channelId: channel.id,
  61.           guildId: channel.guild.id,
  62.           adapterCreator: channel.guild.voiceAdapterCreator,
  63.         });
  64.  
  65.         // Track the connection for cleanup
  66.         voiceConnections.push(connection);
  67.  
  68.         console.log(`๐ŸŽค Bot ${bot.user.tag} joined voice channel ${channel.name}`);
  69.         resolve(bot);
  70.       } catch (error) {
  71.         console.error(`โŒ Error joining voice channel for bot ${bot.user.tag}:`, error);
  72.         reject(error);
  73.       }
  74.     });
  75.  
  76.     client.on('error', (error) => {
  77.       console.error(`โŒ Bot client error:`, error);
  78.       reject(error);
  79.     });
  80.  
  81.     client.login(token).catch(reject);
  82.   });
  83. };
  84.  
  85. let firstBot: Client | null = null;
  86. const initializeBots = async () => {
  87.   try {
  88.     for (const [index, token] of config.DISCORD_BOT_TOKENS.entries()) {
  89.       const client = new Client({
  90.         intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates],
  91.       });
  92.  
  93.       await createBot({ token, client });
  94.  
  95.       if (index === 0) {
  96.         firstBot = client;
  97.       }
  98.  
  99.       // Add delay before next bot (except for the last bot)
  100.       if (index < config.DISCORD_BOT_TOKENS.length - 1) {
  101.         console.log(`โณ Waiting 65 seconds before initializing next bot...`);
  102.         await new Promise((resolve) => setTimeout(resolve, 65000));
  103.       }
  104.     }
  105.  
  106.     console.log('โœจ All bots initialized successfully!');
  107.     if (firstBot) {
  108.       const usersInVoiceChannel = await firstBot.channels.fetch(config.VOICE_CHANNEL_ID);
  109.       if (!usersInVoiceChannel || usersInVoiceChannel.type !== ChannelType.GuildVoice) {
  110.         throw new Error('usersInVoiceChannel is not a voice channel');
  111.       }
  112.       console.log(`๐Ÿ‘ฅ Users in voice channel: ${usersInVoiceChannel.members.size}`);
  113.     }
  114.     setupUserInput();
  115.   } catch (error) {
  116.     console.error('โŒ Error initializing bots:', error);
  117.     process.exit(1);
  118.   }
  119. };
  120.  
  121. initializeBots();
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement