I'm using node.js + discord.js on vs code, this is also my first time coding, so excuse my ugly formatting etc etc. Everything is up to date and was running perfectly fine until it just... didn't. It's driving me nuts and none of the solutions that worked for others has worked for me. I was following this tutorial but deviated from it to personalize my bot.
Index.js
require('dotenv').config();
const {
Client,
Message,
User,
ClientUser,
GuildMember,
ClientApplication,
Guild
} = require('discord.js');
const client = new Client({
intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MESSAGE_TYPING", "GUILD_PRESENCES"]
});
const prefix = '!';
client.on('ready', () => {
console.log('SHOWTIME!!');
client.user.setStatus('idle');
});
client.on("message", (message) => {// EventEmitter
if (message.content == "!ping") { // Check if message is "!ping"
message.channel.send("Pinging ...") // Placeholder for pinging ...
.then((msg) => { // Resolve promise
msg.edit("Pong! " + (Date.now() - message.createdTimestamp)); // Edits message with current timestamp minus timestamp of message
});
}
});
client.on("message", (message) => {
if (message.author.bot) return; {
if (message.content == "!help") {
message.channel.send("What can I help you with?")
.then((message) => {
message.react("1️⃣").then(_e => {
message.react("2️⃣").then(_e => {
message.react("⏹");
});
});
});
}
}
client.on("message", (message) => {
if (message.content == "!ns") {
(message.react)("🔜");
message.reply('The next session is scheduled for ...!');
}
});
client.on("message", async (message) => {
if (message.author.bot) return; {
if (message.content == "!uptime") {
(message.react)("✨");
let days = Math.floor(client.uptime / 86400000);
let hours = Math.floor(client.uptime / 3600000) % 24;
let minutes = Math.floor(client.uptime / 60000) % 60;
let seconds = Math.floor(client.uptime / 1000) % 60;
message.channel.send((`__I have been online for...__\n${days}d ${hours}h ${minutes}m ${seconds}s!`));
}
}
});
client.login(process.env.token_renren);
});
package.JSON
{
"name": "renren_v2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./src/index.js",
"dev": "nodemon ./src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "^13.1.0",
"dotenv": "^10.0.0",
"express": "^4.17.1"
},
"devDependencies": {
"nodemon": "^2.0.12"
}
}
Message that keeps showing up in the terminal:
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./src/index.js`
[nodemon] clean exit - waiting for changes before restart
Specifically, Nodemon stopped working after I added this specific command:
client.on("message", (message) => {
if (message.author.bot) return; {
if (message.content == "!help") {
message.channel.send("Hm? What can I help you with?")
.then((message) => {
message.react("1️⃣").then(_e => {
message.react("2️⃣").then(_e => {
message.react("⏹");
});
I removed it after it first gave the error to me, but added it back to see if it was the problem. After adding it back, removing it would no longer make nodemon work. I've tried uninstalling, installing it as a project dev, clean installing node, etc and so far nothing has worked.
A clean exit
means your program finishes without any errors. Which is exactly what your program currently does. You define quite a few event listeners, but your bot itself is never started.
As @Zsolt pointed out, you currently log in your discord client inside a message handler. This means, the bot must receive a message before its startet. But since your bot is not logged in, this never happens.
You need to move your login call to the bottom of your script and then it should work:
client.login(process.env.token_renren)
You are defining multiple listeners for one event. While this works, you have no control over the order in which your listeners are called which can lead to unexpected behavior. As @Zsolt pointed out, you usually define one message
listener and then pass on the event to specific functions handling different messages.
You are also changing a lot of then
s which can make the code very hard to read (this is called callback hell). You can use async
functions to avoid this:
async function handleMessage(message) {
if (message.author.bot) return
if (message.content == "!help") {
let response = await message.channel.send("What can I help you with?")
await response.react("1️⃣")
await response.react("2️⃣")
await response.react("⏹")
}
}
client.on("message", handleMessage)
This makes the code much shallower, making it easier to spot things like the login
call inside a message handler.
Additionally, most IDEs offer automatic code formatting these days. Unless you are working on a large repository which didn't use this feature, I would highly recommend running that once in a while. I have "format on save" on for my code and got used to it, but that's a personal preference (although it does save time and some employers might even require it). That makes it very easy to spot wrong indentations and weird formating like your if (message.author.bot) return; {
.