discord tools | discord mesh generator | sync discord llm tools | Search

The code defines an interaction module that exports three functions (doInteraction, doMention, and doPrivate) to handle different types of interactions with the bot, including greetings, mentions, and private messages. Each function uses a combination of imported modules and functions to generate responses, such as triggering typing, selecting models, and creating messages.

Run example

npm run import -- "discord llm interactions"

discord llm interactions

const {DEFAULT_MODEL} = importer.import("general chit chat")
const {DEFAULT_APPLICATION} = importer.import("discord configuration")
const {triggerTyping, createMessage} = importer.import("disrcord api")
const handleConversation = importer.import("handle conversation")
const selectModel = importer.import("select llm")

async function doInteraction(interaction) {
  let prompt
  if(interaction.data.name == 'hello') {
    prompt = 'hello'
  }
  else { // if (interaction.data.name == 'prompt') {
    prompt = interaction.data.options[0].value
  }
  let a1 = await llmPrompt(prompt + '\nmake it short.')
  await updateInteraction({
    content: a1.substring(0, 1800)
  }, interaction.id, interaction.token)
}

async function doMention(interaction) {
  await triggerTyping(interaction.channel_id)
  let q1 = interaction.content.replace('<@' + DEFAULT_APPLICATION + '>', '') + '\nmake it short.'

  let promptModel = await selectModel(DEFAULT_MODEL)
  let result = await handleConversation(promptModel, interaction.author.id, q1)

  return await createMessage({
    content: result.substring(0, 1800), 
    message_reference: {
      message_id: interaction.id
    }
  }, interaction.channel_id)
}

async function doPrivate(interaction) {
  await triggerTyping(interaction.channel_id)
  
  let promptModel = await selectModel(DEFAULT_MODEL)
  let result = await handleConversation(promptModel, interaction.author.id, interaction.content)

  return await createMessage({
    content: result.substring(0, 1800), 
    message_reference: {
      message_id: interaction.id
    }
  }, interaction.channel_id)
}

module.exports = {
  doInteraction,
  doMention,
  doPrivate
}

What the code could have been:

const {
  DEFAULT_MODEL,
  DEFAULT_APPLICATION,
} = require('./general-chit-chat-importer');

const {
  triggerTyping,
  createMessage,
} = require('./discord-api-importer');

const {
  handleConversation,
} = require('./handle-conversation-importer');

const {
  selectModel,
} = require('./select-llm-importer');

/**
 * Handles interactions with the bot.
 *
 * @param {object} interaction - The interaction object.
 * @returns {Promise} A promise resolving when the interaction is handled.
 */
async function doInteraction(interaction) {
  const prompt = getPrompt(interaction);

  const result = await llmPrompt(prompt + '\nMake it short.');
  await updateInteraction({
    content: result.substring(0, 1800),
  }, interaction.id, interaction.token);
}

/**
 * Handles mentions with the bot.
 *
 * @param {object} interaction - The interaction object.
 * @returns {Promise} A promise resolving when the mention is handled.
 */
async function doMention(interaction) {
  await triggerTyping(interaction.channel_id);
  const q1 = interaction.content.replace(`<@${DEFAULT_APPLICATION}>`, '') + '\nMake it short.';

  const promptModel = await selectModel(DEFAULT_MODEL);
  const result = await handleConversation(promptModel, interaction.author.id, q1);

  return await createMessage({
    content: result.substring(0, 1800),
    message_reference: {
      message_id: interaction.id,
    },
  }, interaction.channel_id);
}

/**
 * Handles private messages with the bot.
 *
 * @param {object} interaction - The interaction object.
 * @returns {Promise} A promise resolving when the private message is handled.
 */
async function doPrivate(interaction) {
  await triggerTyping(interaction.channel_id);

  const promptModel = await selectModel(DEFAULT_MODEL);
  const result = await handleConversation(promptModel, interaction.author.id, interaction.content);

  return await createMessage({
    content: result.substring(0, 1800),
    message_reference: {
      message_id: interaction.id,
    },
  }, interaction.channel_id);
}

/**
 * Gets the prompt for the interaction.
 *
 * @param {object} interaction - The interaction object.
 * @returns {string} The prompt.
 */
function getPrompt(interaction) {
  if (interaction.data.name === 'hello') {
    return 'Hello';
  } else {
    return interaction.data.options[0].value;
  }
}

module.exports = {
  doInteraction,
  doMention,
  doPrivate,
};

// TODO: Move the getPrompt function to a separate file.
// TODO: Implement logging for errors.

Code Breakdown

Importing Modules

const {DEFAULT_MODEL} = importer.import('general chit chat')
const {DEFAULT_APPLICATION} = importer.import('discord configuration')
const {triggerTyping, createMessage} = importer.import('disrcord api')
const handleConversation = importer.import('handle conversation')
const selectModel = importer.import('select llm')

Interaction Functions

async function doInteraction(interaction) {
  //...
}

async function doMention(interaction) {
  //...
}

async function doPrivate(interaction) {
  //...
}

doInteraction Function

async function doInteraction(interaction) {
  let prompt
  if(interaction.data.name == 'hello') {
    prompt = 'hello'
  }
  else { 
    prompt = interaction.data.options[0].value
  }
  let a1 = await llmPrompt(prompt + '\nmake it short.')
  await updateInteraction({
    content: a1.substring(0, 1800)
  }, interaction.id, interaction.token)
}

doMention Function

async function doMention(interaction) {
  await triggerTyping(interaction.channel_id)
  let q1 = interaction.content.replace('<@' + DEFAULT_APPLICATION + '>', '') + '\nmake it short.'

  let promptModel = await selectModel(DEFAULT_MODEL)
  let result = await handleConversation(promptModel, interaction.author.id, q1)

  return await createMessage({
    content: result.substring(0, 1800), 
    message_reference: {
      message_id: interaction.id
    }
  }, interaction.channel_id)
}

doPrivate Function

async function doPrivate(interaction) {
  await triggerTyping(interaction.channel_id)
  
  let promptModel = await selectModel(DEFAULT_MODEL)
  let result = await handleConversation(promptModel, interaction.author.id, interaction.content)

  return await createMessage({
    content: result.substring(0, 1800), 
    message_reference: {
      message_id: interaction.id
    }
  }, interaction.channel_id)
}

Module Exports

module.exports = {
  doInteraction,
  doMention,
  doPrivate
}