Deferrals

I've had a few issues as I have started to try and learn deferring and avoiding timeout errors. Many scripts that worked before now only work half the time. Often I get the discord API error for 10062 which I understand means it usually took longer than 3 seconds to respond, then it times out. I also am self hosting this bot for testing purposes and have a less than optimal connection. However, all these commands worked fine previously so I want to make sure its not something in my amateur code. More than anything Im just wanting to verify whether or not Im approaching this correctly or if its a strange issues because of my subpar connection speeds. Ive been through the documentation and cant seem to find what I'm doing wrong, if anything at all. I've tried a few variations of things, sometimes with half sucess, but this is an example code thats having the problem right now and crashing for the api 10062 error:
12 Replies
d.js toolkit
d.js toolkitβ€’11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
TTastic
TTasticβ€’11mo ago
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { checkHearthProfile } = require('../utils/verifications/profileVerify'); // Import profile verification function

module.exports = {
data: new SlashCommandBuilder()
.setName('location')
.setDescription("Check your current location in the game world"),

async run({ interaction }) {

try {

const userWhoRan = interaction.user;
const hasHearthProfile = await checkHearthProfile(interaction, userWhoRan.id);
console.log(`πŸ’»User who ran the /location command: ${userWhoRan.tag}`);

// Defer the initial response, more than 3 seconds without a response will cause a Discord API error
await interaction.deferReply();

if (!hasHearthProfile) {
await interaction.reply('Oops! You need to register!');
return;
} else {

// If a profile is found, respond with a "Location placeholder" embed
const profileFoundEmbed = new EmbedBuilder()
.setTitle(' :warning: Location placeholder')
.setColor('#FFD700') // Dark yellow color
.setDescription('This is coming soon.');

// Respond with the "Location placeholder" embed
await interaction.editReply({ embeds: [profileFoundEmbed] });
}

// Additional logic for the location command

} catch (error) {
console.error(':no_entry_sign:Error while querying the database:', error);
}
},

options: {
// deleted: true;
devOnly: true,
},
};
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { checkHearthProfile } = require('../utils/verifications/profileVerify'); // Import profile verification function

module.exports = {
data: new SlashCommandBuilder()
.setName('location')
.setDescription("Check your current location in the game world"),

async run({ interaction }) {

try {

const userWhoRan = interaction.user;
const hasHearthProfile = await checkHearthProfile(interaction, userWhoRan.id);
console.log(`πŸ’»User who ran the /location command: ${userWhoRan.tag}`);

// Defer the initial response, more than 3 seconds without a response will cause a Discord API error
await interaction.deferReply();

if (!hasHearthProfile) {
await interaction.reply('Oops! You need to register!');
return;
} else {

// If a profile is found, respond with a "Location placeholder" embed
const profileFoundEmbed = new EmbedBuilder()
.setTitle(' :warning: Location placeholder')
.setColor('#FFD700') // Dark yellow color
.setDescription('This is coming soon.');

// Respond with the "Location placeholder" embed
await interaction.editReply({ embeds: [profileFoundEmbed] });
}

// Additional logic for the location command

} catch (error) {
console.error(':no_entry_sign:Error while querying the database:', error);
}
},

options: {
// deleted: true;
devOnly: true,
},
};
Unknown User
Unknown Userβ€’11mo ago
Message Not Public
Sign In & Join Server To View
TTastic
TTasticβ€’11mo ago
It scans a collection in my database to see if the user has any associated information so Im guessing that may be it. Im relatively new to this, so are you saying to run the deferReply before the try block or to just remove the await from the initial defer altogether?
Unknown User
Unknown Userβ€’11mo ago
Message Not Public
Sign In & Join Server To View
TTastic
TTasticβ€’11mo ago
OH WAIT. Sorry, like I said, new. I see what you're saying. I need to defer it before I declare that constant which checks my database...right?
Unknown User
Unknown Userβ€’11mo ago
Message Not Public
Sign In & Join Server To View
TTastic
TTasticβ€’11mo ago
Thank you, Im still trying to catchup and learn to use proper terminology. I appreciate the breakdown. πŸ™‚ Im gonna try a few things with that. Even if Im self hosting with my slow connection, everything should still go through without the 10062 if I set it up properly, right?
Unknown User
Unknown Userβ€’11mo ago
Message Not Public
Sign In & Join Server To View
TTastic
TTasticβ€’11mo ago
Absolutely, I appreciate the advice on best practices. Just frutsrating trying to learn how to diagnose whether is a user issue or network issue on my end lol. (90% of the time its me πŸ˜› )
Unknown User
Unknown Userβ€’11mo ago
Message Not Public
Sign In & Join Server To View
TTastic
TTasticβ€’11mo ago
Just wanted to pop back and let you know everything seems smoother now. It never clicked with me the await was taking the time up even though that makes perfect sense. Thanks again for helping a rookie out πŸ™‚