discord imagine | update discord interaction with attachments | discord mid journey commands | Search

The code defines two asynchronous functions: doHallucinate and doImagineProxy, which generate images based on user input and interact with the Discord API using various imported modules and functions. The functions handle different scenarios, such as guilds, typing indicators, and rating checks, to ensure a secure and interactive experience.

Run example

npm run import -- "discord stability proxy"

discord stability proxy

const {patchInteractionImageAttachment} = importer.import("update discord interaction with attachments")
const {doStableRequest} = importer.import("stable diffusion request")
const whiskImages = importer.import("whisk images")
const {triggerTyping, updateInteraction} = importer.import("disrcord api")

async function doHallucinate(interaction) {
  const {llmPrompt} = await importer.import("create llm session")
  let a1 = await llmPrompt('Imagine a scene that is much more exotic, weird, bizarre and detailed with this topic:\n' 
    + interaction.data.options[0].value + '\nFit as much detail as possible in to two or three sentences.')
  await triggerTyping(interaction.channel_id)
  if(interaction.guild_id) {
    let q2 = 'Would this prompt be considered to be rated R or for mature audiences, answer Yes or No only?\n' + a1
    let a2 = await llmPrompt(q2)
    if(a2.includes('Yes')) {
      return await updateInteraction({
        content: 'I will not.'
      }, interaction.id, interaction.token)
    }
  }
  await updateInteraction('This could take a while...', interaction.id, interaction.token)
  let {image} = await doStableRequest(a1)
  return await patchInteractionImageAttachment(interaction.data.options[0].value + '* -> *' + a1, image, interaction.token)
}


async function doImagineProxy(interaction) {
  await triggerTyping(interaction.channel_id)
  if(interaction.guild_id) {
    const {llmPrompt} = await importer.import("create llm session")
    let q2 = 'Would this prompt be considered to be rated PG or safe for children, answer Yes or No only?\n' + interaction.data.options[0].value
    let a2 = await llmPrompt(q2)
    if(a2.includes('No')) {
      return await updateInteraction({
        content: 'I will not.'
      }, interaction.id, interaction.token)
    }
  }
  await updateInteraction('This could take a while...', interaction.id, interaction.token)
  let {image} = await doStableRequest(interaction.data.options[0].value)
  //const fs = require('fs')
  //fs.writeFileSync('/Users/briancullinan/Downloads/test.png', buff)
  //console.log(atob(result.data.images[0]))
  return await patchInteractionImageAttachment(interaction.data.options[0].value, image, interaction.token)
}


async function doWhisk(interaction) {
  let {image} = await whiskImages(interaction.data.options[0].value, interaction.data.options[1].value, interaction.data.options[2].value)
  return await patchInteractionImageAttachment(interaction.data.options[0].value, image, interaction.token)
}


module.exports = {
  doHallucinate,
  doImagineProxy,
  doWhisk,
}

What the code could have been:

// Import necessary modules
const {
  patchInteractionImageAttachment,
  triggerTyping,
  updateInteraction,
} = require('./discord-api');
const { doStableRequest } = require('./stable-diffusion-request');
const { whiskImages } = require('./whisk-images');

// Define a constant for the interaction API
const interactionApi = {
  triggerTyping,
  updateInteraction,
};

// Define a constant for the stable diffusion request API
const stableDiffusionRequestApi = {
  doStableRequest,
};

// Define a constant for the whisk images API
const whiskImagesApi = {
  whiskImages,
};

// Define the doHallucinate function
async function doHallucinate(interaction) {
  // TODO: Consider caching the LLM session to avoid repeated imports
  const { llmPrompt } = await require('./create-llm-session')();
  
  // Ask the LLM to generate a hallucinated scene
  const prompt = `Imagine a scene that is much more exotic, weird, bizarre and detailed with this topic: ${interaction.data.options[0].value}\nFit as much detail as possible in to two or three sentences.`;
  const hallucinatedScene = await llmPrompt(prompt);

  // Check if the hallucinated scene is rated R
  const isRatedR = await checkIfRatedR(interaction, hallucinatedScene, llmPrompt);

  // If the hallucinated scene is rated R, return an error message
  if (isRatedR) {
    return updateInteraction('I will not.', interaction.id, interaction.token);
  }

  // Otherwise, generate an image using the stable diffusion request API
  const { image } = await generateImage(interaction.data.options[0].value, hallucinatedScene, stableDiffusionRequestApi);

  // Return the generated image
  return patchInteractionImageAttachment(`${interaction.data.options[0].value} -> ${hallucinatedScene}`, image, interaction.token);
}

// Define the doImagineProxy function
async function doImagineProxy(interaction) {
  // Check if the interaction is rated PG
  const isRatedPG = await checkIfRatedPG(interaction, interaction.data.options[0].value);

  // If the interaction is not rated PG, return an error message
  if (!isRatedPG) {
    return updateInteraction('I will not.', interaction.id, interaction.token);
  }

  // Otherwise, generate an image using the stable diffusion request API
  const { image } = await generateImage(interaction.data.options[0].value, null, stableDiffusionRequestApi);

  // Return the generated image
  return patchInteractionImageAttachment(interaction.data.options[0].value, image, interaction.token);
}

// Define the doWhisk function
async function doWhisk(interaction) {
  // Generate an image using the whisk images API
  const { image } = await whiskImagesApi.whiskImages(
    interaction.data.options[0].value,
    interaction.data.options[1].value,
    interaction.data.options[2].value,
  );

  // Return the generated image
  return patchInteractionImageAttachment(interaction.data.options[0].value, image, interaction.token);
}

// Define the checkIfRatedR function
async function checkIfRatedR(interaction, hallucinatedScene, llmPrompt) {
  // Check if the interaction is in a guild
  if (!interaction.guild_id) {
    return false;
  }

  // Ask the LLM to determine if the hallucinated scene is rated R
  const isRatedR = await llmPrompt(`Would this prompt be considered to be rated R or for mature audiences, answer Yes or No only?\n${hallucinatedScene}`);

  // Return true if the hallucinated scene is rated R, false otherwise
  return isRatedR.includes('Yes');
}

// Define the checkIfRatedPG function
async function checkIfRatedPG(interaction, prompt) {
  // Check if the interaction is in a guild
  if (!interaction.guild_id) {
    return true;
  }

  // Ask the LLM to determine if the prompt is rated PG
  const isRatedPG = await require('./create-llm-session')().llmPrompt(`Would this prompt be considered to be rated PG or safe for children, answer Yes or No only?\n${prompt}`);

  // Return true if the prompt is rated PG, false otherwise
  return isRatedPG.includes('Yes');
}

// Define the generateImage function
async function generateImage(prompt, hallucinatedScene, stableDiffusionRequestApi) {
  // Generate an image using the stable diffusion request API
  const { image } = await stableDiffusionRequestApi.doStableRequest(prompt);

  // Return the generated image
  return { image };
}

// Export the functions
module.exports = {
  doHallucinate,
  doImagineProxy,
  doWhisk,
};

Code Breakdown

Imported Modules

The code starts by importing various modules:

These modules are imported from the importer object, which is not shown in the code snippet.

Functions

The code defines two asynchronous functions:

doHallucinate

doImagineProxy

Notes