Servaloop
Servaloop

Reputation: 21

Discord Bug Error Invalid Token in discord.js

So I want to create a bot discord for fun in JavaScript using discord.js.

I wrote this code:

const Discord = require("discord.js")
const client = new Discord.client()

client.once('ready',() => {
  console.log("Ready !");

});

client.login('token');

But when I'm trying to run it, I get this error:

/home/runner/MelodicPlainApplicationprogrammer/node_modules/discord.js/src/rest/RESTManager.js:32 const token = this.client.token ?? this.client.accessToken; ^ SyntaxError: Unexpected token '?'

This is on repl.it, and when I'm in VSCode, it works.

Why?

Upvotes: 0

Views: 2428

Answers (3)

Nahh
Nahh

Reputation: 86

Try upgrading your node.js version (go to shell, next to console):

$ npm install node@16

Or you can downgrade your discord.js version to v12.5.3 in your json file, because v12 has way more tutorials than v13 at the moment.

The choice is upto you, you can upgrade your node.js version or downgrade your discord.js version

Upvotes: 0

Dregg
Dregg

Reputation: 673

  1. You are using Discord.js v13 and Discord.js13 requires node.js v16 to work. Replit.com uses node.js v12 by default so you will need to set up node16 manually.

First, create a node.js repl enter image description here

Next, run npm install node@16 in shell enter image description here

After that you need to create a file called .replit

enter image description here

Inside the .replit file, add run = "npx node index.js". If your main file has a different name change index.js to your main file's name.

Now when you click run, replit uses node.js v16 instead of v12

  1. Now for the problems with your code:
const Discord = require("discord.js")
const client = new Discord.client()

client.once('ready',() => {
  console.log("Ready !");

});

client.login('token');

In the second line, const client = new Discord.client(), the c in Discord.client has to be capital, so write const client = new Discord.Client()

Now you need to add gateway intents so that your bot receives events. This is required in discord.js13 (Here's a list of gateway intents)

So change const client = new Discord.Client()

to const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] }) (You can add more intents if you want, these are just the basic ones)

You will also need to get your bot token and replace "token" in client.login('token'); with your bot token. Check this to see how to get your token.

Final code:

const Discord = require("discord.js")
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })

client.once('ready',() => {
  console.log("Ready !");

});

client.login(enter your token here);

Upvotes: 3

Eldar B.
Eldar B.

Reputation: 1347

Most likely repl.it is running Node 12 or earlier. The error is saying that the operator ?? is a syntax error, meaning that it does not recognize the nullish coalescing operator. It is supported from Node 14.0.0 and later.

Upvotes: 0

Related Questions