Reputation: 21
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
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
Reputation: 673
Next, run npm install node@16
in shell
After that you need to create a file called .replit
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
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
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