My slash commands aren't registering in my discord server. Client.on() doesn't seem to run.

Github: https://github.com/Niimaii/Harmony I am putting the repo here to make things easier. I put several console.log() to check how things are. The bot does login, it seems to register all the commands. However it doesn't show up when I try to use the slash command in the server. I am at a loss and spent wayyyyy too much time trying to figure this out, there is probably a simple explanation, I just can't figure it out. The only hint I have is that the only console.log() that doesn't show up are the one in this section:
client.on(Events.InteractionCreate, (interaction) => {
console.log('Client.on ran');

if (!interaction.isChatInputCommand()) return;

console.log(`Command received: ${interaction.commandName}`);

let command = client.commands.get(interaction.commandName);

try {
if (interaction.replied) return;
command.execute(interaction);
} catch (error) {
console.error(error);
}
});
client.on(Events.InteractionCreate, (interaction) => {
console.log('Client.on ran');

if (!interaction.isChatInputCommand()) return;

console.log(`Command received: ${interaction.commandName}`);

let command = client.commands.get(interaction.commandName);

try {
if (interaction.replied) return;
command.execute(interaction);
} catch (error) {
console.error(error);
}
});
So it seems that client.on() isn't running at all, I am not sure. Please help! ahahahaha
GitHub
GitHub - Niimaii/Harmony
Contribute to Niimaii/Harmony development by creating an account on GitHub.
7 Replies
d.js toolkit
d.js toolkit4mo 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!
Anarchy
AnarchyOP4mo ago
I am sure there is something wrong with it, but it doesn't seem to error when I run it
No description
Anarchy
AnarchyOP4mo ago
I am a little stupid and new to discord bots. I tried not calling anything from deploy-commands in index, but nothing from the deploy-commands would run (even the console.log()). So I was desperate and thought I needed to run it in index.js? tbh I am pretty lost on what to do. None of this code from deploy-commands is running including the console.logs() I have there (i updated it to what I had it originally before deciding to call it in index):
import fs from 'node:fs';
import path from 'node:path';
import { REST } from '@discordjs/rest';
import { Routes } from 'discord.js';
import { DISCORD_TOKEN, CLIENT_ID, GUILD_ID } from '../config.js';

console.log('deploy-commands ran');
function getFiles(dir) {
const files = fs.readdirSync(dir, {
withFileTypes: true,
});

let commandFiles = [];

// Loop through all the files and folders in the `command` folder
for (const file of files) {
// If a folder is being looped over, run `getFiles()` recursively so that all the files in that folder is accounted for
if (file.isDirectory()) {
commandFiles = [...commandFiles, ...getFiles(`${dir}/${file.name}`)];
}
// If what is being looped over a file that ends with .js, then add the file path to `commandFiles`
else if (file.name.endsWith('.js')) {
commandFiles.push(`${dir}/${file.name}`);
}
}

return commandFiles;
}

async function loadCommands() {
let commands = [];
const commandFiles = getFiles('./src/commands');

for (const file of commandFiles) {
const command = await import(path.resolve(file));
// Push all the commands within "data" in each file
commands.push(command.default.data.toJSON());
}

return commands;
}

(async () => {
const commands = await loadCommands();
const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);

// Run all the commands in our specific server
rest
.put(
Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID, { body: commands })
)
.then(() =>
console.log('Successfully registered the application commands!')
)
.catch(console.error);
})();
import fs from 'node:fs';
import path from 'node:path';
import { REST } from '@discordjs/rest';
import { Routes } from 'discord.js';
import { DISCORD_TOKEN, CLIENT_ID, GUILD_ID } from '../config.js';

console.log('deploy-commands ran');
function getFiles(dir) {
const files = fs.readdirSync(dir, {
withFileTypes: true,
});

let commandFiles = [];

// Loop through all the files and folders in the `command` folder
for (const file of files) {
// If a folder is being looped over, run `getFiles()` recursively so that all the files in that folder is accounted for
if (file.isDirectory()) {
commandFiles = [...commandFiles, ...getFiles(`${dir}/${file.name}`)];
}
// If what is being looped over a file that ends with .js, then add the file path to `commandFiles`
else if (file.name.endsWith('.js')) {
commandFiles.push(`${dir}/${file.name}`);
}
}

return commandFiles;
}

async function loadCommands() {
let commands = [];
const commandFiles = getFiles('./src/commands');

for (const file of commandFiles) {
const command = await import(path.resolve(file));
// Push all the commands within "data" in each file
commands.push(command.default.data.toJSON());
}

return commands;
}

(async () => {
const commands = await loadCommands();
const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);

// Run all the commands in our specific server
rest
.put(
Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID, { body: commands })
)
.then(() =>
console.log('Successfully registered the application commands!')
)
.catch(console.error);
})();
Oh wow, I didn't even catch that. Thank you! I updated the code to this:
(async () => {
const commands = await loadCommands();
const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);

// Run all the commands in our specific server
rest
.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), {
body: commands,
})
.then(() =>
console.log('Successfully registered the application commands!')
)
.catch(console.error);
})();
(async () => {
const commands = await loadCommands();
const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);

// Run all the commands in our specific server
rest
.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), {
body: commands,
})
.then(() =>
console.log('Successfully registered the application commands!')
)
.catch(console.error);
})();
The deploy-commands.js file doesn't seem to run still. None of the console.log() are running. I tried updating my package.json script to include it when I run, but nothing still:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon ./src/deploy-commands.js && nodemon ./src/index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon ./src/deploy-commands.js && nodemon ./src/index.js"
},
So commands are now registered! But only when I manually run the script; the deploy-commands.js script is still not running on it's own and the bot does not reply with 'pong' or anything when I run the slash command.
treble/luna
treble/luna4mo ago
that is intended, hence why its a separate script You still have to run your actual bot itself
Anarchy
AnarchyOP4mo ago
The bot is being ran though I had to manually run deploy-commands so the slash commands would register. Are you saying that it's intended to be like that?
treble/luna
treble/luna4mo ago
yes thats why its a separate file to avoid running it every time your bots starts
Anarchy
AnarchyOP4mo ago
Ohhhhhh Interesting, I didn't realize that's how it worked.
Want results from more Discord servers?
Add your server