discord-buttons :想知道如何使用按钮重复编辑以前的回复
discord-buttons : Wondering how I can repeatedly edit a previous reply using a button
我正在为我的机器人添加一个烘焙小游戏。在这里您可以按照机器人提供的不同说明来创建项目。
我想知道如何编辑我的机器人之前的消息以匹配其中一个步骤的进度。在这种情况下,您需要通过单击按钮将鸡蛋打入碗中。我正在努力做到这一点,以便机器人发送的消息会在您每次打蛋时更新您的进度,直到达到配额。
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 并且在涉及此类内容时只是一个业余爱好者。
首先,您应该将事件监听器放在 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();
}
});
我正在为我的机器人添加一个烘焙小游戏。在这里您可以按照机器人提供的不同说明来创建项目。
我想知道如何编辑我的机器人之前的消息以匹配其中一个步骤的进度。在这种情况下,您需要通过单击按钮将鸡蛋打入碗中。我正在努力做到这一点,以便机器人发送的消息会在您每次打蛋时更新您的进度,直到达到配额。
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 并且在涉及此类内容时只是一个业余爱好者。
首先,您应该将事件监听器放在 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();
}
});