How would I fix this code?

Read the text document for it
143 Replies
d.js toolkit
d.js toolkit6d 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!
TheActualGamer1YT
I cant send my version as im using a host that doesnt allow it it claims that there is no command as "verify" or any other command on either bot when there is
d.js docs
d.js docs6d ago
To share long code snippets, use a service like gist, sourcebin, pastebin, or similar instead of posting them as large code blocks or files.
treble/luna
treble/luna6d ago
why do you have 2 client instances in there Thats prone to cause issues
TheActualGamer1YT
We are hosting 2 different bots.
treble/luna
treble/luna6d ago
Have a separate project per bot Not cram them in 1 single file
Samtino
Samtino6d ago
Also show the entire error message, not just the description of it
TheActualGamer1YT
Then its more time I would need to watch ads for and more craziness for me One minute
treble/luna
treble/luna6d ago
Then get a proper host Not a shady free one, thats just gonna cause more issues
Samtino
Samtino6d ago
But I love giving my discord bot token to a hosting company that is farming bot tokens to raid/spam millions of users simultaneously
TheActualGamer1YT
erm
Samtino
Samtino6d ago
It's a risk you run with a free host. If you're not paying them, then you're not a customer. You're a product. So your code (most importantly, your token) shouldn't be thought of as secure. They might be scraping and selling bot tokens to make money off their free "users"
TheActualGamer1YT
and i refuse to pay for bots that will only be used in a few servers
TheActualGamer1YT
but when i go to the command file for that command
TheActualGamer1YT
..
TheActualGamer1YT
.
treble/luna
treble/luna6d ago
Be patient. No one is obligated to help you.
35k._a
35k._a6d ago
what is the issue you are having exactly?
TheActualGamer1YT
Commands are not working with 2 bots in 1 index.
35k._a
35k._a6d ago
you're using the same commands for both bots?
TheActualGamer1YT
No im using 2 different folders for commands bc they are for 2 different servers
35k._a
35k._a6d ago
how about the events?
TheActualGamer1YT
The events work the commands dont the bots start and register the commands though
treble/luna
treble/luna6d ago
you shouldnt register your commands every time you start your bot though. And registering is separate. Log to see if they actually get added to your commands collection
TheActualGamer1YT
How could i proceed with this?
35k._a
35k._a6d ago
to log?
treble/luna
treble/luna6d ago
your command registering should be separate files which you only run when you add or edit commands to debug your commands setting, thats basic js and should be common knowledge
TheActualGamer1YT
They are different files
treble/luna
treble/luna6d ago
indeed they are, command registering is meant to be separate and has nothing to do with handling or setting it into client.commands which is where your issue is
TheActualGamer1YT
oOk so should i log to see if they are getting detected?
treble/luna
treble/luna6d ago
yes
TheActualGamer1YT
Ok! Let me do that rq.
Samtino
Samtino6d ago
Looks like you're still updating your commands every restart Srarted refreshing 12 application (/) commands. That should be in a completely different launch script like npm run deploy rather than npm run start
KeepChatHappyOwner
the issue is that my host doesnt allow me to run npm.
TheActualGamer1YT
SourceBin
deploy-commands.js
Instantly share your code with the world.
SourceBin
deploy-commands-winter.js
Instantly share your code with the world.
Samtino
Samtino6d ago
Just run the script manually on your own computer then push the update to the bot. It doesn't matter who/where the "deploy" script is run, it just has to be the same commands and the same token. As what you're doing is sending a list of commands to Discord for them to populate in the app
TheActualGamer1YT
ok so if i do that this should be fixed?
Samtino
Samtino6d ago
No. Because as Vinny said, your problem is with your caching of commands to client.commands or with your handling of interactionCreate events This is just to not get blocked from Discord for API spam
TheActualGamer1YT
so if I’m understanding this correctly it’s something with my command handler?
35k._a
35k._a5d ago
yes, your issue is located either in the function where you retrieve the files or in your interactionCreate event
SHIFTED TO @xpd8
:SA_pepemusic:
35k._a
35k._a5d ago
what Samtino told you to do has nothing to do with your actual issue, it was just a concern for you to avoid getting blocked from the Discord API because if you register automatically your slash commands on every startup you will end up banned from registering them again for 24 hours if im not wrong also, your handleInteraction function is async and you are not implementing await when you call it on both interactionCreate events for both clients
TheActualGamer1YT
So that might be my problem?
35k._a
35k._a5d ago
maybe yes try first by adding await in front of handleInteraction on both interactionCreate events
TheActualGamer1YT
I’ll try that when I get a chance.
TheActualGamer1YT
https://sourceb.in/8mM6yGcBr5 - still printing the exact same thing
SourceBin
index.js
new
35k._a
35k._a5d ago
what are you printing?
TheActualGamer1YT
its printing the following and not responding: "No command matching (INSERT COMMAND NAME HERE) was found on (INSERT BOT NAME HERE)
35k._a
35k._a5d ago
in that case the issue is in your loadCommands function
async function loadCommands(client, folders, botType) {}
//^^^^^ remove this
async function loadCommands(client, folders, botType) {}
//^^^^^ remove this
@TheActualGamer1YT You're not running any promises in the function, so remove it
TheActualGamer1YT
same thing happens.
35k._a
35k._a5d ago
hmmm are you sure there is data and execute in your commands?
TheActualGamer1YT
yes one sec i could send you the command im runninf
35k._a
35k._a5d ago
yeah
TheActualGamer1YT
one minute let me send it over
35k._a
35k._a5d ago
alr
TheActualGamer1YT
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with my ping!'),
async execute(interaction) {
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true });
const roundtripLatency = sent.createdTimestamp - interaction.createdTimestamp;

interaction.editReply(`Pong! Roundtrip latency: ${roundtripLatency}ms.`);
},
};
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with my ping!'),
async execute(interaction) {
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true });
const roundtripLatency = sent.createdTimestamp - interaction.createdTimestamp;

interaction.editReply(`Pong! Roundtrip latency: ${roundtripLatency}ms.`);
},
};
35k._a
35k._a5d ago
does this actually print?
console.log(`[LAUNCH] Detected command '${command.data.name}' for ${botType}`);
console.log(`[LAUNCH] Detected command '${command.data.name}' for ${botType}`);
when you load the commands
TheActualGamer1YT
No but it does show up in discord
35k._a
35k._a5d ago
yeah can you show me your deply-commands.js?
TheActualGamer1YT
like it shows up in the discord servers but it never prints one moment
35k._a
35k._a5d ago
yeah the commands register but this
console.log(`[LAUNCH] Detected command '${command.data.name}' for ${botType}`);
console.log(`[LAUNCH] Detected command '${command.data.name}' for ${botType}`);
does not print?
TheActualGamer1YT
no
TheActualGamer1YT
SourceBin
deploy-commands.js
Instantly share your code with the world.
SourceBin
deploy-commands-winter.js
Instantly share your code with the world.
35k._a
35k._a5d ago
i think i see why do you have sub folders in your "commands" and "winter-commands" folders? like commands --> info --> commandName.js
TheActualGamer1YT
yes
35k._a
35k._a5d ago
okay so the issue you are having indeed is that you see the "commands" folder that you are finding with the loadCommands function?
TheActualGamer1YT
yes
35k._a
35k._a5d ago
you are directly looking for files that end with ".js" inside the "commands" main folder, but as you have sub-folders inside that "commands" folder then you can't find them like that
TheActualGamer1YT
ohhhhhhhhh
35k._a
35k._a5d ago
yeah you are looking for stuff that don't exist in the "commands" folder instead, you should look for folders inside the "commands" folder and for each of these folders inside the "commands" folder look for files that end with ".js"
TheActualGamer1YT
How would I do that
35k._a
35k._a5d ago
you create another foor loop for the retrieved sub-folders hold on
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
this is retrieving folders instead of files start by removing the .filter make it only fs.readdirSync(commandsPath) and uh obviously change the name of the constant to "commandFolders" instead of "comandFiles"
TheActualGamer1YT
SourceBin
index.js
new v2
35k._a
35k._a5d ago
change const file to const folder
35k._a
35k._a5d ago
remove the filter from "commandFolder"
TheActualGamer1YT
so it would be
fs.readdirSync(commandsPath)
fs.readdirSync(commandsPath)
35k._a
35k._a5d ago
yeah then you get the path to the obtained folder from const folder
TheActualGamer1YT
const filePath = path.join(commandFolders, file);
const command = require(filePath);

const filePath = path.join(commandFolders, file);
const command = require(filePath);

35k._a
35k._a5d ago
no
TheActualGamer1YT
would change to: const filePath = path.join(commandFolders, folder); const command = require(filePath); right
35k._a
35k._a5d ago
yes now
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
console.log(`[LAUNCH] Detected command '${command.data.name}' for ${botType}`); // Log detected command
} else {
console.log(`[WARNING] The command at ${filePath} for ${botType} is missing a required "data" or "execute" property.`);
}
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
console.log(`[LAUNCH] Detected command '${command.data.name}' for ${botType}`); // Log detected command
} else {
console.log(`[WARNING] The command at ${filePath} for ${botType} is missing a required "data" or "execute" property.`);
}
remove this whole block, but you'll need it for later
TheActualGamer1YT
k
35k._a
35k._a5d ago
then, below that, create a new for (...) loop that iterates the folder
TheActualGamer1YT
k
35k._a
35k._a5d ago
no actually @TheActualGamer1YT below that, add a constant where you use fs to read the folderPath and filtering it in order to only get files that end with ".js" then you create the for loop that iterates through the created constant my bad for the confusion
TheActualGamer1YT
its fine!
TheActualGamer1YT
https://sourceb.in/LYtvnPJuzE then make the for loop?
SourceBin
index.js
new v2
35k._a
35k._a5d ago
const files = commandFolders.filter(file => file.endsWith('.js'))
const files = commandFolders.filter(file => file.endsWith('.js'))
this should be:
const files = fs.readdirSync(folderPath).filter(file => file.endsWith('.js')
const files = fs.readdirSync(folderPath).filter(file => file.endsWith('.js')
instead then, yeah, you make the for loop
TheActualGamer1YT
what would the for loop check for
35k._a
35k._a5d ago
it would check for files inside of your files constant do you know how to do the for loop?
TheActualGamer1YT
i think
35k._a
35k._a5d ago
try and ill tell u if its correct
TheActualGamer1YT
for (files) {

}
for (files) {

}
35k._a
35k._a5d ago
for (const file of files) {

}
for (const file of files) {

}
instead, by that you are going through every file of files inside the for loop, you put this don't forget to define "filePath" before
TheActualGamer1YT
oh ok
35k._a
35k._a5d ago
please ping me when you are done because im not on the app
TheActualGamer1YT
k
35k._a
35k._a5d ago
you need to create a constant called "filePath" inside the for loop i gave you and put it inside require() to do so you do the same thing as you did for "folderPath", using the path.join method
TheActualGamer1YT
SourceBin
index.js
new v2
35k._a
35k._a5d ago
hold on
TheActualGamer1YT
k
35k._a
35k._a5d ago
const filePath = path.join(folderPath, filePath);
const filePath = path.join(folderPath, filePath);
replace the filePath inside the parenthesis with file also, ive noticed something else
TheActualGamer1YT
k
35k._a
35k._a5d ago
const folderPath = path.join(commandFolders, folder);
const folderPath = path.join(commandFolders, folder);
replace commandFolders with commandsPath
No description
35k._a
35k._a5d ago
commandFolders is an array, and you want to put strings inside of the join method after doing all that, you're good to go
TheActualGamer1YT
SourceBin
index.js
new v2
35k._a
35k._a5d ago
you need to do this lastly replace the commandFolders inside the parenthesis of the folderPath constant with commandsPath line 28
35k._a
35k._a5d ago
you're good to go now try now
TheActualGamer1YT
ok!
35k._a
35k._a5d ago
lmk how it goes
TheActualGamer1YT
alright Worked as I can see! Thanks a lot
35k._a
35k._a5d ago
no problem uh just one thing
TheActualGamer1YT
ye
35k._a
35k._a5d ago
i hardly suggest you to get documented about these for (...) loops and more topics about javascript before doing anything else so you know the basics of the language, you may view these links covering these loops: - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration - https://www.youtube.com/watch?v=c5a4sNshBA4 (for ... in, for ...of loops, not covered on the video below) - 2 min video - https://www.youtube.com/watch?v=ZOQYIWLngSU - 5 min video oh no hold on i got confused w the links lol
TheActualGamer1YT
Alright! lol
35k._a
35k._a5d ago
i tell u that so you dont face these kind of issues again because your issue today was because due to these for loops
TheActualGamer1YT
oh ok
35k._a
35k._a5d ago
also check #resources for more documentation sites
TheActualGamer1YT
Alright thanks a lot!
35k._a
35k._a5d ago
np
TheActualGamer1YT
im having issues again this time with it not printing anything out if something went wrong: https://sourceb.in/JnyWru0Z27 running any command is not working, here are the logs as well:
[LAUNCH] Loaded 'adduser' for pepe
[LAUNCH] Loaded 'claim' for pepe
[LAUNCH] Loaded 'close' for pepe
[LAUNCH] Loaded 'openfor' for pepe
[LAUNCH] Loaded 'reopen' for pepe
[LAUNCH] Loaded 'open' for pepe
[LAUNCH] Loaded 'dm' for pepe
[LAUNCH] Loaded 'help' for pepe
[LAUNCH] Loaded 'ping' for pepe
[LAUNCH] Loaded 'dm' for winter
[LAUNCH] Loaded 'forceunverify' for winter
[LAUNCH] Loaded 'forceverify' for winter
[LAUNCH] Loaded 'help' for winter
[LAUNCH] Loaded 'hrrank' for winter
[LAUNCH] Loaded 'ping' for winter
[LAUNCH] Loaded 'rank' for winter
[LAUNCH] Loaded 'sync-rank' for winter
[LAUNCH] Loaded 'winterverify' for winter
API running at http://localhost:20226
Connected to the SQLite database.
Ready! Logged in as Pepe#0561
Ready! Logged in as Winter The Dolphin#2269
[LAUNCH] Loaded 'adduser' for pepe
[LAUNCH] Loaded 'claim' for pepe
[LAUNCH] Loaded 'close' for pepe
[LAUNCH] Loaded 'openfor' for pepe
[LAUNCH] Loaded 'reopen' for pepe
[LAUNCH] Loaded 'open' for pepe
[LAUNCH] Loaded 'dm' for pepe
[LAUNCH] Loaded 'help' for pepe
[LAUNCH] Loaded 'ping' for pepe
[LAUNCH] Loaded 'dm' for winter
[LAUNCH] Loaded 'forceunverify' for winter
[LAUNCH] Loaded 'forceverify' for winter
[LAUNCH] Loaded 'help' for winter
[LAUNCH] Loaded 'hrrank' for winter
[LAUNCH] Loaded 'ping' for winter
[LAUNCH] Loaded 'rank' for winter
[LAUNCH] Loaded 'sync-rank' for winter
[LAUNCH] Loaded 'winterverify' for winter
API running at http://localhost:20226
Connected to the SQLite database.
Ready! Logged in as Pepe#0561
Ready! Logged in as Winter The Dolphin#2269
Unknown User
Unknown User5h ago
Message Not Public
Sign In & Join Server To View
TheActualGamer1YT
registering them in the deploy-command files i think
Unknown User
Unknown User5h ago
Message Not Public
Sign In & Join Server To View
TheActualGamer1YT
its worked until i added the button handlers
Unknown User
Unknown User5h ago
Message Not Public
Sign In & Join Server To View
TheActualGamer1YT
it only runs that if i manually restart it (it doesnt go down and up it stays up unless i restart it, the host goes down, or i don't have enough coins on my host to pay.) ^
Unknown User
Unknown User5h ago
Message Not Public
Sign In & Join Server To View
TheActualGamer1YT
like what, ive tried the:
process.on('unhandledRejection', error => {
};
process.on('unhandledRejection', error => {
};
that didn't tell me anything
Unknown User
Unknown User5h ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs5h ago
If you aren't getting any errors, try to place console.log checkpoints throughout your code to find out where execution stops. - Once you do, log relevant values and if-conditions - More sophisticated debugging methods are breakpoints and runtime inspections: learn more
TheActualGamer1YT
after i looked closer, i found that i forgot the interactioncreated event lol
Unknown User
Unknown User5h ago
Message Not Public
Sign In & Join Server To View
TheActualGamer1YT
its fine! i didn't either lol

Did you find this page helpful?