#Chat-GPT Bot

6 messages · Page 1 of 1 (latest)

steel swan
#

my api_key in the .env file does not work and whenever i want to refer to it and use it, it says "undefined".
here is the code:

require('dotenv/config');
const { Client, IntentsBitField } = require('discord.js');
const {Configuration, OpenAIApi } = require('openai');

const client = new Client({
    intents: [
        IntentsBitField.Flags.Guilds,
        IntentsBitField.Flags.GuildMessages,
        IntentsBitField.Flags.MessageContent,
    ]
});

client.on('ready', () => {
    console.log('Bot is online.');
});
console.log(process.env.API_KEY);
const configuration = new Configuration({
    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 friendly chatbot.'}];

    await message.channel.sendTyping();

    let prevMessages = await message.channel.messages.fetch({ limit: 15 });
    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);
#

the error message:

error: { message: 'Incorrect API key provided: undefined. You can find your API key at https://platform.openai.com/account/api-keys.', type: 'invalid_request_error', param: null, code: 'invalid_api_key' } } }, isAxiosError: true, toJSON: [Function: toJSON] }

limber bear
steel swan
#

thanks tho!

limber bear
#

np