Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

discord.js: 'ReferenceError: Message not defined'

im gonna go to make an open ai discord bot but this is happen
yesterday, i make open ai bot with SAME CODE like this but today it full with error
someone pls help me

require('dotenv/config');
const{ Client } = require('discord.js');
const{ OpenAI } = require('openai');

const client = new Client({
    intents: [
        'Guilds', 
        'GuildMembers', 
        'GuildMessages', 
        'MessageContent'],
});

client.on('ready', () => {
    console.log('gpt READY!')
})

const IGNORE_PREFIX = "!";
const CHANNELS = ['1041682904729800829', '1042040860382089297'];

const openai = new OpenAI({
    apiKey: process.env.OPENAI_KEY,
})

client.on('messageCreate', async (message) => {
    if (message.author.bot) return;
    if (message.content.startsWith(IGNORE_PREFIX)) return;
    if (!CHANNELS.includes(message.channelId) && !message.mentions.users.has(client.user.id)) return;

    const response = await openai.chat.completions.create({
        model: 'gpt-3.5-turbo-16k-0613',
        messages: [
            {
                // name:
                role: 'system',
                content: 'Chat GPT is a friendly chatbot.'
            },
            {
                // name:
                role: 'user',
                content: messages.content,
            }
        ]
    }).catch((error) => console.error('OpenAI Error:\n',error));

    message.reply(response.choices[0].message.content);
});

client.login(process.env.TOKEN);

and this is happen

gpt READY!
node:events:496
      throw er; // Unhandled 'error' event
      ^

ReferenceError: messages is not defined
    at Client.<anonymous> (D:\---------\APP\gpt discord.js\index.js:41:26)

Node.js v20.11.1
[nodemon] app crashed - waiting for file changes before starting...

pls help me im a beginner javacript coder so pls help me
i apreciate you if you help me

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem occurs because you are using a messages variable that is not defined in your code. To fix this, change messages.content to message.content.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading