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
driedfxrn
driedfxrn49d 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
Favna49d 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
driedfxrn49d ago
no bcuz in new
Favna
Favna49d ago
Well you did join the server and you got the welcome screen that clearly told you the purpose of the server :wtfummm:
driedfxrn
driedfxrn49d ago
oh wai no im new to the discord bot dev i plan on using sapphire after a bit of learning
Favna
Favna49d ago
I guess but this way you're learning poor practises that you're going to have to unlearn later.
driedfxrn
driedfxrn49d ago
do you have any recommendations then
Favna
Favna49d 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
Spinel49d ago
The guide for Sapphire and its many utilities, plugins and related libraries can be found on our website
driedfxrn
driedfxrn49d 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
Favna49d 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
Spinel49d 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
driedfxrn49d ago
oke i passed in client myself it wasn't in the guide or anything
Want results from more Discord servers?
Add your server
More Posts
Error when clicking buttonerror: https://srcb.in/dIL4feMtz6 code: https://srcb.in/MGmSNTYXw6 I tried using Message Component Prevent commands from being ran on DMs and prevent bots from using commands by defaultdid Sapphire recently add this behavior stated in the title? I recently removed Preconditions that pButton doing nothing```const button = new ButtonBuilder() .setCustomId("COB") .setLabel("Custom InteractionOptionResolver pleasing TypeScriptI have an HTTP interactions app that utilizes `@discordjs/core/http-only`. Currently, I have my own JavascriptAlright guys, quick question if i am new to javascript (i want to learn typescript by the way.) and Any way to add a sort of "middleware" to all listenersIs there any way to have something like a middleware or precondition for all events? I want to delayAre my slashcommands guild based or global?When I use `registerApplicationCommands` (like shown in the picture) does that command get registereTwo Commands In One Filewhen i export chat input command and user context menu command in one file it reads the first exportQuestion regarding client.login()A bit of a curiosity question, I noticed that you can use `await client.login();` without providing Invalid Token ErrorHow do I fix this error: ```js const invalidToken = new DiscordjsError(ErrorCodes.TokenInvalid); @sapphire/type error```Error when loading '/home/matthew/projects/ts/waddlebot/src/commands/General/eval.ts': error: CanloggingIs there anyway I can log to console every time a command or a listener is ran?Random ErrorSo everything was working fine and all but then out of no where it's decided to go and throw this onDiscordAPIError[10062]: Unknown interactionSo it keep giving me unknow interaction, while im using deferReply normaly. Also when i try to log sScheduled Tasks with Common JSI am looking to do a task every 2 minutes, I want to use scheduled tasks but seems like all the examCannot find module issue though the module is exported.Please check the screenshot attached with this post.Apply Overwrites in a voicechannelHow do i apply overrides after a channel was created?User Install (The new update)Hello, I am attempting to make my JavaScript d.js Discord Bot work with the new User Install featureTranslated Subcommand does not register because a error``` 2024-03-22 19:16:49 - INFO - ApplicationCommandRegistries: Initializing... 2024-03-22 19:16:49 Interactionits just a dumb question , but i didnt manage to find it on the doc , i might have missed the part ,