I am making a discord bot to turn on and off the aternos Minecraft server

280 Views Asked by At

I got bot.js from chatgpt and I'm using node v20.3.0 however I get this error about intents

error:

PS C:\Users\joe\node_modules\discord.js> node bot.js
C:\Users\joe\node_modules\discord.js\bot.js:8
const intents = new Intents();
                ^

TypeError: Intents is not a constructor
    at Object.<anonymous> (C:\Users\joe\node_modules\discord.js\bot.js:8:17)
    at Module._compile (node:internal/modules/cjs/loader:1255:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1309:10)
    at Module.load (node:internal/modules/cjs/loader:1113:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47

bot.js:

const Discord = require('discord.js');
const { Client, Intents } = require('discord.js');
const axios = require('axios');

const serverIP = 'YOUR_ATERNOS_SERVER_IP';
const serverPort = 'YOUR_ATERNOS_SERVER_PORT';

const intents = new Intents();
intents.add(Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES);

const client = new Client({ intents });

client.once('ready', () => {
  console.log('Bot is online!');
});

client.on('messageCreate', (message) => {
  if (message.content === '!startserver') {
    startServer(message.channel);
  } else if (message.content === '!stopserver') {
    stopServer(message.channel);
  }
});

async function startServer(channel) {
  try {
    await axios.get(`https://aternos.org/server/${serverIP}:${serverPort}/start`);
    channel.send('Starting the server...');
  } catch (error) {
    console.error(error);
    channel.send('Failed to start the server.');
  }
}

async function stopServer(channel) {
  try {
    await axios.get(`https://aternos.org/server/${serverIP}:${serverPort}/stop`);
    channel.send('Stopping the server...');
  } catch (error) {
    console.error(error);
    channel.send('Failed to stop the server.');
  }
}

client.login('YOUR_DISCORD_BOT_TOKEN');

I tried fixing the script but I still get the same error all I need is the discord bot that can remotely turn on and off the Minecraft server

1

There are 1 best solutions below

0
cozen On

Discord.js intents are not supposed to be used that way. To get you bot to work you need to change to following lines:

const intents = new Intents();
intents.add(Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES);

const client = new Client({ intents });

Change the to:

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    // Add more intents here
  ],
});

Check out the documentation here.