Reputation:
I'm having some problems with the discord.js delete function. I'm using this code to remove 1 point from a user's balance when an image is deleted from a channel, the first time everything works fine, but when i try to delete another image i sent, instead of removing 1 point from my balance, the bot will remove many points as how many images have been deleted in total starting from the bot's launch. Example: I delete an image, and the bot will remove from my balance 1 point, then if i delete another image i uploaded, instead of removing 1 point, it will remove 2 points from my balance, because it will count the first image deleted and this. How can i fix it and make only respond to one image deleted?
//(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;
});
The bot should just remove 1 point from the user's balance.
Upvotes: 0
Views: 1157
Reputation:
My issue was that I had the messageDelete
event inside the message
event. attachIsImage()
should've been defined outside any events, then called inside of them.
Upvotes: 1