Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

AI

Jeremiah Lewis
seal-mask
.a{fill-rule:evenodd;}techdegree
Jeremiah Lewis
Front End Web Development Techdegree Student 10,533 Points

Bot not returning a message. How do I fix this?

So after following along twice, I keep getting the same error. I type into the discord server my question, at the bottom it says "ChatGPT is typing" then it stops and nothing happens. What could be causing this?

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("ChatGPT is online");
});

const configuration = new Configuration({
    apiKey: process.env.API
});

const openai = new OpenAIApi(configuration);

client.on('messageCreate', async (msg) => {
    if (msg.author.bot) return;
    if (msg.channel.id !== process.env.CHANNEL) return;
    if (msg.content.startsWith('!')) return;

    let conversationLog = [
        {
            role: 'system',
            content: 'You are a friendly chatbot.'
        }
    ];

    await msg.channel.sendTyping();

    let previousMessages = await msg.channel.messages.fetch({ limit: 15 });
    previousMessages.reverse();

    previousMessages.forEach((message) => {
        if (msg.content.startsWith('!')) return;
        if (message.author.id !== client.user.id && msg.author.bot) return;
        if (message.author.id !== msg.author.id) return;

        conversationLog.push({
            role: 'user',
            content: message.content,
        });
    });

    conversationLog.push({
        role: 'user',
        content: msg.content
    });



    const res = await openai.createChatCompletion({
        model: 'gpt-3.5-turbo',
        messages: conversationLog,
    });

    msg.reply(res.data.choices[0].message);
});

client.login(process.env.TOKEN);

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey Jeremiah Lewis 👋

It's hard to say what is causing this issue if I had to guess it most likely has to do with reaching limits. Most likely it's either timing out because of lengthy responses or you're running into the rate limiter. Are there any errors / warnings being shown in the console where you have the code running? This should provide more information as to what is happening 🙂

Hope this helps!