In this blog post, we'll explore how to integrate Google Generative AI into a Discord bot to enhance content generation. The code snippet provided below showcases a function called
discordGeminiAI, which leverages the capabilities of Google's Generative AI to generate content based on a user's message and attached image.

Setting up the Environment

Before diving into the code, make sure you have the necessary dependencies installed, including the @google/generative-ai library and any other relevant packages. Additionally, ensure you have obtained an API key from Google to access their Generative AI service.

Understanding Discord.js Collections

Discord.js, a popular JavaScript library for interacting with the Discord API, uses a Collection to manage various data structures. In this case, message.attachments is a Discord.js Collection containing Map objects. The code begins by checking the presence and size of the attachments to ensure they are in the expected format.

if (!message.attachments || message.attachments.size === 0) {
  console.error('No attachments found');
  return 'No attachments found';
}

This conditional statement ensures that the bot proceeds only when there are attachments to process. If the message.attachments collection is empty, an error is logged, and the function returns a corresponding message.

Integrating Google Generative AI

The code then initializes the Google Generative AI using the provided API key and retrieves the desired generative model, in this case, 'gemini-pro-vision'

const genAI = new GoogleGenerativeAI('your key');
const model = genAI.getGenerativeModel({ model: 'gemini-pro-vision' });


const prompt = message.content;

const images = [];

const attachmentPromises = message.attachments.map(async (attachment) => {

  try {

    const img = await imageUrlToBase64(attachment[1]?.attachment, attachment[1]?.contentType);

    images.push(img);

  } catch (error) {

    console.error('Error:', error.message);

    throw error;

  }


});


Content Generation with Google Generative AI

Once the images are processed, the function utilizes Google's Generative AI to generate content based on the user's message and attached images.

try {
  await Promise.all(attachmentPromises);

  const result = await model.generateContent([prompt, ...images]);
  const response = await result.response;
  const text = response.text();

  // Check text length and log/return accordingly
  if (text.length <= 2000) {
    console.log(text);
    return text;
  } else {
    const truncatedText = text.substring(0, 2000);
    console.log(truncatedText);
    return truncatedText;
  }
} catch (error) {
  console.error('Error generating content:', error);
  return 'Error generating content.';
}

Integrating Google Generative AI into a Discord bot opens up exciting possibilities for enhancing user interactions. Whether you're creating a chatbot, generating creative responses, or adding a touch of AI to your Discord server, this code serves as a starting point for exploring the potential of generative models in your applications.