Slash Command Error

node:events:492 throw er; // Unhandled 'error' event ^ TypeError: interaction.isChatInputCommand is not a function at Object.execute (C:\Users\LoneDeveloper\Documents\Bots\events\interactionCreate.js:13:26) at Client.<anonymous> (C:\Users\LoneDeveloper\Documents\Bots\index.js:24:50) at Client.emit (node:events:514:28) at InteractionCreateAction.handle (C:\Users\LoneDeveloper\Documents\Bots\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12) at module.exports [as INTERACTION_CREATE] (C:\Users\LoneDeveloper\Documents\Bots\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) at WebSocketManager.handlePacket (C:\Users\LoneDeveloper\Documents\Bots\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31) at WebSocketManager.<anonymous> (C:\Users\LoneDeveloper\Documents\Bots\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12) at WebSocketManager.emit (C:\Users\LoneDeveloper\Documents\Bots\node _modules\@vladfrangu\async_event_emitter\dist\index.cjs:282:31) at WebSocketShard.<anonymous> (C:\Users\LoneDeveloper\Documents\Bots\node_modules\@discordjs\ws\dist\index.js:1173:51) at WebSocketShard.emit (C:\Users\LoneDeveloper\Documents\Bots\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:282:31) Emitted 'error' event on Client instance at: at emitUnhandledRejectionOrErr (node:events:397:10) at process.processTicksAndRejections (node:internal/process/task_queues:84:21) Node.js v20.5.1
13 Replies
driedfxrn
driedfxrnOP8mo ago
const {Client, Collection, GatewayIntentBits} = require('discord.js')
const fs = require('fs')
const path = require('node:path');
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
});

client.commands = getCommands('./commands');

module.exports = {
name: 'interactionCreate',
async execute(interaction) {
if (!interaction.isChatInputCommand()) return;

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

try {
if (interaction.replied) return;
command.execute(interaction);
} catch (error) {
console.error(error);
interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}}
};

// Functions
function getCommands(dir) {
let commands = new Collection();
const commandFiles = getFiles(dir);

for (const commandFile of commandFiles) {
const command = require("."+commandFile);
commands.set(command.data.toJSON().name, command);
}
return commands;
}

function getFiles(dir) {
const files = fs.readdirSync(dir, {
withFileTypes: true
});

let commandFiles = [];

for (const file of files) {
if (file.isDirectory()) {
commandFiles = [
...commandFiles,
...getFiles(`${dir}/${file.name}`)
];
} else if (file.name.endsWith(".js")) {
commandFiles.push(`${dir}/${file.name}`);
}
}
return commandFiles;
}
const {Client, Collection, GatewayIntentBits} = require('discord.js')
const fs = require('fs')
const path = require('node:path');
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
});

client.commands = getCommands('./commands');

module.exports = {
name: 'interactionCreate',
async execute(interaction) {
if (!interaction.isChatInputCommand()) return;

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

try {
if (interaction.replied) return;
command.execute(interaction);
} catch (error) {
console.error(error);
interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}}
};

// Functions
function getCommands(dir) {
let commands = new Collection();
const commandFiles = getFiles(dir);

for (const commandFile of commandFiles) {
const command = require("."+commandFile);
commands.set(command.data.toJSON().name, command);
}
return commands;
}

function getFiles(dir) {
const files = fs.readdirSync(dir, {
withFileTypes: true
});

let commandFiles = [];

for (const file of files) {
if (file.isDirectory()) {
commandFiles = [
...commandFiles,
...getFiles(`${dir}/${file.name}`)
];
} else if (file.name.endsWith(".js")) {
commandFiles.push(`${dir}/${file.name}`);
}
}
return commandFiles;
}
Favna
Favna8mo ago
Where do you actually bind the exported function to client.on('interactionCreate', ...) Also since you're in the server for sapphire, have you considered using sapphire instead of making your own framework?
driedfxrn
driedfxrnOP8mo ago
no bcuz in new
Favna
Favna8mo ago
Well you did join the server and you got the welcome screen that clearly told you the purpose of the server :wtfummm:
driedfxrn
driedfxrnOP8mo ago
oh wai no im new to the discord bot dev i plan on using sapphire after a bit of learning
Favna
Favna8mo ago
I guess but this way you're learning poor practises that you're going to have to unlearn later.
driedfxrn
driedfxrnOP8mo ago
do you have any recommendations then
Favna
Favna8mo ago
Well yeah just starting your bot with sapphire from the get go. Follow the guide on the website and/or generate a template with the CLI and go from there.
Spinel
Spinel8mo ago
The guide for Sapphire and its many utilities, plugins and related libraries can be found on our website
driedfxrn
driedfxrnOP8mo ago
ok this where i register the commands
const fs = require('node:fs')
const path = require('node:path')
const { REST } = require('@discordjs/rest')
const { Routes } = require('discord.js')
const { token, guildID, clientID} = require('discord.js')


function getFiles(dir){
const files = fr.readdirSync(dir, {
withFileypes: true
});

let commandFiles = {};

for(const file of files){
if(file.isDirectory()){
commandFiles = [
...commandFiles,
...getFiles(`${dir}/${file.name}`)
]
} else if(file.name.endsWith(".js")) {
commandFiles.push(`${dir}/${file.name}`)
}
}
return commandFiles;
}

let commands = [];
const commandFiles = getFiles('./commands')

for(const file of commandFiles){
const command = require(file)
commands.push(command.data.toJSON())
}

const rest = new REST({ version: '10'}).setToken(token)

rest.put(Routes.applicationGuildCommands(clientID, guildID), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error)
const fs = require('node:fs')
const path = require('node:path')
const { REST } = require('@discordjs/rest')
const { Routes } = require('discord.js')
const { token, guildID, clientID} = require('discord.js')


function getFiles(dir){
const files = fr.readdirSync(dir, {
withFileypes: true
});

let commandFiles = {};

for(const file of files){
if(file.isDirectory()){
commandFiles = [
...commandFiles,
...getFiles(`${dir}/${file.name}`)
]
} else if(file.name.endsWith(".js")) {
commandFiles.push(`${dir}/${file.name}`)
}
}
return commandFiles;
}

let commands = [];
const commandFiles = getFiles('./commands')

for(const file of commandFiles){
const command = require(file)
commands.push(command.data.toJSON())
}

const rest = new REST({ version: '10'}).setToken(token)

rest.put(Routes.applicationGuildCommands(clientID, guildID), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error)
Favna
Favna8mo ago
The problem with your code is that you call event.execute(client, ...args) but then implement it as execute(interaction). So it's not an interaction but a client in the implementation. Either you have to implement it as execute(client, interaction) or do what sapphire does and not pass client that way at all but use depdency injection. You have to be mindful of the order of parameters when programming.
Spinel
Spinel8mo ago
Before you make a Discord Bot, you should have a good understanding of JavaScript. This means you should have a basic understanding of the following topics: - Read and understand docs - Debug code - Syntax - NodeJS module system If you aren't sure that your understanding of JavaScript is truly good enough to make a bot, you should try to continue learning first. Here are good resources to learn both Javascript and NodeJS: - Codecademy: https://www.codecademy.com/learn/javascript - Udemy: https://www.udemy.com/javascript-essentials/ - Eloquent JavaScript, free book: http://eloquentjavascript.net/ - You-Dont-Know-JS: https://github.com/getify/You-Dont-Know-JS - JavaScript Garden: https://bonsaiden.github.io/JavaScript-Garden/ - JavaScript reference/docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference - Nodeschool: https://nodeschool.io/ - Pluralsight: https://www.codeschool.com/courses/real-time-web-with-node-js Before you ask a question, you should ask these yourself: 1. Is this question related to JavaScript, or the library I am using? - If it is the library you are using, go to the proper server. You would get better answers there. 2. Have I tried to google and/or check StackOverflow? - Double-check that you can't find anywhere that can lead you to a solution online. 3. Have I tried to look on MDN or the library documentation? - You should always check documentation to make sure you aren't missing any details. 4. Does my question make enough sense so that people can understand it, and do they understand what I am trying to accomplish? - If no, revise your question. Give as much detail as possible. Include any error or code output that can help us help you. 5. Am I aware of what I am doing, and not just mindlessly copying and pasting? - If you are just copying and pasting code from a guide, you are not going to be able to solve anything. Make sure you understand the code you are writing.
driedfxrn
driedfxrnOP8mo ago
oke i passed in client myself it wasn't in the guide or anything
Want results from more Discord servers?
Add your server