【问题标题】:discord-buttons : Wondering how I can repeatedly edit a previous reply using a buttondiscord-buttons :想知道如何使用按钮重复编辑以前的回复
【发布时间】:2021-09-05 11:21:37
【问题描述】:

我正在为我的机器人添加一个烘焙小游戏。在这里,您可以按照机器人提供的不同说明来创建项目。

我想知道如何编辑我的机器人的上一条消息以匹配其中一个步骤的进度。在这种情况下,您需要通过单击按钮将鸡蛋打入碗中。我正在努力做到这一点,以便每次您破解鸡蛋时机器人发送的消息都会更新您的进度,直到您达到配额为止。

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**")
                                    }
                                }
                            }
                        }
                    }
                });
            }

另外,如果我能更好地组织这个命令,请告诉我!我还在学习 JavaScript,在这类东西方面我只是个业余爱好者。

【问题讨论】:

  • 你能告诉我你使用的是哪个包,因为clickButton事件不是discord.js事件。
  • 我正在使用不和谐按钮,你可以在这里找到它:npmjs.com/package/discord-buttons。没提是我的错,对不起。

标签: javascript discord discord.js discord-buttons


【解决方案1】:

首先你应该把事件监听器放在discord.js的message事件监听器之外。在我看来,使用 Map 通过消息 ID 保存鸡蛋是明智的。它看起来像这样:

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();
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2016-05-20
    • 2023-03-21
    • 2015-06-29
    相关资源
    最近更新 更多