devRael1 / discordjs-pagination

A pagination system for Discord.js v14
https://www.npmjs.com/package/@devraelfreeze/discordjs-pagination
MIT License
11 stars 10 forks source link

Bot crashing when embed is deleted #12

Closed ryan-hmd closed 1 year ago

ryan-hmd commented 1 year ago

Hello, your module works perfectly but I've found a small problem that I can't solve. If I delete the embed before the timeout has expired, the bot crashes completely and I can't catch the error with a trycatch block. I can't publish my bot while this problem persists. Can you help?

devRael1 commented 1 year ago

Hello, if you want to delete the message with pagination system, try to remove the timer from pagination function.

ryan-hmd commented 1 year ago

Thank you for your reply. I removed the timer option from the pagination function but the bot crashes again if I try to remove the embed. If the embed is deleted when the timer is over, there's no problem; but before, i.e. while the timer is still active, it's impossible to delete messages without the bot crashing.

devRael1 commented 1 year ago

Can you send me the error here please ?

and

Do you have page Travel enable ?

ryan-hmd commented 1 year ago

I've tried enabling or disabling the pageTravel option but it makes no difference: the bot keeps crashing if I delete an embed whose buttons are still active.

Here's the error I get when I delete an embed whose timeout hasn't expired yet:


C:\Users\ryanh\Desktop\Episkopos\node_modules\@discordjs\rest\dist\index.js:640
      throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
            ^

DiscordAPIError[10008]: Unknown Message
    at handleErrors (C:\Users\ryanh\Desktop\Episkopos\node_modules\@discordjs\rest\dist\index.js:640:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async SequentialHandler.runRequest (C:\Users\ryanh\Desktop\Episkopos\node_modules\@discordjs\rest\dist\index.js:1021:23)
    at async SequentialHandler.queueRequest (C:\Users\ryanh\Desktop\Episkopos\node_modules\@discordjs\rest\dist\index.js:862:14)
    at async REST.request (C:\Users\ryanh\Desktop\Episkopos\node_modules\@discordjs\rest\dist\index.js:1387:22)
    at async InteractionWebhook.editMessage (C:\Users\ryanh\Desktop\Episkopos\node_modules\discord.js\src\structures\Webhook.js:338:15)
    at async ChatInputCommandInteraction.editReply (C:\Users\ryanh\Desktop\Episkopos\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:158:17) {
  requestBody: {
    files: [],
    json: {
      content: undefined,
      tts: false,
      nonce: undefined,
      embeds: undefined,
      components: [ { type: 1, components: [ [Object], [Object] ] } ],
      username: undefined,
      avatar_url: undefined,
      allowed_mentions: undefined,
      flags: undefined,
      message_reference: undefined,
      attachments: undefined,
      sticker_ids: undefined,
      thread_name: undefined
    }
  },
  rawError: { message: 'Unknown Message', code: 10008 },
  code: 10008,
  status: 404,
  method: 'PATCH',
  url: 'https://discord.com/api/v10/webhooks/1106104630548631603/aW50ZXJhY3Rpb246MTExNTY4MTIzMzI4NDkwMzAyMzowUjJJREpyNUJRRHRIMEk3MkowM3RUWTRaOXRqSGRVc1VGbUc2UklQTzZ0NGJnSTZ5VTNqR01rWEVBQm1oN1hsY05jUEtTSlF5aTRKUlNRcldzUXR0YTFKYmZtZ0RrQWxtemZ3UVlDYjJMeldnOHJXalU0R0NKRXRvcWJubjJaNw/messages/@original'
}

Node.js v18.16.0```
devRael1 commented 1 year ago

Show me your code where you use the pagination function of the package

ryan-hmd commented 1 year ago

For context: the command calls an API that provides the content of Catholic religious services. After processing this content (liturgy which is an array of objects), I create my embeds by organizing the number of fields associated with each (array cadency) then I put each embed in an array (embedLiturgy) which I pass as a parameter to the pagination function.

if (interaction.commandName === 'liturgie') {
    const office = interaction.options.get('office').value
    const liturgy = await aelf.getLiturgy(office)
    try {
        const embedLiturgy = []
        const cadency = config[office.toUpperCase() + "_CADENCY"]
        cadency.forEach(fieldsNb => {
            const embedPart = new EmbedBuilder()
                .setColor(0xF6D496)
                .setTitle("Liturgie des heures 🙏")
                .setDescription(`Office des **${office}** du jour.`)
                .setImage('https://www.soissons.catholique.fr/wp-content/uploads/sites/13/2016/09/Logo-AELF.png')
                .setFooter({ text: "Episkopos - Liturgie des heures - https://www.aelf.org/" })
            for(let i = 1; i <= fieldsNb; i++) {
                const content_object = liturgy[0]
                embedPart.addFields({ name: content_object.title.toUpperCase(), value: content_object.content.slice(0, 1024) })
                liturgy.shift()
            }
            embedLiturgy.push(embedPart)
        })
        await pagination({
            interaction: interaction,
            embeds: embedLiturgy,
            author: interaction.member.user,
            fastSkip: false,
            time: 40000,
            disableButtons: true,
            pageTravel: false,
            buttons: [
                {
                    type: ButtonTypes.previous,
                    label: '',
                    style: ButtonStyles.Primary,
                    emoji: '⬅️'
                },
                {
                    type: ButtonTypes.next,
                    label: '',
                    style: ButtonStyles.Primary,
                    emoji: '➡️'
                }
            ]
        })
    } catch (error) {
        interaction.reply({ content: `Erreur dans le traitement de la commande.\n${error}`, ephemeral: true })
        console.log(error)
    }
}
devRael1 commented 1 year ago

Try to set disableButtons to false.

If it doesn't work, try to go to the package folder in node_modules. Go to dist folder and go to the pagination.js file. Try to add a catch function error on these 2 edit functions:

image

I think that's where the mistake comes from

ryan-hmd commented 1 year ago

Well done !

I tried the first suggestion you made and it didn't make any difference, the bot kept crashing if you deleted the embeds whose buttons hadn't yet expired.

But the modification you suggested works! So I added the catch to the elements you indicated and the bot no longer crashes when the "active" embeds are deleted. I suggest you update the module. Thanks for your help!

Here's the code:

collector.on("end", () => {
    if (type === 'message') {
        initialMessage.edit({
            components: disableB ? components(true) : []
        }).catch(err => console.log("Interaction has been deleted before timeout."));
    }
    else {
        interaction.editReply({
            components: disableB ? components(true) : []
        }).catch(err => console.log("Interaction has been deleted before timeout."));
    }
});
devRael1 commented 1 year ago

Ah lets go !

I will update the package.