Your own personal Jarvis: Setting Up a Discord Bot with OpenAi.

Growing up, I always dreamed of being Tony Stark – fast cars, beautiful women, and above all, JARVIS. The AI butler integrated into all his software/hardware, JARVIS was his best friend, assistant, and eventually a humanoid super being – a true force to be reckoned with. 

Here’s the thing – you can have that too! Just follow these simple steps and impress the absolute crap out of your friends:

First of all this is not my original code. I took and modified the code from this github.

Please refer to this video below if you want to follow along and see how to get the process started to make a Discord Bot.

Prerequisites:

VS Code

A Discord Bot ready to be used

Watch this video on how to create a Discord Bot and integrate OpenAi:

Step 1: Check Node.js Version

Before getting started, ensure that you have Node.js installed on your computer. Open Visual Studio Code and enter the following command in the terminal to check the version of Node.js you are running:

node -v

Step 2: Open a folder in VS Code

Use VS Code or the superior alternative VS Codium to open a folder that you want the code to reside in.

Step 3: Initialize a new npm project

In VS Code, go to View and select Terminal to open a new terminal window. Enter the following command to initialize a new npm project:

npm init -y

This command creates a package.json file in your directory.

Step 4: Install Dependencies

Enter the following command in the terminal to install the required dependencies:

npm install discord.js openai dotenv

This will compile node_modules and package-lock.json files in your directory.

Step 5: Create a .env file

Create a new file in the root directory of your project called .env. Inside this file, add the following lines of code, replacing the placeholders with your actual OpenAI API key, Discord bot token, and the ID of the Discord channel where you want your bot to operate:

TOKEN=your-<discord-bot-token-here>
API_KEY=<your-openai-api-key-here>
CHANNEL_ID=<your-discord-channel-id-here>

Step 6: Create an index.js file

Create a new file in the root directory of your project called index.js. Copy and paste the following code into the file:

require('dotenv/config');
const { Client, GatewayIntentBits} = require('discord.js');
const { Configuration, OpenAIApi } = require( 'openai');
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent
    ]
});
client.on('ready', () => {
    console.log("The bot is online!");
})
const configuration = new Configuration({
    organization: process.env.ORG_KEY,
    apiKey: process.env.API_KEY
})
const openai = new OpenAIApi(configuration);
client.on('messageCreate', async (message) => {
    if(message.author.bot) return;
    if(message.channel.id !== process.env.CHANNEL_ID) return;
    if(message.content.startsWith('!')) return;
    let conversationLog= [{ role: 'system', content: "You are a butler designed to help me in every way"}]
    await message.channel.sendTyping();
    let prevMessages = await message.channel.messages.fetch({ limit: 3 });
    prevMessages.reverse();
    prevMessages.forEach((msg) => {
        if (message.content.startsWith('!')) return;
        if (msg.author.id !== client.user.id && message.author.bot) return;
        if (msg.author.id !== message.author.id) return;
        conversationLog.push({
            role: 'user',
            content: msg.content,
        })
    });
    const result = await openai.createChatCompletion({
        model: 'gpt-3.5-turbo',
        messages: conversationLog,
    })
    message.reply(result.data.choices[0].message);
});
client.login(process.env.TOKEN);

This code sets up a new Discord bot client and creates an OpenAI API configuration. It then listens for new messages in the designated Discord channel and uses OpenAI’s GPT-3.5-Turbo Model.

Will release an update when I get access to the newest iteration, GPT 4.

7 responses to “Your own personal Jarvis: Setting Up a Discord Bot with OpenAi.”

  1. I appreciate your website, however I think you might check the spelling of a few of your postings. Even though I find it quite difficult to tell the truth because so many of them have spelling errors, I will most certainly return.

  2. I loved as much as you will receive carried out right here The sketch is attractive your authored material stylish nonetheless you command get got an impatience over that you wish be delivering the following unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike

  3. Obrigado, há muito tempo que procuro informações sobre este assunto e a sua é a melhor que descobri até agora. Mas e em relação aos resultados financeiros Você tem certeza em relação ao fornecimento

  4. Simplywall You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

  5. Normally I do not read article on blogs however I would like to say that this writeup very forced me to try and do so Your writing style has been amazed me Thanks quite great post

  6. Thanks I have just been looking for information about this subject for a long time and yours is the best Ive discovered till now However what in regards to the bottom line Are you certain in regards to the supply

  7. Your article helped me a lot, is there any more related content? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *