discord tools | discord pdf converter | discord llm connector | Search

The code imports various modules and defines functions to interact with the Discord API, interpret code, and summarize articles. It exports three main functions: doInterpret, doSystemUsage, and doSummary, which are used to provide user interaction and generate content on demand.

Run example

npm run import -- "discord notebook connector"

discord notebook connector

const systemBuffer = importer.import("system usage png buffer")
const {patchInteractionImageAttachment} = importer.import("update discord interaction with attachments")
const {triggerTyping, createMessage, updateInteraction} = importer.import("disrcord api")
const summerizeArticle = importer.import("test article summarizer")

/*
async function doVoice(interaction) {
  const {llmVoice} = await importer.import("llm voice")
  await triggerTyping(interaction.channel_id)
  let a1 = await llmVoice('Convert the text to speech: ' + interaction.data.options[0].value)
  return await patchInteractionImageAttachment(interaction.data.options[0].value, Buffer.from(a1), interaction.token, 'audio/wav')
}
*/

async function doSystemUsage(interaction) {
  let buffer = await systemBuffer()
  await patchInteractionImageAttachment('system usage', buffer, interaction.token)
  let update
  let count = 24
  update = setInterval(async () => {
    count--
    buffer = await systemBuffer()
    await patchInteractionImageAttachment('system usage', buffer, interaction.token)
    if(count == 0) {
      clearInterval(update)
    }
  }, 4000)
}

async function doInterpret(interaction) {
  let code = await importer.interpret(interaction.data.options[0].value)
  await updateInteraction({
    content: code.language + ' code:\n```' + (code.language == 'node' ? 'javascript' : code.language) + '\n' + code.code.substring(0, 1800) + '\n```\n'
  }, interaction.id, interaction.token)
}

async function doSummary(interaction) {
  let summary = await summerizeArticle(interaction.data.options[0].value)
  await updateInteraction({
    content: 'Link: ' + interaction.data.options[0].value + '\nTLDR: ' + summary[1] + '\n\n' + summary[0]
  }, interaction.id, interaction.token)
}

module.exports = {
  doInterpret,
  doSystemUsage,
  //doVoice,
  doSummary,
}

What the code could have been:

// Import required modules
const { 
  systemBuffer, 
  patchInteractionImageAttachment, 
  triggerTyping, 
  createMessage, 
  updateInteraction 
} = importer.import('discord api');

const { interpret } = importer.import('code interpreter');
const summerizeArticle = importer.import('test article summarizer');
const { llmVoice } = importer.import('llm voice');

// Define constant for system update interval
const SYSTEM_UPDATE_INTERVAL = 4000;

// Define constant for system update count
const SYSTEM_UPDATE_COUNT = 24;

// Define function to update system usage
async function doSystemUsage(interaction) {
  // Initialize system buffer
  let buffer;

  // Get initial system buffer
  try {
    buffer = await systemBuffer();
  } catch (error) {
    console.error(`Error getting system buffer: ${error}`);
    return;
  }

  // Update system usage interaction
  try {
    await patchInteractionImageAttachment('system usage', buffer, interaction.token);
  } catch (error) {
    console.error(`Error updating system usage interaction: ${error}`);
    return;
  }

  // Start system update interval
  const updateIntervalId = setInterval(async () => {
    // Decrement system update count
    const count = SYSTEM_UPDATE_COUNT - 1;

    // Get updated system buffer
    try {
      buffer = await systemBuffer();
    } catch (error) {
      console.error(`Error getting updated system buffer: ${error}`);
      clearInterval(updateIntervalId);
      return;
    }

    // Update system usage interaction
    try {
      await patchInteractionImageAttachment('system usage', buffer, interaction.token);
    } catch (error) {
      console.error(`Error updating system usage interaction: ${error}`);
      clearInterval(updateIntervalId);
      return;
    }

    // Check if system update interval should expire
    if (count === 0) {
      clearInterval(updateIntervalId);
    }
  }, SYSTEM_UPDATE_INTERVAL);
}

// Define function to interpret code
async function doInterpret(interaction) {
  // Get code to interpret
  const code = interaction.data.options[0].value;

  try {
    // Interpret code
    const result = await interpret(code);

    // Update interaction with code interpretation
    try {
      await updateInteraction({
        content: `${result.language} code:\n```${result.language === 'node'? 'javascript' : result.language}\n${result.code.substring(0, 1800)}\n````
      }, interaction.id, interaction.token);
    } catch (error) {
      console.error(`Error updating interaction with code interpretation: ${error}`);
    }
  } catch (error) {
    console.error(`Error interpreting code: ${error}`);
  }
}

// Define function to summarize article
async function doSummary(interaction) {
  // Get article to summarize
  const article = interaction.data.options[0].value;

  try {
    // Summarize article
    const summary = await summerizeArticle(article);

    // Update interaction with article summary
    try {
      await updateInteraction({
        content: `Link: ${article}\nTLDR: ${summary[1]}\n\n${summary[0]}`
      }, interaction.id, interaction.token);
    } catch (error) {
      console.error(`Error updating interaction with article summary: ${error}`);
    }
  } catch (error) {
    console.error(`Error summarizing article: ${error}`);
  }
}

// Define function to convert text to speech
async function doVoice(interaction) {
  // Get text to convert
  const text = interaction.data.options[0].value;

  try {
    // Convert text to speech
    const audio = await llmVoice(`Convert the text to speech: ${text}`);

    // Update interaction with audio attachment
    try {
      await patchInteractionImageAttachment('text to speech', Buffer.from(audio), interaction.token, 'audio/wav');
    } catch (error) {
      console.error(`Error updating interaction with audio attachment: ${error}`);
    }
  } catch (error) {
    console.error(`Error converting text to speech: ${error}`);
  }
}

// Export functions
module.exports = {
  doInterpret,
  doSystemUsage,
  doVoice,
  doSummary
};

Code Breakdown

Importing Modules

The code imports various modules from the importer object:

Functions

doSystemUsage

doInterpret

doSummary

Exports

The code exports the following functions:

Note: The doVoice function is commented out and not exported.