Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Client, GatewayIntentBits, ChannelType } from 'discord.js';
- import { joinVoiceChannel, VoiceConnection } from '@discordjs/voice';
- import { config } from './config';
- import readline from 'readline';
- type BotConfig = {
- token: string;
- client: Client;
- };
- // Track all voice connections for cleanup
- const voiceConnections: VoiceConnection[] = [];
- const cleanup = async () => {
- console.log('๐งน Cleaning up voice connections...');
- for (const connection of voiceConnections) {
- try {
- connection.destroy();
- // Wait a bit to ensure the connection is properly closed
- await new Promise((resolve) => setTimeout(resolve, 100));
- } catch (error) {
- console.error('โ Error destroying voice connection:', error);
- }
- }
- };
- const setupUserInput = () => {
- const rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout,
- });
- console.log('\n๐ Type "exit" to gracefully shutdown the bots');
- rl.on('line', async (input) => {
- if (input.toLowerCase() === 'exit') {
- console.log('๐ Initiating graceful shutdown...');
- await cleanup();
- rl.close();
- process.exit(0);
- } else {
- console.log('๐ Type "exit" to gracefully shutdown the bots');
- }
- });
- };
- const createBot = ({ token, client }: BotConfig): Promise<Client> => {
- return new Promise((resolve, reject) => {
- client.on('ready', async (bot) => {
- console.log(`โ Bot ${bot.user.tag} is ready!`);
- try {
- const channel = await bot.channels.fetch(config.VOICE_CHANNEL_ID);
- if (!channel || channel.type !== ChannelType.GuildVoice) {
- throw new Error('Channel is not a voice channel');
- }
- const connection = joinVoiceChannel({
- channelId: channel.id,
- guildId: channel.guild.id,
- adapterCreator: channel.guild.voiceAdapterCreator,
- });
- // Track the connection for cleanup
- voiceConnections.push(connection);
- console.log(`๐ค Bot ${bot.user.tag} joined voice channel ${channel.name}`);
- resolve(bot);
- } catch (error) {
- console.error(`โ Error joining voice channel for bot ${bot.user.tag}:`, error);
- reject(error);
- }
- });
- client.on('error', (error) => {
- console.error(`โ Bot client error:`, error);
- reject(error);
- });
- client.login(token).catch(reject);
- });
- };
- let firstBot: Client | null = null;
- const initializeBots = async () => {
- try {
- for (const [index, token] of config.DISCORD_BOT_TOKENS.entries()) {
- const client = new Client({
- intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates],
- });
- await createBot({ token, client });
- if (index === 0) {
- firstBot = client;
- }
- // Add delay before next bot (except for the last bot)
- if (index < config.DISCORD_BOT_TOKENS.length - 1) {
- console.log(`โณ Waiting 65 seconds before initializing next bot...`);
- await new Promise((resolve) => setTimeout(resolve, 65000));
- }
- }
- console.log('โจ All bots initialized successfully!');
- if (firstBot) {
- const usersInVoiceChannel = await firstBot.channels.fetch(config.VOICE_CHANNEL_ID);
- if (!usersInVoiceChannel || usersInVoiceChannel.type !== ChannelType.GuildVoice) {
- throw new Error('usersInVoiceChannel is not a voice channel');
- }
- console.log(`๐ฅ Users in voice channel: ${usersInVoiceChannel.members.size}`);
- }
- setupUserInput();
- } catch (error) {
- console.error('โ Error initializing bots:', error);
- process.exit(1);
- }
- };
- initializeBots();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement