discord imagine | discord stability proxy | discord ollama vision | Search

The code defines functions related to Discord bot commands, including imagineCommands which handles image generation, and other functions for various interactions. The code uses async/await syntax and imports modules from a library called importer, but the purpose and specifics of some functions and variables are unclear.

Run example

npm run import -- "discord mid journey commands"

discord mid journey commands

const {
  registerCommand, getCommands, deleteCommand, updateCommand
} = importer.import("discord api")
const {interactionsCommands} = importer.import("discord gateway")
const {
  doHallucinate, doImagineProxy, doWhisk
} = importer.import("discord stability proxy")
const doVision = importer.import("discord ollama vision")
const {discordExpress, closeExpress} = importer.import("discord music player server")
const {doMention, doPrivate} = importer.import("discord llm interactions")

const ALL_COMMANDS = [
  'imagine',
  'hallucinate',
  'describe',
  'display',
  'whisk',
]

// bot commands using new API, same names as above but lower-case
async function imagineCommands(guildId = null) {
  var cmd, cmdDef
  var commandResult = await getCommands(guildId)
  var commands = commandResult.map(command => command.name)

  interactionsCommands['promptMention'] = doMention
  interactionsCommands['promptPrivate'] = doPrivate

  cmdDef = {
    'name': 'imagine',
    'description': 'Prompt an image generator.',
    'options': [
      {
        'name': 'text',
        'description': 'Text to prompt stability diffusion with.',
        'required': true,
        'type': 3
      }
    ]
  }
  if(!commands.includes('imagine')) {
    await registerCommand(cmdDef, null, guildId)
  } else {
    cmd = commandResult.filter(c => c.name == 'imagine')[0]
    if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
      await updateCommand(cmdDef, null, cmd.id, guildId)
  }
  interactionsCommands['imagine'] = doImagineProxy

  cmdDef = {
    'name': 'hallucinate',
    'description': 'Prompt an image generator with weirder content.',
    'options': [
      {
        'name': 'text',
        'description': 'Text to prompt the llm with then use with stable diffusion.',
        'required': true,
        'type': 3
      }
    ]
  }
  if(!commands.includes('hallucinate')) {
    await registerCommand(cmdDef, null, guildId)
  } else {
    cmd = commandResult.filter(c => c.name == 'hallucinate')[0]
    if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
      await updateCommand(cmdDef, null, cmd.id, guildId)
  }
  interactionsCommands['hallucinate'] = doHallucinate


  cmdDef = {
    'name': 'describe',
    'description': 'Prompt Ollama 3.2 Vision with an image to describe.',
    'options': [
      {
        'name': 'url',
        'description': 'URL to the image you want a description of.',
        'required': true,
        'type': 3
      }
    ]
  }
  if(!commands.includes('describe')) {
    await registerCommand(cmdDef, null, guildId)
  } else {
    cmd = commandResult.filter(c => c.name == 'describe')[0]
    if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
      await updateCommand(cmdDef, null, cmd.id, guildId)
  }
  interactionsCommands['describe'] = doVision

  
  cmdDef = {
    'name': 'whisk',
    'description': 'Run a series of prompts to combine input images and stages.',
    'options': [
      {
        'name': 'subject',
        'description': 'URL/prompt to the image you want to use as the subject.',
        'required': true,
        'type': 3
      },
      {
        'name': 'scene',
        'description': 'URL/prompt to the image you want to use as the scenery.',
        'required': false,
        'type': 3
      },
      {
        'name': 'style',
        'description': 'URL/prompt to the image you want to use as the style.',
        'required': false,
        'type': 3
      }
    ]
  }
  if(!commands.includes('whisk')) {
    await registerCommand(cmdDef, null, guildId)
  } else {
    cmd = commandResult.filter(c => c.name == 'whisk')[0]
    if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
      await updateCommand(cmdDef, null, cmd.id, guildId)
  }
  interactionsCommands['whisk'] = doWhisk


  interactionsCommands['startActivity'] = discordExpress
  interactionsCommands['endActivity'] = closeExpress
  
  cmdDef = {
    'name': 'display',
    'description': 'Launch an activity',
    'type': 4,
    'handler': 2,
  }
  if(!commands.includes('display')) {
    await registerCommand(cmdDef, null, guildId)
  } else {
    cmd = commandResult.filter(c => c.name == 'display')[0]
    if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
      await updateCommand(cmdDef, null, cmd.id, guildId)
  }

  
  var toRemove = commandResult.filter(c => !ALL_COMMANDS.includes(c.name))
  for(var i = 0; i < toRemove.length; i++) {
    await deleteCommand(toRemove[i].id, guildId)
  }

  return await getCommands()
}


module.exports = imagineCommands

What the code could have been:

const {
  registerCommand,
  getCommands,
  deleteCommand,
  updateCommand,
} = require('./discord-api');
const { interactionsCommands } = require('./discord-gateway');
const {
  doHallucinate,
  doImagineProxy,
  doWhisk,
} = require('./discord-stability-proxy');
const doVision = require('./discord-ollama-vision');
const { discordExpress, closeExpress } = require('./discord-music-player-server');
const { doMention, doPrivate } = require('./discord-llm-interactions');

const ALL_COMMANDS = [
  'imagine',
  'hallucinate',
  'describe',
  'display',
  'whisk',
];

/**
 * Register, update, and delete discord commands using the new API.
 *
 * @param {string} [guildId=null] The ID of the guild to operate on.
 * @returns {Promise} A promise resolving to the updated list of commands.
 */
async function imagineCommands(guildId = null) {
  const commands = await getCommands(guildId);
  const toRegister = [];

  const commandMap = ALL_COMMANDS.reduce((acc, name) => {
    acc[name] = {
      name,
      description: `Prompt an image generator.`,
      options: [
        {
          name: 'text',
          description: 'Text to prompt stability diffusion with.',
          required: true,
          type: 3,
        },
      ],
    };

    return acc;
  }, {});

  Object.keys(commandMap).forEach((name) => {
    const cmdDef = commandMap[name];

    if (!commands.some((cmd) => cmd.name === name)) {
      toRegister.push(cmdDef);
    } else {
      const existingCmd = commands.find((cmd) => cmd.name === name);
      if (cmdDef.name!== existingCmd.name || cmdDef.description!== existingCmd.description) {
        toRegister.push(cmdDef);
      }
    }
  });

  await Promise.all(
    toRegister.map(async (cmdDef, index) => {
      if (index === 0) {
        // Register first command
        await registerCommand(cmdDef, null, guildId);
        interactionsCommands['promptMention'] = doMention;
        interactionsCommands['promptPrivate'] = doPrivate;
      } else if (index === 1) {
        // Register second command
        await registerCommand(cmdDef, null, guildId);
        interactionsCommands['hallucinate'] = doHallucinate;
      } else if (index === 2) {
        // Register third command
        await registerCommand(cmdDef, null, guildId);
        interactionsCommands['describe'] = doVision;
      } else if (index === 3) {
        // Register fourth command
        await registerCommand(cmdDef, null, guildId);
        interactionsCommands['whisk'] = doWhisk;
      } else if (index === 4) {
        // Register fifth command
        await registerCommand(cmdDef, null, guildId);
        interactionsCommands['startActivity'] = discordExpress;
        interactionsCommands['endActivity'] = closeExpress;
      } else if (index === 5) {
        // Register sixth command
        await registerCommand(cmdDef, null, guildId);
        interactionsCommands['display'] = 'Launch an activity';
      } else {
        await registerCommand(cmdDef, null, guildId);
      }
    })
  );

  const toRemove = commands.filter((cmd) =>!ALL_COMMANDS.includes(cmd.name));
  await Promise.all(toRemove.map((cmd) => deleteCommand(cmd.id, guildId)));

  return await getCommands();
}

module.exports = imagineCommands;

Function Breakdown

This is a JavaScript code that defines a set of functions related to Discord bot commands. It imports various modules from a library called importer and uses them to manage Discord commands.

Functions

  1. imagineCommands: This is the main function that handles commands related to image generation. It takes an optional guildId parameter and performs the following tasks:
  2. doMention and doPrivate: These functions are assigned to the interactionsCommands object, but their purpose is not clear from the code snippet.
  3. doVision: This function is imported from a module called discord ollama vision, but its purpose is not clear from the code snippet.
  4. discordExpress and closeExpress: These functions are imported from a module called discord music player server, but their purpose is not clear from the code snippet.

Variables

  1. ALL_COMMANDS: This is an array of command names that are used to validate the existence of commands.
  2. interactionsCommands: This is an object that stores functions that can be used as interactions with the Discord bot.

Notes