我在discord.js删除功能上遇到了一些问题。从频道删除图片时,我第一次使用此代码从用户的余额中删除1点,但一切正常,但是当我尝试删除发送的另一张图片时,而不是从余额中删除1点,该机器人将删除许多点,因为从该机器人启动以来,总共删除了多少张图片。示例:我删除了一张图片,机器人会从我的余额中删除1点,然后,如果我删除了我上传的另一张图片,而不是删除1点,它将从我的余额中删除2点,因为它将计算删除的第一张图片和这个。我该如何解决它并仅响应删除的一张图像?
//(works) This is the function that make the bot read the message's attach
function attachIsImageJPG(messageAttach) {
var url = messageAttach.url;
//True if this url is a png image.
return url.indexOf("jpg", url.length - "jpg".length /*or 3*/) !== -1;
}
//(works) There's the code that make the bot assign 1 point when the image is uploaded
if (message.attachments.size > 0) {
if (message.channel.id != '593093789971644417') return;
if (message.attachments.every(attachIsImage)){
eco.AddToBalance(message.author.id, 1)
bot.channels.get("593093471175311438").send(itag + message.author.id + ftag + " **1** has been **added** to your `!balance` for sending the success the screenshot");
}
}
//(problem) This is the part with the error
bot.on("messageDelete", async (messageDelete) => {
if (messageDelete.channel.id === '593093789971644417') {
function attachIsImageJPG(messageAttach) {
var url = messageAttach.url;
//True if this url is a png image.
return url.indexOf("jpg", url.length - "jpg".length /*or 3*/) !== -1;
}
if (message.attachments.size > 0) {
if (message.attachments.every(attachIsImageJPG)) {
await eco.SubstractFromBalance(message.author.id, 1) // money.updateBal grabs the (userID, value) value being how much you want to add, and puts it into 'i'.
return bot.channels.get("593093471175311438").send(itag + message.author.id + ftag + " **1** has been **removed** from your `!balance` for deleting the screenshot");
} else return;
} else return;
} else return;
});
机器人应仅从用户余额中删除1点。
最佳答案
我的问题是我在messageDelete
事件中包含了message
事件。 attachIsImage()
应该在任何事件之外定义,然后在它们内部调用。