discord activities | check discord commands | discord authenticate instances | Search

This Node.js script manages a collection of Discord bots by launching and running multiple instances of a bot service, each with its own configuration and environment settings. The launchDiscord function allows users to specify which bot to launch by providing an index in the DISCORD_SERVICES array.

Run example

npm run import -- "start a bunch of discord services"

start a bunch of discord services

const {spawn, spawnSync} = require("child_process");
const path = require('path')

const DISCORD_SERVICES = [
  void 0,
  void 0, // TODO: Orbb bot
  'discord mid journey commands',
  'sync deceptive chat',
  'sync discord llm tools',
  'discord handy tools',
  'discord remote control',
  'discord music player',
]

const APPLICATION_IDS = [
  void 0,
  void 0,
  '1335464182111535124',
  '1335472379232780401',
  '1335479586858733568',
  '1335483072602177626',
  '1335491680630997034',
  '1335769252409380884',
]


const ENVIRONMENTS = [
  void 0,
  {
    MODEL_NAME: 'Orbb',
    DEFAULT_MODEL: 'Default',
  },
  {
    BASE_DIRECTORY: path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE, 'stable-diffusion-webui/outputs'),
    EXPRESS_PORT: 3002,
    MODEL_NAME: 'Imagineer',
    DEFAULT_MODEL: 'Meta',
  },
  {
    MODEL_NAME: 'Megatron',
    DEFAULT_MODEL: 'DeepSeek',
  },
  {
    BASE_DIRECTORY: path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE, 'jupyter_ops/Resources/Projects'),
    EXPRESS_PORT: 3004,
    MODEL_NAME: 'Optimus Prime',
    DEFAULT_MODEL: 'Qwen',
  },
  {
    MODEL_NAME: 'WALL-E',
    DEFAULT_MODEL: 'Code',
  },
  {
    MODEL_NAME: 'GLaDOS',
    DEFAULT_MODEL: 'Code',
  },
  {
    BASE_DIRECTORY: path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE, 'Music'),
    EXPRESS_PORT: 3001,
    MODEL_NAME: 'BMO',
    DEFAULT_MODEL: 'Mistral',
  },
]

function launchDiscord(botIndex = 0) {

  if(parseInt(botIndex).toString() != botIndex.toString()) {
    return
  } else {
    botIndex = parseInt(botIndex)
  }

  if(typeof botIndex != 'number' && !botIndex) {
    return
  }

  for(let i = botIndex; i < DISCORD_SERVICES.length; i++) {
    if(!DISCORD_SERVICES[i]) {
      continue
    }

    spawn('node', ['--experimental-vm-modules', '-e', 'var result = require(\'./Core\').run()', '--', DISCORD_SERVICES[i]], {
      env: Object.assign({}, process.env, {
        TOKENPATH: '/Users/briancullinan/.credentials/discord-bot' + (i ? i : '') + '.txt',
        SECRETPATH: '/Users/briancullinan/.credentials/discord' + (i ? i : '') + '.txt',
        DEFAULT_APPLICATION: APPLICATION_IDS[i]
      }, ENVIRONMENTS[i] ? ENVIRONMENTS[i] : {}),
      stdio: [0, 1, 2],
      //detached: true,
      //shell: true,
      cwd: path.dirname(__dirname)
    })

    if(botIndex) {
      break
    }
  }
}

module.exports = launchDiscord

What the code could have been:

const { spawn, spawnSync } = require("child_process");
const path = require('path');

// Define Discord services and their corresponding application IDs
const DISCORD_SERVICES = [
  null, // TODO: Define the first service
  null, // TODO: Define the second service
  'discord mid journey commands',
 'sync deceptive chat',
 'sync discord llm tools',
  'discord handy tools',
  'discord remote control',
  'discord music player',
];

const APPLICATION_IDS = [
  null, // TODO: Define the first application ID
  null, // TODO: Define the second application ID
  '1335464182111535124',
  '1335472379232780401',
  '1335479586858733568',
  '1335483072602177626',
  '1335491680630997034',
  '1335769252409380884',
];

// Define environments for each Discord service
const ENVIRONMENTS = [
  null, // TODO: Define the first environment
  {
    model: 'Orbb',
    defaultModel: 'Default',
  },
  {
    baseDirectory: path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,'stable-diffusion-webui/outputs'),
    expressPort: 3002,
    model: 'Imagineer',
    defaultModel: 'Meta',
  },
  {
    model: 'Megatron',
    defaultModel: 'DeepSeek',
  },
  {
    baseDirectory: path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE, 'jupyter_ops/Resources/Projects'),
    expressPort: 3004,
    model: 'Optimus Prime',
    defaultModel: 'Qwen',
  },
  {
    model: 'WALL-E',
    defaultModel: 'Code',
  },
  {
    model: 'GLaDOS',
    defaultModel: 'Code',
  },
  {
    baseDirectory: path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE, 'Music'),
    expressPort: 3001,
    model: 'BMO',
    defaultModel: 'Mistral',
  },
];

/**
 * Launches a Discord bot with the specified index.
 * @param {number} [botIndex=0] The index of the bot to launch.
 */
function launchDiscord(botIndex = 0) {
  // Validate the bot index
  if (typeof botIndex!== 'number' || botIndex < 0 || botIndex >= DISCORD_SERVICES.length) {
    return;
  }

  // Iterate over the Discord services starting from the specified index
  for (let i = botIndex; i < DISCORD_SERVICES.length; i++) {
    // Skip services with no ID
    if (!DISCORD_SERVICES[i]) {
      continue;
    }

    // Create a new environment for the service
    const environment = ENVIRONMENTS[i] || {};

    // Set environment variables
    const envVariables = {
     ...process.env,
      tokenPath: `/Users/briancullinan/.credentials/discord-bot${i? `-${i}` : ''}.txt`,
      secretPath: `/Users/briancullinan/.credentials/discord${i? `-${i}` : ''}.txt`,
      defaultApplication: APPLICATION_IDS[i],
     ...environment,
    };

    // Spawn a new process for the bot
    spawn('node', [
      '--experimental-vm-modules',
      '-e',
      `var result = require('./Core').run();`,
      '--',
      DISCORD_SERVICES[i],
    ], {
      env: envVariables,
      stdio: [0, 1, 2],
      cwd: path.dirname(__dirname),
    });

    // Break the loop if the specified index is reached
    if (botIndex === i) {
      break;
    }
  }
}

module.exports = launchDiscord;

Code Breakdown

This is a Node.js script that appears to be managing a collection of Discord bots. Here's a concise explanation of the code:

Requires

The script starts by importing the child_process module and the path module.

Constants

The script defines several constants:

launchDiscord function

The launchDiscord function takes an optional botIndex parameter, which defaults to 0. This function:

  1. Checks if the input botIndex is not a valid integer, in which case it returns immediately.
  2. Iterates over the DISCORD_SERVICES array, starting from the specified botIndex.
  3. For each bot service, it attempts to spawn a new Node.js process using the child_process module. The process runs a script in a file named ./Core.js using the var result = require('./Core').run() command.
  4. The env option is used to set environment variables, including TOKENPATH, which points to a Discord bot token file.

Purpose

The purpose of this script appears to be launching and managing multiple Discord bots, each with its own configuration and environment settings. The launchDiscord function allows the user to specify which bot to launch by providing an index in the DISCORD_SERVICES array.