Bot becomes unresponsive after executing a slash command

Hello! I'm building a discord bot and I've got it functional up to the point of the slash command handler working for one iteration. After any user sends a slash command the bot becomes unresponsive I'm not sure where in my Logic the issue may be, so I'm going to post the relevant code blocks and maybe someone might be able to help me understand where I went wrong šŸ™‚
20 Replies
d.js toolkit
d.js toolkitā€¢2y ago
ā€¢ What's your exact discord.js npm list discord.js and node node -v version? ā€¢ Post the full error stack trace, not just the top part! ā€¢ Show your code! ā€¢ Explain what exactly your issue is. ā€¢ Not a discord.js issue? Check out #useful-servers.
Snapper
SnapperOPā€¢2y ago
discord.js@14.11.0 node v18.16.0 bot.js
require('dotenv').config();
const { token } = process.env;
const { Client, Collection, Events, GatewayIntentBits} = require('discord.js');
const fs = require('fs');


const client = new Client({intents: [GatewayIntentBits.Guilds]});
client.commands = new Collection();
client.commandArray = []
//client.colour = "";

const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders){
const functionFiles = fs
.readdirSync(`./src/functions/${folder}`)
.filter(file => file.endsWith(".js"));
for (const file of functionFiles)
require(`./functions/${folder}/${file}`)(client);
}

client.once(Events.ClientReady, c => {
console.log(`Logged in as ${c.user.tag}`);
})

client.handleEvents();
client.handleCommands();
client.login(token);
require('dotenv').config();
const { token } = process.env;
const { Client, Collection, Events, GatewayIntentBits} = require('discord.js');
const fs = require('fs');


const client = new Client({intents: [GatewayIntentBits.Guilds]});
client.commands = new Collection();
client.commandArray = []
//client.colour = "";

const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders){
const functionFiles = fs
.readdirSync(`./src/functions/${folder}`)
.filter(file => file.endsWith(".js"));
for (const file of functionFiles)
require(`./functions/${folder}/${file}`)(client);
}

client.once(Events.ClientReady, c => {
console.log(`Logged in as ${c.user.tag}`);
})

client.handleEvents();
client.handleCommands();
client.login(token);
handleCommands.js
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const fs = require("fs");

module.exports = (client) => {
client.handleCommands = async () => {
const commandFolders = fs.readdirSync("./src/commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./src/commands/${folder}`)
.filter((file) => file.endsWith(`.js`));

const { commands, commandArray } = client;
for (const file of commandFiles) {
const command = require(`../../commands/${folder}/${file}`);
commands.set(command.data.name, command);
commandArray.push(command.data.toJSON());
}
}

const clientId = "1114306384029306994";
const guildId = "1103668741000142919";
const rest = new REST({ version: "9" }).setToken(process.env.token);
try {
console.log("Started refreshing application (/) commands.");

await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: client.commandArray,
});

console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
};
};
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const fs = require("fs");

module.exports = (client) => {
client.handleCommands = async () => {
const commandFolders = fs.readdirSync("./src/commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./src/commands/${folder}`)
.filter((file) => file.endsWith(`.js`));

const { commands, commandArray } = client;
for (const file of commandFiles) {
const command = require(`../../commands/${folder}/${file}`);
commands.set(command.data.name, command);
commandArray.push(command.data.toJSON());
}
}

const clientId = "1114306384029306994";
const guildId = "1103668741000142919";
const rest = new REST({ version: "9" }).setToken(process.env.token);
try {
console.log("Started refreshing application (/) commands.");

await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: client.commandArray,
});

console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
};
};
interactionCreate.js
module.exports = {
name: "interactionCreate",
once: true,
async execute(interaction, client) {
if (interaction.isChatInputCommand()){
const { commands } = client;
const { commandName } = interaction;
const command = commands.get(commandName);
if(!command) return;

try{
await command.execute(interaction, client);
}catch(error){
console.error(error);
await interaction.reply({
content: `Something went wrong while executing this command...`,
ephemeral: true
});
}
}
},
};
module.exports = {
name: "interactionCreate",
once: true,
async execute(interaction, client) {
if (interaction.isChatInputCommand()){
const { commands } = client;
const { commandName } = interaction;
const command = commands.get(commandName);
if(!command) return;

try{
await command.execute(interaction, client);
}catch(error){
console.error(error);
await interaction.reply({
content: `Something went wrong while executing this command...`,
ephemeral: true
});
}
}
},
};
ping.js (the only command I currently have)
const { SlashCommandBuilder } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("Return my ping!"),
async execute(interaction, client) {
const message = await interaction.deferReply({
fetchReply: true,
});

const newMessage = `API Latency: ${client.ws.ping}\nClient Ping: ${
message.createdTimestamp - interaction.createdTimestamp
}`;
await interaction.editReply({
content: newMessage,
});
},
};
const { SlashCommandBuilder } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("Return my ping!"),
async execute(interaction, client) {
const message = await interaction.deferReply({
fetchReply: true,
});

const newMessage = `API Latency: ${client.ws.ping}\nClient Ping: ${
message.createdTimestamp - interaction.createdTimestamp
}`;
await interaction.editReply({
content: newMessage,
});
},
};
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOPā€¢2y ago
there aren't any compiler errors in VS Code
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOPā€¢2y ago
none that come up no it just freezes on the console
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOPā€¢2y ago
like, I can't type anything into the console after I hit to run
Snapper
SnapperOPā€¢2y ago
Snapper
SnapperOPā€¢2y ago
and I get this error the second time I try to use a slash command
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOPā€¢2y ago
handleEvents.js
const fs = require(`fs`);

module.exports = (client) =>
(client.handleEvents = async () => {
const eventFolders = fs.readdirSync(`./src/events`);
for (const folder of eventFolders) {
const eventFiles = fs
.readdirSync(`./src/events/${folder}`)
.filter((file) => file.endsWith(`.js`));
switch (folder){
case "client":
for (const file of eventFiles){
const event = require(`../../events/${folder}/${file}`)
if (event.once) client.once(event.name, (...args) => event.execute(...args, client));
else client.on(event.name, (...args) => event.execute(...args, client));
}
break;

default:
break;
}
}
});
const fs = require(`fs`);

module.exports = (client) =>
(client.handleEvents = async () => {
const eventFolders = fs.readdirSync(`./src/events`);
for (const folder of eventFolders) {
const eventFiles = fs
.readdirSync(`./src/events/${folder}`)
.filter((file) => file.endsWith(`.js`));
switch (folder){
case "client":
for (const file of eventFiles){
const event = require(`../../events/${folder}/${file}`)
if (event.once) client.once(event.name, (...args) => event.execute(...args, client));
else client.on(event.name, (...args) => event.execute(...args, client));
}
break;

default:
break;
}
}
});
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOPā€¢2y ago
just set that to false or remove it entirely?
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOPā€¢2y ago
I'll give this a try, thank you šŸ™‚
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOPā€¢2y ago
I was worried about that too x.x sometimes it returns a positive value like 30 or so which seemed correct
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOPā€¢2y ago
that worked! šŸ˜„ thank you so much and now the -1 isn't showing up either lol

Did you find this page helpful?