-2

I am having an issue getting my intents to import from discord.js.

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

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

const token = 'mytoken'
client.on('ready', () => {
    console.log('bot logged in')
})

client.login(token)

When I run npm index.js I get this error: Intent error

Could my installation of the discord.js package be corrupt or maybe didnt install all the way/properly? This is my first time messing with the discord api so im not too familiar with it all yet. Reading documentation and still confused as to why mine wont import properly.

2 Answers2

0

I think you missed to import Discord on the first place. You are almost there. Just try this out,

import { Client, GatewayIntentBits } from 'discord.js';
const client = new Client({ intents: [GatewayIntentBits.Flags.Guilds, GatewayIntentBits.Flags.GuildMessages] });
const token = 'mytoken'
client.on('ready', () => {
    console.log('bot logged in')
})

client.login(token)
Atul Singh
  • 175
  • 1
  • 14
  • I tried this fix a couple times, however, VScode prompts me that im importing discord.js twice so I initially didnt put it in. But when re added, it still stops at the 'Intents' object import field when trying to run. – grantyskrt Aug 09 '22 at 21:32
  • ```Import { Client, Intents } from 'discord.js' ^^^^^ SyntaxError: Named export 'Intents' not found. The requested module 'discord.js' is a CommonJS module, which may not support all module.exports as named exports.``` – grantyskrt Aug 09 '22 at 21:34
  • I am also trying to stick with ES6 syntax by using the 'import from' method instead of 'require()'. – grantyskrt Aug 09 '22 at 21:38
  • Try the updated answer – Atul Singh Aug 09 '22 at 21:40
  • Still no luck sadly. – grantyskrt Aug 09 '22 at 21:42
  • Try the updated answer. It should be working. – Atul Singh Aug 09 '22 at 21:58
  • v14 got rid of 'Intents' and changed to 'GatewayIntentBits'. – grantyskrt Aug 09 '22 at 22:01
  • OP, if you want to get the ES6 export/import with CommonJS you could use TypeScript and in the tsconfig.json file set module to CommonJS and target to ES6 (or ESNext which i use) – Piecuu Aug 12 '22 at 09:32
0

Apparently in v14 of Discord.js Intents was changed to GatewayIntentBits: https://discordjs.guide/additional-info/changes-in-v14.html#enum-values

// import Discord from 'discord.js'
import { Client, GatewayIntentBits } from 'discord.js'
// const Discord = require("discord.js");
// const { Client, GatewayIntentBits } = require("discord.js")

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

const token = 'mytoken'
client.on('ready', () => {
    console.log('bot logged in')
})
client.login(token)