Problems with starting up the bot

This can seem a little bit strange, but I have a problem with the startup of my bot. Sometimes, It just wont start for no reason, I don't know why. I tried to reduce the launch time, but it's still too long. (with other code that i tried, its not that long). I'm using nodemon, and even without, i still need to start 4 to 5 times my index.js in order to start the bot correctly. I'll send my code below
13 Replies
d.js toolkit
d.js toolkit8mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Niwa
NiwaOP8mo ago
"use strict";

import { Client, Collection } from "discord.js";
import { readdirSync } from 'node:fs';
import settings from "../../app.settings.js";
import Logger from "./Logger.js";
import { REST, Routes } from "discord.js";


class Madi extends Client {
constructor(client, token ){
super(client);
this.commands = new Collection()
this.events = new Collection()
this.settings = settings;
this.token = token;
this.logger = Logger;
this.id = "1240374752183586826"
this.guild = "1001197214766477462"
this.regex = new RegExp("<@!?1240374752183586826>");
}

async init(){
await this.login(this.options.token);

await this.loadCommands()
await this.loadEvents()

return this;
}

async loadCommands(){
console.log(this.id)
const commands = [];
const rest = new REST().setToken(this.options.token);
const cmdList = readdirSync("./src/commands/").filter((file) => file.endsWith(".js"))
for(const cmd of cmdList){
const cmdName = cmd.split(".js")[0]
const CommandInstance = (await import(`./../commands/${cmd}`)).default
const Command = new CommandInstance(this, cmdName)
if ('data' in Command && 'run' in Command) {
commands.push(Command.data.toJSON());
} else {
console.log(`[WARNING] The command ${cmdName} is missing a required "data" or "execute" property.`);
}
this.commands.set(cmdName, Command)

}

try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(this.id, this.guild),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}

}

async loadEvents(){
const eventList = readdirSync("./src/events/").filter((file) => file.endsWith(".js"))
for(const event of eventList){
const eventName = event.split(".js")[0]
const EventInstance = (await import(`./../events/${event}`)).default
const Event = new EventInstance(this)
this.events.set(eventName, Event)
if (Event.once){
this.once(eventName, (...args) => Event.run(...args))
} else {
this.on(eventName, (...args) => Event.run(...args))
}
}
}
}

export { Madi }
"use strict";

import { Client, Collection } from "discord.js";
import { readdirSync } from 'node:fs';
import settings from "../../app.settings.js";
import Logger from "./Logger.js";
import { REST, Routes } from "discord.js";


class Madi extends Client {
constructor(client, token ){
super(client);
this.commands = new Collection()
this.events = new Collection()
this.settings = settings;
this.token = token;
this.logger = Logger;
this.id = "1240374752183586826"
this.guild = "1001197214766477462"
this.regex = new RegExp("<@!?1240374752183586826>");
}

async init(){
await this.login(this.options.token);

await this.loadCommands()
await this.loadEvents()

return this;
}

async loadCommands(){
console.log(this.id)
const commands = [];
const rest = new REST().setToken(this.options.token);
const cmdList = readdirSync("./src/commands/").filter((file) => file.endsWith(".js"))
for(const cmd of cmdList){
const cmdName = cmd.split(".js")[0]
const CommandInstance = (await import(`./../commands/${cmd}`)).default
const Command = new CommandInstance(this, cmdName)
if ('data' in Command && 'run' in Command) {
commands.push(Command.data.toJSON());
} else {
console.log(`[WARNING] The command ${cmdName} is missing a required "data" or "execute" property.`);
}
this.commands.set(cmdName, Command)

}

try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(this.id, this.guild),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}

}

async loadEvents(){
const eventList = readdirSync("./src/events/").filter((file) => file.endsWith(".js"))
for(const event of eventList){
const eventName = event.split(".js")[0]
const EventInstance = (await import(`./../events/${event}`)).default
const Event = new EventInstance(this)
this.events.set(eventName, Event)
if (Event.once){
this.once(eventName, (...args) => Event.run(...args))
} else {
this.on(eventName, (...args) => Event.run(...args))
}
}
}
}

export { Madi }
yeah theres still commentary tho, i was testing slash commands (im not used to) if u need more info u can ping me
Niwa
NiwaOP8mo ago
watch how many times i need to restart
No description
FeroxNotMonday
FeroxNotMonday8mo ago
this.options.token? ClientOptions doesn't have token property
Niwa
NiwaOP8mo ago
if i log that, it returns my token this.token returns nothing i was kinda confused too If i edit them, i dont need to deploy them again? Alright, so what shall I do, it's better to check if the name/description doesnt change or to make a command to just deploy them?
NyR
NyR8mo ago
No, that's a roundabout way of doing it, you should have seperate script for deploying command and when you change/add something to the command structure, then run the script
Niwa
NiwaOP8mo ago
oh i see that would prevent the loading time?
NyR
NyR8mo ago
Possibly
Niwa
NiwaOP8mo ago
Its still doing the same however its more faster that'll be enough i guess so, for deploying command i just make a deploy.js for exemple that i start when i want to deploy?
Harnes
Harnes8mo ago
The best way is just checking for a flag (like --deploy-commands) instead of doing another file because (for me) its to much work to every time changing command run deploy script and after that bot Or im just too lazy
Niwa
NiwaOP8mo ago
like node index.js --deploy-commands?
Harnes
Harnes8mo ago
yeah then in your index file check for that flag If this flag is there, deploy If not, do nothing/ignore If you want to read more of it You can use this
Niwa
NiwaOP8mo ago
thank you
Want results from more Discord servers?
Add your server