Code not working

Question: I tried removing that comma in position 19 but seems like the code is buggy, can anyone help me?

Repl link: https://replit.com/@flowerbunny212/Bot-Project#index.js

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Project is running!");

  app.get("/", (req, res) => {
  res.send("Hello world");

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


    
client.on("message"), message => {
  if(message.content === "ping") {
    message.channel.send("pong");
  }
});

client.login(process.env.token);

Just so you know, you have one file named index.js and the other index .js (with the space). Make sure you are adding your changes to the index.js file (without a space) because that is the one that Replit runs by default.

Also, you need to update the following from line 15:

client.on("message"), function(message) {
  if(message.content === "ping") {
    message.channel.send("pong");
  }
};
4 Likes

I did that and then It said the same comma is missing but now fixed, afterwards it now says unexpected end of input on line 21

The callback is a parameter, so it should be inside ().

Try

client.on("message", function(message) {
  if(message.content === "ping") {
    message.channel.send("pong");
  }
});

Or if you want arrow functions

client.on("message", (message) => {
  if(message.content === "ping") {
    message.channel.send("pong");
  }
});
1 Like

Parentheses here are not needed…

True, but I always add () in arrow functions.

3 Likes

Discord.js does not use “message” anymore. You should use “messageCreate”.

client.on("messageCreate", message => {
  if(message.content === "ping") {
    message.channel.send("pong");
  }
});

Also, change new Discord.client() to new Discord.Client() . Javascript is case sensitive.

Another thing is that client.login(process.env.token); is used when you have a .env file. Replit uses Secrets so you better change this too.

2 Likes

you can still use process.env.<SECRET_KEY>, but I prefer to have a secret with key DISCORD_TOKEN which means that I do just client.login(); with no arguments.


Your error would be that you haven’t closed the app.listen callback properly. In future please paste in the relevant errors in a code block. But I would recommend going without express, since the webserver is just for keep-alive and you could replace it (lines 2-7) with

require("http")
  .createServer((_, res) => res.writeHead(204).end())
  .listen(80);

For the next issue you’ll get: see

2 Likes

I ended up coding in the similar way before seeing all this on visual studio code, bot is ready but now it keeps going offline, gonna try and add that code here too