Search code examples
node.jsdiscord

Error connecting Chatgpt API to Discord Server


I'm trying to use the ChatGPT Api and connect to a discord bot but I am getting this error. Can anyone show me how to fix this?

Error

ReferenceError: require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension and 'C:\Users\jkru0\OneDrive\Desktop\gpt\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

Full Code

import Discord from 'discord.js';
import { ChatGPTAPI } from 'chatgpt';
import readline from 'readline';

const apiKey = 'hidden';
const api = new ChatGPTAPI({ apiKey });
const { Client, Intents } = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
  

let conversationId;
let parentMessageId;

async function sendMessage(message, channel) {
  let res = await api.sendMessage(message, {
    conversationId: conversationId,
    parentMessageId: parentMessageId
  });

  conversationId = res.conversationId;
  parentMessageId = res.id;

  console.log('\nBot:', res.text, '\n');

  if (channel) {
    await channel.send(res.text);
  }
}

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', async (message) => {
  if (message.author.bot) return;

  if (message.content === 'quit') {
    process.exit();
  }

  await sendMessage(message.content, message.channel);
});

async function main() {
  await sendMessage('Hello World!');

  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });

  function askQuestion() {
    rl.question('\nYou: ', async (question) => {
      if (question === 'quit') {
        rl.close();
        process.exit();
      }

      await sendMessage(question);
      askQuestion();
    });
  }

  askQuestion();
}

client.login('hidden');
main();

Solution

  • The error message is telling you that you cannot use require in an ES module, and you should use import instead. However, in your code, you are using both require and import statements.

    It is a simple fix by replacing require with import Example:

    import Discord, { Client, Intents } from 'discord.js';
    

    Full Code:

    import Discord, { Client, Intents } from 'discord.js';
    import { ChatGPTAPI } from 'chatgpt';
    import readline from 'readline';
    
    const apiKey = 'hidden';
    const api = new ChatGPTAPI({ apiKey });
    
    
    const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
      
    
    let conversationId;
    let parentMessageId;
    
    async function sendMessage(message, channel) {
      let res = await api.sendMessage(message, {
        conversationId: conversationId,
        parentMessageId: parentMessageId
      });
    
      conversationId = res.conversationId;
      parentMessageId = res.id;
    
      console.log('\nBot:', res.text, '\n');
    
      if (channel) {
        await channel.send(res.text);
      }
    }
    
    client.on('ready', () => {
      console.log(`Logged in as ${client.user.tag}!`);
    });
    
    client.on('message', async (message) => {
      if (message.author.bot) return;
    
      if (message.content === 'quit') {
        process.exit();
      }
    
      await sendMessage(message.content, message.channel);
    });
    
    async function main() {
      await sendMessage('Hello World!');
    
      const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
      });
    
      function askQuestion() {
        rl.question('\nYou: ', async (question) => {
          if (question === 'quit') {
            rl.close();
            process.exit();
          }
    
          await sendMessage(question);
          askQuestion();
        });
      }
    
      askQuestion();
    }
    
    client.login('hidden');
    main();