Bot crashing when calling custom /suggest command

So I have a /suggest command for my discord bot that should take the user's input, and output it to a private text channel 'bot-suggestions'. I've confirmed that it's writing the suggestion to suggestions.txt, but I think the bot is crashing when it tries to send the embed? Not entirely sure what's happening here:
// Import required dependencies
const { SlashCommandBuilder } = require("@discordjs/builders");
const { EmbedBuilder } = require("discord.js");
const fs = require("fs").promises;

// Define the slash command
const suggestCommand = new SlashCommandBuilder()
.setName("suggest")
.setDescription("Make a suggestion for the bot")
.addStringOption((option) =>
option
.setName("suggestion")
.setDescription("Your suggestion")
.setRequired(true)
);

// Define the function to run when the command is used
async function suggest(interaction) {
// Get the suggestion from the user
const suggestion = interaction.options.getString("suggestion");

// Get the bot-suggestions channel
const botSuggestionsChannel = interaction.guild.channels.cache.find(
(channel) => channel.name === "bot-suggestions"
);

try {
// Write the suggestion to the file
await fs.appendFile("suggestions.txt", `${suggestion}\n`);
console.log(`Suggestion written to suggestions.txt: ${suggestion}`);

// Create an embed with the suggestion and user info
const embed = new EmbedBuilder()
.setColor("#0099ff")
.setTitle("New suggestion")
.setDescription(suggestion)
.addFields({ name: "User", value: interaction.user.tag, inline: true })
.setTimestamp();

console.log("Embed built.");
console.log("Writing to channel: " + botSuggestionsChannel.name);
// Send the embed to the bot-suggestions channel
await botSuggestionsChannel.send({ embeds: [embed] });
console.log("Suggestion sent to bot-suggestions channel.");

// Reply to the user with a confirmation message
await interaction.reply({
content: "Your suggestion has been received.",
});
console.log("Reply sent to user: Your suggestion has been received.");
} catch (error) {
console.error(error);
await interaction.reply({
content: "An error occurred while processing your suggestion.",
});
console.log("Error occurred while processing suggestion:", error);
}
}

// Export the command and execute function
module.exports = {
data: suggestCommand,
execute: suggest,
};
// Import required dependencies
const { SlashCommandBuilder } = require("@discordjs/builders");
const { EmbedBuilder } = require("discord.js");
const fs = require("fs").promises;

// Define the slash command
const suggestCommand = new SlashCommandBuilder()
.setName("suggest")
.setDescription("Make a suggestion for the bot")
.addStringOption((option) =>
option
.setName("suggestion")
.setDescription("Your suggestion")
.setRequired(true)
);

// Define the function to run when the command is used
async function suggest(interaction) {
// Get the suggestion from the user
const suggestion = interaction.options.getString("suggestion");

// Get the bot-suggestions channel
const botSuggestionsChannel = interaction.guild.channels.cache.find(
(channel) => channel.name === "bot-suggestions"
);

try {
// Write the suggestion to the file
await fs.appendFile("suggestions.txt", `${suggestion}\n`);
console.log(`Suggestion written to suggestions.txt: ${suggestion}`);

// Create an embed with the suggestion and user info
const embed = new EmbedBuilder()
.setColor("#0099ff")
.setTitle("New suggestion")
.setDescription(suggestion)
.addFields({ name: "User", value: interaction.user.tag, inline: true })
.setTimestamp();

console.log("Embed built.");
console.log("Writing to channel: " + botSuggestionsChannel.name);
// Send the embed to the bot-suggestions channel
await botSuggestionsChannel.send({ embeds: [embed] });
console.log("Suggestion sent to bot-suggestions channel.");

// Reply to the user with a confirmation message
await interaction.reply({
content: "Your suggestion has been received.",
});
console.log("Reply sent to user: Your suggestion has been received.");
} catch (error) {
console.error(error);
await interaction.reply({
content: "An error occurred while processing your suggestion.",
});
console.log("Error occurred while processing suggestion:", error);
}
}

// Export the command and execute function
module.exports = {
data: suggestCommand,
execute: suggest,
};
Console output ('Ready! Logged in' is the bot's initialization message): 0|bot | Ready! Logged in as "OPE Bot#0931" 0|bot | Suggestion written to suggestions.txt: goddamn dude 0|bot | Embed built. 0|bot | Writing to channel: bot-suggestions 0|bot | Ready! Logged in as "OPE Bot#0931" Discord in/output: /suggest goddamn dude 'The application did not respond'
28 Replies
d.js toolkit
d.js toolkit17mo 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.
spunkir
spunkirOP17mo ago
[email protected] node -v: v18.16.0
Shaurya
Shaurya17mo ago
use codeblock or code playgrounds for sharing long codes so we can read properly
spunkir
spunkirOP17mo ago
? sorry I haven't posted before in here, can you elaborate?
Shaurya
Shaurya17mo ago
use codeblock i.e. ```lang code ```
spunkir
spunkirOP17mo ago
Ah, apologies
Shaurya
Shaurya17mo ago
lang = js, for this case js in place of lang, not lang = js 💀 also, can you show the full error stack
spunkir
spunkirOP17mo ago
got it lmao The 0 | bot output is the only log I have
/home/ec2-user/.pm2/logs/bot-out.log last 15 lines:
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Suggestion written to suggestions.txt: eat dick
0|bot | Embed built.
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Suggestion written to suggestions.txt: who?
0|bot | Embed built.
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Suggestion written to suggestions.txt: eat my ass
0|bot | Embed built.
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Ready! Logged in as "OPE Bot#0931"

0|bot | Suggestion written to suggestions.txt: this guy's a murderer
0|bot | Embed built.
0|bot | Writing to channel: bot-suggestions
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Suggestion written to suggestions.txt: goddamn dude
0|bot | Embed built.
0|bot | Writing to channel: bot-suggestions
0|bot | Ready! Logged in as "OPE Bot#0931"
/home/ec2-user/.pm2/logs/bot-out.log last 15 lines:
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Suggestion written to suggestions.txt: eat dick
0|bot | Embed built.
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Suggestion written to suggestions.txt: who?
0|bot | Embed built.
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Suggestion written to suggestions.txt: eat my ass
0|bot | Embed built.
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Ready! Logged in as "OPE Bot#0931"

0|bot | Suggestion written to suggestions.txt: this guy's a murderer
0|bot | Embed built.
0|bot | Writing to channel: bot-suggestions
0|bot | Ready! Logged in as "OPE Bot#0931"
0|bot | Suggestion written to suggestions.txt: goddamn dude
0|bot | Embed built.
0|bot | Writing to channel: bot-suggestions
0|bot | Ready! Logged in as "OPE Bot#0931"
Shaurya
Shaurya17mo ago
you should improve your error handling then, we don't know what the actual error is
spunkir
spunkirOP17mo ago
how so? how would you catch the await botSuggestionsChannel.send([ embeds: [embed] }); ? i.e. line 43
Shaurya
Shaurya17mo ago
seeing the logs I think the bot is logging in after the command is ran?
spunkir
spunkirOP17mo ago
the current structure is: log "embed built" log "writing to channel..." channel.send embed there's another log underneath the channel.send, but the bot looks like it's restarting before reaching it
Shaurya
Shaurya17mo ago
try removving your custom error logging system, and see what's the actual error
spunkir
spunkirOP17mo ago
I ran it on my local machine instead of my EC2 and it looks like it ran the command with no issue? The whole bot's stored in git and all leaves are updated
SpecialSauce
SpecialSauce17mo ago
So it runs on local but not on EC2? Does it also log the same 'Ready!' Messages?
spunkir
spunkirOP17mo ago
no, it outputs as expected:
Ready! Logged in as "OPE Bot#0931"
Suggestion written to suggestions.txt: testing
Embed built.
Writing to channel: bot-suggestions
Suggestion sent to bot-suggestions channel.
Reply sent to user: Your suggestion has been received.
Ready! Logged in as "OPE Bot#0931"
Suggestion written to suggestions.txt: testing
Embed built.
Writing to channel: bot-suggestions
Suggestion sent to bot-suggestions channel.
Reply sent to user: Your suggestion has been received.
SpecialSauce
SpecialSauce17mo ago
It’s becoming ready an awful lot, are you running any commands that cause the client to crash and restart? I’m just trying to figure out the circumstances of it becoming ready 4 times in a row.
spunkir
spunkirOP17mo ago
I don't have any that explicitly crash it, I am however running it on a pm2, so in the event that the bot does crash, it auto-restarts.
SpecialSauce
SpecialSauce17mo ago
Is there a separate error log? Try to console.log instead of console.error and see if the logs are different. That’s the only thing I can think of.
spunkir
spunkirOP17mo ago
Looks like pm2 has it's own logging system:
[TAILING] Tailing last 15 lines for [bot] process (change the value with --lines option)
/home/ec2-user/.pm2/logs/bot-error.log last 15 lines:
0|bot | | CombinedPropertyError (1)
0|bot | | Received one or more errors
0|bot | |
0|bot | | input.value
0|bot | | | ValidationError > s.string
0|bot | | | Expected a string primitive
0|bot | | |
0|bot | | | Received:
0|bot | | | | [User]
0|bot |
0|bot | at ArrayValidator.handle (/home/ec2-user/OPEbot/node_modules/@sapphire/shapeshift/dist/index.js:468:70)
0|bot | at ArrayValidator.parse (/home/ec2-user/OPEbot/node_modules/@sapphire/shapeshift/dist/index.js:207:88)
0|bot | at EmbedBuilder.addFields (/home/ec2-user/OPEbot/node_modules/@discordjs/builders/dist/index.js:222:31)
0|bot | at Object.suggest [as execute] (/home/ec2-user/OPEbot/commands/moderation/suggest.js:37:8)
0|bot | at async Client.<anonymous> (/home/ec2-user/OPEbot/index.js:43:5)
[TAILING] Tailing last 15 lines for [bot] process (change the value with --lines option)
/home/ec2-user/.pm2/logs/bot-error.log last 15 lines:
0|bot | | CombinedPropertyError (1)
0|bot | | Received one or more errors
0|bot | |
0|bot | | input.value
0|bot | | | ValidationError > s.string
0|bot | | | Expected a string primitive
0|bot | | |
0|bot | | | Received:
0|bot | | | | [User]
0|bot |
0|bot | at ArrayValidator.handle (/home/ec2-user/OPEbot/node_modules/@sapphire/shapeshift/dist/index.js:468:70)
0|bot | at ArrayValidator.parse (/home/ec2-user/OPEbot/node_modules/@sapphire/shapeshift/dist/index.js:207:88)
0|bot | at EmbedBuilder.addFields (/home/ec2-user/OPEbot/node_modules/@discordjs/builders/dist/index.js:222:31)
0|bot | at Object.suggest [as execute] (/home/ec2-user/OPEbot/commands/moderation/suggest.js:37:8)
0|bot | at async Client.<anonymous> (/home/ec2-user/OPEbot/index.js:43:5)
SpecialSauce
SpecialSauce17mo ago
Ahh yeah there’s your error(or one of them). Sent a user object as a field value.
spunkir
spunkirOP17mo ago
Yeah I tried sending in the user.username, which should be a string, but haven't seen a difference:
// Create an embed with the suggestion and user info
const embed = new EmbedBuilder()
.setColor("#0099ff")
.setTitle("New suggestion")
.setDescription(suggestion)
.addFields({
name: "User",
value: interaction.user.username,
inline: true,
})
.setTimestamp();
// Create an embed with the suggestion and user info
const embed = new EmbedBuilder()
.setColor("#0099ff")
.setTitle("New suggestion")
.setDescription(suggestion)
.addFields({
name: "User",
value: interaction.user.username,
inline: true,
})
.setTimestamp();
Looks like even removing the fields produces the same error
SpecialSauce
SpecialSauce17mo ago
And you’re sure you’re saving and restarting the client? Seems unlikely to receive the same error on the same line if the code doesn’t match. Suggest.js line 37 (as long as this is still being newly produced in the logs) Try clearing the error log and see if it’s still logging errors.
spunkir
spunkirOP17mo ago
Weird. git pull node index.js works, but running my wrapper is when I have issues (there's a git pull with the main repo above the top line):
# Stop the current bot instance
pm2 stop bot

# Run the deploy-commands.js script (assuming it is located in the bot directory)
node deploy-commands.js

# Start the bot using PM2
pm2 start index.js --name bot --watch
# Stop the current bot instance
pm2 stop bot

# Run the deploy-commands.js script (assuming it is located in the bot directory)
node deploy-commands.js

# Start the bot using PM2
pm2 start index.js --name bot --watch
No dice on killing the pm2 either
Auride
Auride17mo ago
Try replacing interaction.user.username with a static string like "hello world" and seeing if that works
spunkir
spunkirOP17mo ago
That produces the same issue oddly enough. I also tried replacing the 'suggestion' variable with a static string and it resulted in the same issue
Auride
Auride17mo ago
Keep replacing variables with static values until you've isolated which expression is causing the error You could also try using breakpoints and a debugger
spunkir
spunkirOP17mo ago
await botSuggestionsChannel.send("Hello world");
await botSuggestionsChannel.send("Hello world");
Also results in the timeout issue. Could it be the way that I'm setting the channel to send to?
// Get the bot-suggestions channel
const botSuggestionsChannel = interaction.guild.channels.cache.find(
(channel) => channel.name === "bot-suggestions"
);
// Get the bot-suggestions channel
const botSuggestionsChannel = interaction.guild.channels.cache.find(
(channel) => channel.name === "bot-suggestions"
);
Oh I meant the timeout where it doesn't respond to the user in discord, not an actual timeout error ^ Current code:
// Import required dependencies
const { SlashCommandBuilder } = require("@discordjs/builders");
const { EmbedBuilder } = require("discord.js");
const fs = require("fs").promises;

// Define the slash command
const suggestCommand = new SlashCommandBuilder()
.setName("suggest")
.setDescription("Make a suggestion for the bot")
.addStringOption((option) =>
option
.setName("suggestion")
.setDescription("Your suggestion")
.setRequired(true)
);

// Define the function to run when the command is used
async function suggest(interaction) {
// Get the suggestion from the user
const suggestion = interaction.options.getString("suggestion");

// Get the bot-suggestions channel
const botSuggestionsChannel = interaction.guild.channels.cache.find(
(channel) => channel.name === "bot-suggestions"
);

try {
// Write the suggestion to the file
await fs.appendFile("suggestions.txt", `${suggestion}\n`);
console.log(`Suggestion written to suggestions.txt: ${suggestion}`);

// Create an embed with the suggestion and user info
const embed = new EmbedBuilder()
.setColor("#0099ff")
.setTitle("New suggestion")
.setDescription(suggestion)
.addFields({
name: "User",
value: interaction.user.username || interaction.member.nickname,
inline: true,
})
.setTimestamp();

console.log("Embed built.");
console.log("Writing to channel: " + botSuggestionsChannel.name);
// Send the embed to the bot-suggestions channel
await botSuggestionsChannel.send(
"Hello world"
// { embeds: [embed] }
);
console.log("Suggestion sent to bot-suggestions channel.");

// Reply to the user with a confirmation message
await interaction.reply({
content: "Your suggestion has been received.",
});
console.log("Reply sent to user: Your suggestion has been received.");
} catch (error) {
console.log(error);
await interaction.reply({
content: "An error occurred while processing your suggestion.",
});
console.log("Error occurred while processing suggestion:", error);
}
}

// Export the command and execute function
module.exports = {
data: suggestCommand,
execute: suggest,
};
// Import required dependencies
const { SlashCommandBuilder } = require("@discordjs/builders");
const { EmbedBuilder } = require("discord.js");
const fs = require("fs").promises;

// Define the slash command
const suggestCommand = new SlashCommandBuilder()
.setName("suggest")
.setDescription("Make a suggestion for the bot")
.addStringOption((option) =>
option
.setName("suggestion")
.setDescription("Your suggestion")
.setRequired(true)
);

// Define the function to run when the command is used
async function suggest(interaction) {
// Get the suggestion from the user
const suggestion = interaction.options.getString("suggestion");

// Get the bot-suggestions channel
const botSuggestionsChannel = interaction.guild.channels.cache.find(
(channel) => channel.name === "bot-suggestions"
);

try {
// Write the suggestion to the file
await fs.appendFile("suggestions.txt", `${suggestion}\n`);
console.log(`Suggestion written to suggestions.txt: ${suggestion}`);

// Create an embed with the suggestion and user info
const embed = new EmbedBuilder()
.setColor("#0099ff")
.setTitle("New suggestion")
.setDescription(suggestion)
.addFields({
name: "User",
value: interaction.user.username || interaction.member.nickname,
inline: true,
})
.setTimestamp();

console.log("Embed built.");
console.log("Writing to channel: " + botSuggestionsChannel.name);
// Send the embed to the bot-suggestions channel
await botSuggestionsChannel.send(
"Hello world"
// { embeds: [embed] }
);
console.log("Suggestion sent to bot-suggestions channel.");

// Reply to the user with a confirmation message
await interaction.reply({
content: "Your suggestion has been received.",
});
console.log("Reply sent to user: Your suggestion has been received.");
} catch (error) {
console.log(error);
await interaction.reply({
content: "An error occurred while processing your suggestion.",
});
console.log("Error occurred while processing suggestion:", error);
}
}

// Export the command and execute function
module.exports = {
data: suggestCommand,
execute: suggest,
};
Current error logs with 'pm2 logs bot --err':
$ pm2 logs bot --err
[TAILING] Tailing last 15 lines for [bot] process (change the value with --lines option)
/home/ec2-user/.pm2/logs/bot-error.log last 15 lines:
0|bot | | CombinedPropertyError (1)
0|bot | | Received one or more errors
0|bot | |
0|bot | | input.value
0|bot | | | ValidationError > s.string
0|bot | | | Expected a string primitive
0|bot | | |
0|bot | | | Received:
0|bot | | | | [User]
0|bot |
0|bot | at ArrayValidator.handle (/home/ec2-user/OPEbot/node_modules/@sapphire/shapeshift/dist/index.js:468:70)
0|bot | at ArrayValidator.parse (/home/ec2-user/OPEbot/node_modules/@sapphire/shapeshift/dist/index.js:207:88)
0|bot | at EmbedBuilder.addFields (/home/ec2-user/OPEbot/node_modules/@discordjs/builders/dist/index.js:222:31)
0|bot | at Object.suggest [as execute] (/home/ec2-user/OPEbot/commands/moderation/suggest.js:37:8)
0|bot | at async Client.<anonymous> (/home/ec2-user/OPEbot/index.js:43:5)
$ pm2 logs bot --err
[TAILING] Tailing last 15 lines for [bot] process (change the value with --lines option)
/home/ec2-user/.pm2/logs/bot-error.log last 15 lines:
0|bot | | CombinedPropertyError (1)
0|bot | | Received one or more errors
0|bot | |
0|bot | | input.value
0|bot | | | ValidationError > s.string
0|bot | | | Expected a string primitive
0|bot | | |
0|bot | | | Received:
0|bot | | | | [User]
0|bot |
0|bot | at ArrayValidator.handle (/home/ec2-user/OPEbot/node_modules/@sapphire/shapeshift/dist/index.js:468:70)
0|bot | at ArrayValidator.parse (/home/ec2-user/OPEbot/node_modules/@sapphire/shapeshift/dist/index.js:207:88)
0|bot | at EmbedBuilder.addFields (/home/ec2-user/OPEbot/node_modules/@discordjs/builders/dist/index.js:222:31)
0|bot | at Object.suggest [as execute] (/home/ec2-user/OPEbot/commands/moderation/suggest.js:37:8)
0|bot | at async Client.<anonymous> (/home/ec2-user/OPEbot/index.js:43:5)
Want results from more Discord servers?
Add your server