Error on interactionCreate

const { Events, MessageFlags } = require('discord.js');

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

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

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
flags: MessageFlags.Ephemeral,
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
flags: MessageFlags.Ephemeral,
});
}
}
},
};
const { Events, MessageFlags } = require('discord.js');

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

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

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
flags: MessageFlags.Ephemeral,
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
flags: MessageFlags.Ephemeral,
});
}
}
},
};
This code keeps giving this error "TypeError: interaction.isChatInputCommand is not a function at Object.execute (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\events\interactionCreate.js:6:20) at Client.<anonymous> (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\index.js:47:48) at Client.emit (node:events:508:28) at InteractionCreateAction.handle (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\discord.js\src\client\actions\InteractionCreate.js:101:12) at module.exports [as INTERACTION_CREATE] (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) at WebSocketManager.handlePacket (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31) at WebSocketManager.<anonymous> (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\discord.js\src\client\websocket\WebSocketManager.js:236:12) at WebSocketManager.emit (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules@vladfrangu\async_event_emitter\dist\index.cjs:2504:31) at WebSocketShard.<anonymous> (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules@discordjs\ws\dist\index.js:1190:51) at WebSocketShard.emit (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules@vladfrangu\async_event_emitter\dist\index.cjs:2504:31) Emitted 'error' event on Client instance at: at emitUnhandledRejectionOrErr (node:events:391:10) at process.processTicksAndRejections (node:internal/process/task_queues:91:21) Node.js v24.11.1"
39 Replies
d.js toolkit
d.js toolkitโ€ข2d ago
Skull
SkullOPโ€ข2d ago
Ping me if you need me
sabrw
sabrwโ€ข2d ago
Can you log interaction object using console.log(interaction)? Write it before if statement so it can run before creating error
Skull
SkullOPโ€ข2d ago
Done
sabrw
sabrwโ€ข2d ago
Can you send what you got on console?
Skull
SkullOPโ€ข2d ago
Samtino
Samtinoโ€ข2d ago
Show the event loader where you're registering this event
sabrw
sabrwโ€ข2d ago
This doesnt seem correct, how do you handle events?
Samtino
Samtinoโ€ข2d ago
Should look something like this
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter((file) => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter((file) => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
This is an example from the guide
Skull
SkullOPโ€ข2d ago
In bot.js?
sabrw
sabrwโ€ข2d ago
Could be
Skull
SkullOPโ€ข2d ago
The main file
Samtino
Samtinoโ€ข2d ago
Because your async execute(interaction) has interaction of type Client Wherever it is. I don't know your file structure
Skull
SkullOPโ€ข2d ago
The main file?
sabrw
sabrwโ€ข2d ago
Yeah look for something like this Most of the time people put their event loader in main file, if you didn't create another file for event loader its probably there
Skull
SkullOPโ€ข2d ago
Thanks. Most likely is.
// Load events
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(f => f.toLowerCase().endsWith('.js'));

console.log(`๐Ÿ”Ž Loading ${eventFiles.length} event file(s) from ${eventsPath}`);
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
try {
const event = require(filePath);
if (!event || typeof event !== 'object') { console.warn(`${file}: export is not an object`); continue; }
if (!event.name) { console.warn(`${file}: missing "name"`); continue; }
if (typeof event.execute !== 'function') { console.warn(`${file}: missing "execute()"`); continue; }

if (event.once) {
client.once(event.name, (...args) => event.execute(client, ...args));
console.log(`Registered ONCE event: ${event.name} (${file})`);
} else {
client.on(event.name, (...args) => event.execute(client, ...args));
console.log(`Registered event: ${event.name} (${file})`);
}
} catch (err) {
console.error(`Failed to load ${file}: ${err.message}\n${err.stack}`);
}
}
// Load events
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(f => f.toLowerCase().endsWith('.js'));

console.log(`๐Ÿ”Ž Loading ${eventFiles.length} event file(s) from ${eventsPath}`);
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
try {
const event = require(filePath);
if (!event || typeof event !== 'object') { console.warn(`${file}: export is not an object`); continue; }
if (!event.name) { console.warn(`${file}: missing "name"`); continue; }
if (typeof event.execute !== 'function') { console.warn(`${file}: missing "execute()"`); continue; }

if (event.once) {
client.once(event.name, (...args) => event.execute(client, ...args));
console.log(`Registered ONCE event: ${event.name} (${file})`);
} else {
client.on(event.name, (...args) => event.execute(client, ...args));
console.log(`Registered event: ${event.name} (${file})`);
}
} catch (err) {
console.error(`Failed to load ${file}: ${err.message}\n${err.stack}`);
}
}
sabrw
sabrwโ€ข2d ago
So yeah I see why its not working
Samtino
Samtinoโ€ข2d ago
You see how you are running event.excute(client, ...args) there? That means you're passing the client object first and then the event args
Skull
SkullOPโ€ข2d ago
So remove โ€œclientโ€?
Samtino
Samtinoโ€ข2d ago
Really, you shouldn't pass that client at all, you shouldn't need it. The BaseInteraction class has the Client (and so do many other events)
sabrw
sabrwโ€ข2d ago
Exactly, you can edit your execute file into execute(client, interaction) so youll be fine if you don't want to edit your event loader
Skull
SkullOPโ€ข2d ago
How would i remove it?
Samtino
Samtinoโ€ข2d ago
That or add it before interaction in async execute(client, interaction) but I prefer just removing it
sabrw
sabrwโ€ข2d ago
You can already access Client object with interaction.client so yeah you don't need it
Skull
SkullOPโ€ข2d ago
How do i remove it.
Samtino
Samtinoโ€ข2d ago
^ Remove it from that
Skull
SkullOPโ€ข2d ago
So just keep args there?
Samtino
Samtinoโ€ข2d ago
It should be event.execute(...args) There's 2 of them so don't forget both
Skull
SkullOPโ€ข2d ago
Done. Im gonna try it again. It works. Thanks Guys!
sabrw
sabrwโ€ข2d ago
Remove client, part from your event loaders execute function so its gonna look like event.execute(...args)
Skull
SkullOPโ€ข2d ago
solved
Samtino
Samtinoโ€ข2d ago
Perfect. Now if you ever need the Client class. DJS attaches it to many objects so you can just do Object.client to get it. For example, interaction.client and message.client are 2 ways of getting it
Skull
SkullOPโ€ข2d ago
Thanks Sam Actually, another thing
MongoServerSelectionError: read ECONNRESET
at Topology.selectServer (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\sdam\topology.js:346:38)
at async Topology._connect (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\sdam\topology.js:219:28)
at async Topology.connect (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\sdam\topology.js:171:13)
at async topologyConnect (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\mongo_client.js:258:17)
at async MongoClient._connect (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\mongo_client.js:271:13)
at async MongoClient.connect (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\mongo_client.js:196:13)
at async connectDB (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\db\db.js:10:5)
at async run (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\index.js:9:14) {
errorLabelSet: Set(0) {},
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'ac-uam254z-shard-00-00.zlp5zf7.mongodb.net:27017' => [ServerDescription],
'ac-uam254z-shard-00-01.zlp5zf7.mongodb.net:27017' => [ServerDescription],
'ac-uam254z-shard-00-02.zlp5zf7.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-o9z7oe-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: 30
},
code: undefined,
[cause]: MongoNetworkError: read ECONNRESET
at TLSSocket.<anonymous> (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\cmap\connect.js:286:44)
at Object.onceWrapper (node:events:623:26)
at TLSSocket.emit (node:events:508:28)
at emitErrorNT (node:internal/streams/destroy:170:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at process.processTicksAndRejections (node:internal/process/task_queues:89:21) {
errorLabelSet: Set(1) { 'ResetPool' },
beforeHandshake: false,
[cause]: Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
}
}
MongoServerSelectionError: read ECONNRESET
at Topology.selectServer (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\sdam\topology.js:346:38)
at async Topology._connect (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\sdam\topology.js:219:28)
at async Topology.connect (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\sdam\topology.js:171:13)
at async topologyConnect (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\mongo_client.js:258:17)
at async MongoClient._connect (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\mongo_client.js:271:13)
at async MongoClient.connect (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\mongo_client.js:196:13)
at async connectDB (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\db\db.js:10:5)
at async run (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\index.js:9:14) {
errorLabelSet: Set(0) {},
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'ac-uam254z-shard-00-00.zlp5zf7.mongodb.net:27017' => [ServerDescription],
'ac-uam254z-shard-00-01.zlp5zf7.mongodb.net:27017' => [ServerDescription],
'ac-uam254z-shard-00-02.zlp5zf7.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-o9z7oe-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: 30
},
code: undefined,
[cause]: MongoNetworkError: read ECONNRESET
at TLSSocket.<anonymous> (C:\Users\btrah\OneDrive\Desktop\Stuff for Others\k-link\node_modules\mongodb\lib\cmap\connect.js:286:44)
at Object.onceWrapper (node:events:623:26)
at TLSSocket.emit (node:events:508:28)
at emitErrorNT (node:internal/streams/destroy:170:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at process.processTicksAndRejections (node:internal/process/task_queues:89:21) {
errorLabelSet: Set(1) { 'ResetPool' },
beforeHandshake: false,
[cause]: Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
}
}
Whats that mean? Im new to databases
sabrw
sabrwโ€ข2d ago
I prefer defining let client = interaction.client so you wont need to write interaction over and over again
Skull
SkullOPโ€ข2d ago
Kk
Samtino
Samtinoโ€ข2d ago
It's a network error. It means you lost connection to your database
Skull
SkullOPโ€ข2d ago
Oh alright. Thx How do I add an interaction for when a button is pressed?
Samtino
Samtinoโ€ขthis hour
Switch your InteractionCreate event to instead of returning early if it's not a command, instead do a chain of if, else if statements
if (interaction.isChatInputCommand()) {
// Do command stuff
} else if (interaction.isButton()) {
// Do button stuff
}

return
if (interaction.isChatInputCommand()) {
// Do command stuff
} else if (interaction.isButton()) {
// Do button stuff
}

return
That's at least one way of doing it
Skull
SkullOPโ€ข21h ago
Okay.
const { Events, MessageFlags } = require('discord.js');
module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
flags: MessageFlags.Ephemeral,
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
flags: MessageFlags.Ephemeral,
});
}
}
},
};
const { Events, MessageFlags } = require('discord.js');
module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
flags: MessageFlags.Ephemeral,
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
flags: MessageFlags.Ephemeral,
});
}
}
},
};
How would I add the button logic here

Did you find this page helpful?