Having issues with the API in a Node JS script for trying to add the chatbot to a discord server. Error: Cannot read properties of undefined (reading 'create'). I have looked at the documentation and when I try other intializations of awai openai.Completion statements I am still getting wither that the function is bad or the cannot read properties error. I have all the debugging connected. Wondering if someone can help me sort this out
#Node JS API Script issues
4 messages · Page 1 of 1 (latest)
```
//create a discord bot using openai api that interacts with the discord server
require('dotenv').config();
//connect the discord api
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]})
const openaiExports = require('openai');
console.log(openaiExports);
//connect the openai api
const OpenAI = require('openai').OpenAI;
const openai = new OpenAI({
apiKey: process.env.OPENAI_KEY
});
console.log(Object.getOwnPropertyNames(openai));
const Chat = require('openai').Chat;
const chatInstance = new Chat(openai);
//check for when a message on discord has been sent
client.on('messageCreate', async function(message){
try{
//make sure it doesnt repond to itself or other bots
if(message.author.bot) return;
const response = await openai.Completion.create({
model: "gpt-4",
messages: [
{ role: "system", content: "ChatGPT is fren." },
{ role: "assistant", content: "Hello, How are you?" },
{ role: "user", content: ${message.author.username}: ${message.content} }
],
temperature: 0.9,
max_tokens: 100,
stop: ["ChatGPT:"],
});
const assistantReply = gptResponse.data.choices[0].message.content;
message.reply(assistantReply);
} catch(err) {
console.log("Error:", err.message);
}
});
//log the bot into the discord server
client.login(process.env.DISCORD_TOKEN);
console.log("ChatGPT Bot is Online on Discord")
```
I tried the code block posted in the API support channel information but I dont know if it worked apologies if it is bad
Its fixed