Pepega Pingu
Pepega Pingu

Reputation: 29

discord-buttons : Wondering how I can repeatedly edit a previous reply using a button

I am adding a baking minigame to my bot. It's where you follow along different instructions provided by the bot to create an item.

I would like to know how I can edit my bot's previous message to match the progress of one of the steps. In this case, you need to crack eggs into a bowl by clicking the button. I'm trying to make it so the message the bot sends updates your progress each time you crack an egg until you reach the quota.

var itemNames = args.join(' ').toLowerCase();
            console.log(itemNames)

            if ((itemNames === "cupcakes") || (itemNames === "cupcake")) {

                const eggsButton = new MessageButton()
                    .setLabel("Crack Eggs!")
                    .setStyle("blurple")
                    .setID("cupcakesStepOne")

                const cupcakesStepOne = await message.channel.send("Cupcakes, ok! Don't mess this up, dummy... We need to start with the eggs, why don't you crack a few into that bowl? **0 / 4**", eggsButton)
                var eggs = 0

                client.on('clickButton', async (button) => {
                    if (button.id === "cupcakesStepOne") {
                        button.reply.defer();
                        eggs = eggs + 1
                        console.log(eggs)
                        if (eggs = 1) {
                            cupcakesStepOne.edit("Cupcakes, ok! Don't mess this up, dummy... We need to start with the eggs, why don't you crack a few into that bowl? **1 / 4**")
                        } else {
                            if (eggs = 2) {
                                cupcakesStepOne.edit("Cupcakes, ok! Don't mess this up, dummy... We need to start with the eggs, why don't you crack a few into that bowl? **2 / 4**")
                            } else {
                                if (eggs = 3) {
                                    cupcakesStepOne.edit("Cupcakes, ok! Don't mess this up, dummy... We need to start with the eggs, why don't you crack a few into that bowl? **3 / 4**")
                                } else {
                                    if (eggs = 4) {
                                        cupcakesStepOne.edit("Cupcakes, ok! Don't mess this up, dummy... We need to start with the eggs, why don't you crack a few into that bowl? **4 / 4**")
                                    }
                                }
                            }
                        }
                    }
                });
            }

Also, if I could organise this command better, please let me know! I'm still learning JavaScript and am merely an amateur when it comes to this sort of stuff.

Upvotes: 0

Views: 1123

Answers (1)

Luuk
Luuk

Reputation: 711

First of all you should put the event listener outside the message event listener of discord.js. In my opinion it is smart to use a Map to save the eggs in by the message id. It will look like this:

const buttons = require('discord-buttons');

const client = new Discord.Client();

buttons(client);

const cupcakes = new Map();

client.on('message', async message => {
    if(message.author.bot || message.channel.type === `dm`) return;

    if(!message.content.startsWith(config.prefix)) return;

    let args = message.content.substring("!".length).split(" ");

    var itemNames = args.join(' ').toLowerCase();
    console.log(itemNames)

    if (itemNames === "cupcakes" || itemNames === "cupcake") {

        const eggsButton = new buttons.MessageButton()
        .setLabel("Crack Eggs!")
        .setStyle("blurple")
        .setID("cupcakesStepOne")

        message.channel.send("Cupcakes, ok! Don't mess this up, dummy... We need to start with the eggs, why don't you crack a few into that bowl? **0 / 4**", eggsButton).then(msg => {
            cupcakes.set(msg.id, 0);
        });
    }
});

client.on('clickButton', async (button) => {
    if (button.id === "cupcakesStepOne") {
        if(!cupcakes.get(button.message.id)) return;
        cupcakes.set(button.message.id, cupcakes.get(button.message.id) + 1);
        const eggs = cupcakes.get(button.message.id);
        if(eggs <= 4){
            button.message.edit(`Cupcakes, ok! Don't mess this up, dummy... We need to start with the eggs, why don't you crack a few into that bowl? **${eggs} / 4**`);
            await button.reply.defer();
            if(eggs === 4) cupcakes.delete(button.message.id);
        } else await button.reply.defer();
    }
});

Upvotes: 1

Related Questions