Better Discord.js Chatbots: Revamped AI Plugin for Robo.js
Tags: node, programming, discord, ai
Published: 2023-12-15 16:00:00

> **[Robo.js](https://github.com/Wave-Play/robo.js)** is a framework for creating **[Discord.js](https://discord.js.org)** bots with ease and best practices by default. Plugins extend your bot's features right away!

It's now easier than ever to create AI-powered Discord bots with the latest updates to the Robo.js AI Plugin. This major update allows you to inject knowledge into your bot, enabling it to remember past interactions and respond accordingly, rewrites the core AI engine, and adds new slash commands for more dynamic interactions.

By the way, for those new, this plugin makes creating a fully customizable AI Discord chatbot as easy as one command!

## Installation 💻

To add this plugin to your Robo.js project:
```bash
npx robo add @robojs/ai
```

New to Robo.js? Start your project with this plugin pre-installed:
```bash
npx create-robo <project-name> -p @robojs/ai
```

## Insights: Long-Term Memory and Knowledge Injection

One of the most significant updates is the introduction of the Insights feature, adding depth to AI interactions:

- **Contextual Long-Term Memory**: Insights feature allows AI to remember past interactions for more nuanced responses per channel.
- **Default Knowledge Injection**: Inject your bot with knowledge by placing documents in the project's `documents` folder. Any documents in this folder are automatically loaded into the AI engine's RAG model.

```
- /documents
	- my-first-document.txt
	- my-second-document.md
	- my-third-document.json
```

This feature uses OpenAI's Assistant API in the default model. Just starting your bot will automatically set up an Assistant instance matching your bot's name, commands, system messages, documents, and more.

Insights is enabled by default, but you can disable it in the plugin configuration:

```js
export default {
	insights: false
}
```

By the way, our very own Robo, Sage, uses this feature to help people learn about Robo.js. We fed it the [Robo.js documentation](https://docs.roboplay.dev) and a few other documents, and it's been learning ever since!

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lw4cs5ck4tanrj9obvyu.png)

## Custom AI Engine Support

You can now use any AI model of your choice with the new `engine` configuration option. Just extend the `BaseEngine` class and pass it to the plugin configuration:

```js
export default {
	engine: new MyCustomEngine()
}
```

By default, this plugin still uses OpenAI's API, but you can now use any AI engine you want. This feature is ideal for developers who want to use their own AI models or integrate with other AI APIs!

> **Note:** This feature is still in beta. We're working on improving the API and documentation for custom AI engine support.

## New Slash Commands for Dynamic Interaction

These new commands add a layer of versatility to how users interact with their AI-powered bots:

- **/chat**: Ideal for AI interactions when "Message Content" intent isn't enabled.
- **/imagine**: Enables your bot to generate images from text prompts.
- **/ai say**: A feature for moderators to communicate through the bot, with options for direct or AI-styled speech.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4keu2u4dvjavvpmrbp8h.png)

## Advanced Natural Language Command Execution

The plugin now adeptly handles complex responses and options, enhancing the AI-bot interaction:

- **Full Support for Dynamic Responses**: Including embeds, files, and actions in AI-driven interactions.
- **Improved Handling of Options**: Accurate processing of "Role" or "GuildMember" options in natural language commands.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2zmdakrv5jzxgeuce9y.png)

## Simplified and More Powerful JavaScript API

The JavaScript API has been refined for better usability and more creative control:

- **Renamed and Simplified AI API**: `AIEngine` is now `AI`, offering a more intuitive interface.
- **Enhanced Helper Functions**: Functions like `selectOne` benefit from the new JSON mode, improving their reliability.

## Breaking Changes: Key Updates to Note

This update includes some changes that may affect your current setup:

- **Environment Variable Update**: The plugin now uses `OPENAI_API_KEY` as the environment variable, replacing the previous `openaiKey` configuration option.
- **API Renaming**: The `AIEngine` export has been simplified and renamed to `AI`. This change streamlines the API and enhances its usability.

## Expanded Interaction Capabilities

The update also enhances voice and web interactions, making AI bots more versatile:

- **Voice Channel Interactions**: With `@robojs/ai-voice`, AI can now engage in voice channels. It will be updated within the next few days to support this latest update.
- **Web API Integration**: `@robojs/server` adds a new web chat endpoint for AI interactions.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efe4qxr5v9bradx93sz6.png)

This major update to the `@robojs/ai` opens up new possibilities for Discord.js bot developers using Robo.js. It brings sophistication and a new level of interaction to AI-powered bots!

[**➞ Documentation:** Getting started](https://roboplay.dev/docs)

[**➞ Community:** Join our Discord server](https://roboplay.dev/discord)

Experience the next level of bot interactions with the latest updates to the Robo.js AI Plugin!