So I want to create a bot discord for fun in javascript using discord.js. I write 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 have this message :
/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 ?
CodePudding user response:
To run a bot, you need to have a token, and you are giving to client.login()
literaly "token",
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
- 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);