EN VI

Javascript - discord.js: 'ReferenceError: Message not defined'?

2024-03-10 10:00:04
How to Javascript - 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

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.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login