【问题标题】:Problem with discord.js delete message functiondiscord.js删除消息功能的问题
【发布时间】:2019-11-07 14:15:49
【问题描述】:

我在使用 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 分。

【问题讨论】:

标签: javascript node.js discord discord.js


【解决方案1】:

我的问题是我在 message 事件内有 messageDelete 事件。 attachIsImage() 应该在任何事件之外定义,然后在它们内部调用。

【讨论】:

    猜你喜欢
    • 2020-08-07
    • 2018-08-26
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2021-06-05
    • 2020-05-17
    相关资源
    最近更新 更多