![](https://i0.wp.com/technstuff.tech/wp-content/uploads/2023/03/pngegg-2.png?resize=840%2C840&ssl=1)
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.
Leave a Reply