Very slow working code

I've got a problem:
const coordinate_types = require('../constants.json').coordinate_types;
module.exports = {
name: 'get_standard_coordinates_arrow_left',

async execute (client, interaction) {
try {
interaction.deferUpdate()
const pageSize = 10
const footer_text = interaction.message.embeds[0].footer.text
const sliced = footer_text.slice(9, footer_text.length)
console.log(sliced)
const lastPageNum = sliced.split(' из ')[0]
const lastPageMax = sliced.split(' из ')[1]
const query_res = await client.pg.query(`
SELECT * FROM standard_coordinates
WHERE ${client.calcClearanceLevel(interaction.member)} >= clearance
ORDER BY object
`)
const pageMax = Math.ceil(query_res.rowCount / 10) + Number(query_res.rowCount % 10 != 0)
const pageNum = (lastPageNum > 1) ? lastPageNum - 1 : pageMax
const result = query_res.rows.slice((pageNum - 1) * 10, pageNum * 10)
const newEmbed = EmbedBuilder.from(interaction.message.embeds[0])
.setFooter({text: `Страница ${pageNum} из ${pageMax}`})
.setFields([])
console.log(newEmbed);
for (var i = 0; i < result.length; i++) {
const emoji = coordinate_types.find(coordinateType => coordinateType.value == result[i].object).emoji
const object = coordinate_types.find(coordinateType => coordinateType.value == result[i].object).name
const id = result[i].id
newEmbed.data.fields.push({
name: `<:${result[i].object}:${emoji}> - ${object} #${result[i].id} (${result[i].name})`,
value: `\`\`\`${result[i].description}\nGPS:${result[i].name}:${result[i].vector.x}:${result[i].vector.y}:${result[i].vector.z}:#${result[i].rgb}:\`\`\``
})
}
await interaction.message.edit({
embeds: [
newEmbed
]
})
} catch (error) {
console.error(error)

}
}
}
const coordinate_types = require('../constants.json').coordinate_types;
module.exports = {
name: 'get_standard_coordinates_arrow_left',

async execute (client, interaction) {
try {
interaction.deferUpdate()
const pageSize = 10
const footer_text = interaction.message.embeds[0].footer.text
const sliced = footer_text.slice(9, footer_text.length)
console.log(sliced)
const lastPageNum = sliced.split(' из ')[0]
const lastPageMax = sliced.split(' из ')[1]
const query_res = await client.pg.query(`
SELECT * FROM standard_coordinates
WHERE ${client.calcClearanceLevel(interaction.member)} >= clearance
ORDER BY object
`)
const pageMax = Math.ceil(query_res.rowCount / 10) + Number(query_res.rowCount % 10 != 0)
const pageNum = (lastPageNum > 1) ? lastPageNum - 1 : pageMax
const result = query_res.rows.slice((pageNum - 1) * 10, pageNum * 10)
const newEmbed = EmbedBuilder.from(interaction.message.embeds[0])
.setFooter({text: `Страница ${pageNum} из ${pageMax}`})
.setFields([])
console.log(newEmbed);
for (var i = 0; i < result.length; i++) {
const emoji = coordinate_types.find(coordinateType => coordinateType.value == result[i].object).emoji
const object = coordinate_types.find(coordinateType => coordinateType.value == result[i].object).name
const id = result[i].id
newEmbed.data.fields.push({
name: `<:${result[i].object}:${emoji}> - ${object} #${result[i].id} (${result[i].name})`,
value: `\`\`\`${result[i].description}\nGPS:${result[i].name}:${result[i].vector.x}:${result[i].vector.y}:${result[i].vector.z}:#${result[i].rgb}:\`\`\``
})
}
await interaction.message.edit({
embeds: [
newEmbed
]
})
} catch (error) {
console.error(error)

}
}
}
21 Replies
d.js toolkit
d.js toolkit14mo 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!
SpecialSauce
SpecialSauce14mo ago
If I had to venture a guess on the issue, it’s likely the database query. But a little more info would help. How long is it taking to complete? Is there any error? “I’ve got a problem” isn’t giving us much to go off of.
Ender Yee'kkay
Ender Yee'kkayOP14mo ago
no errors and warnings. Request to local db takes 3-8 ms. I tracked that 99% of the execution time is taken up by this section.
await interaction.message.edit({
embeds: [
newEmbed
]
await interaction.message.edit({
embeds: [
newEmbed
]
@SpecialSauce and the running time of the script is very random, then 300ms, as in all my other similar scripts, then 15000ms
Ishh
Ishh14mo ago
*don't ping. Anyone
Looped
Looped14mo ago
Then try editReply or check if its a network problem since the runtimes are different. and do as Qjuh said by awaiting the deferUpdate
Ender Yee'kkay
Ender Yee'kkayOP14mo ago
that isn't a reply
No description
Ender Yee'kkay
Ender Yee'kkayOP14mo ago
that is common message
Looped
Looped14mo ago
Well, have you tried awaiting the deferUpdate? If that doesn't work then I dont believe this issue is related to d.js
SpecialSauce
SpecialSauce14mo ago
Sounds like a possible rate limit
d.js docs
d.js docs14mo ago
Please add the following code to your code base outside of any other event listeners and provide the full log output relevant to your issue.
client
.on("debug", console.log)
.on("warn", console.log)
client
.on("debug", console.log)
.on("warn", console.log)
- Note: if you initialize your Client as bot or other identifiers you need to use these instead of client - If the output is too long to post consider using a bin instead: gist | paste.gg | sourceb.in | hastebin
SpecialSauce
SpecialSauce14mo ago
It may be a better option to respond to the interaction as recommended by Qjuh. Those endpoints probably have different rate limits.
Ender Yee'kkay
Ender Yee'kkayOP14mo ago
Pastebin
log - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Ender Yee'kkay
Ender Yee'kkayOP14mo ago
it is advisable to avoid this, since it will require a complete restructuring of the database and script
SpecialSauce
SpecialSauce14mo ago
What would need to be changed besides interaction.message.edit > interaction.editReply?
Ender Yee'kkay
Ender Yee'kkayOP14mo ago
That message isn't a respons of interaction. That one is a static message interaction.editReply throws an error, while I am using it
SpecialSauce
SpecialSauce14mo ago
What error?
Ender Yee'kkay
Ender Yee'kkayOP14mo ago
oh lol I used interaction.message.editReply() 😄 now It works and works fast (maximum 500 ms)
SpecialSauce
SpecialSauce14mo ago
Just be aware of the possible race condition of not awaiting the deferUpdate() you could respond sooner than when that completes Id probably just await it
Ender Yee'kkay
Ender Yee'kkayOP14mo ago
but why in other my scripts (I use await there) this problem doesn't cause?
Ender Yee'kkay
Ender Yee'kkayOP14mo ago
Pastebin
code - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Ender Yee'kkay
Ender Yee'kkayOP14mo ago
it works well and I use that:
interaction.message.edit({
embeds: [
interaction.message.embeds[0],
newMainEmbed,
interaction.message.embeds[2],
interaction.message.embeds[3]
]
})
interaction.message.edit({
embeds: [
interaction.message.embeds[0],
newMainEmbed,
interaction.message.embeds[2],
interaction.message.embeds[3]
]
})
Thanks for help) maybe
Want results from more Discord servers?
Add your server