尝试在发送5秒钟后在discord.js中删除discord机器人的消息,但出现错误

时间:2020-05-28 11:46:03

标签: javascript node.js discord.js chatbot

首先,我对任何一种编程方法都是陌生的,这是我刚开始的3个小时之内。

正如我所说,我试图让该机器人在说出几秒钟后删除其“清除”消息。 这是我尝试过的方法:

if (msg.member.hasPermission("ADMINISTRATOR") || msg.member.roles.cache.find(r => r.name === "Clear Perm")) {
  if (!args[1] || args[1] > 99 || args[1] < 1) {
    msg.channel.send('Invalid Argument Given, Please Provide An Amount Of Messages Between 1-99 To Be Deleted')
  } else {
    var BD = parseInt(args[1]) + 1;

    function DelCount() {
      msg.channel.send("`I Have Deleted " + args[1] + " Message(s)`");
    }

    msg.channel.bulkDelete(BD);
    setTimeout(DelCount, 10);
    setTimeout(() => msg.delete(msg.content.includes('I Have Deleted')), 5000)
  }
  if (!msg.member.hasPermission("ADMINISTRATOR") && !msg.member.roles.cache.find(r => r.name === "Clear Perm")) {
    msg.channel.send("You Do Not Have Permission To Clear Chat");
  }
}

当我这样做时,出现以下错误:

C:\Users\Rick\Desktop\Discord Bot\KendanK\node_modules\discord.js\src\structures\Message.js:501
    if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
                                     ^

TypeError [INVALID_TYPE]: Supplied options is not an object.
    at Message.delete (C:\Users\Rick\Desktop\Discord Bot\KendanK\node_modules\discord.js\src\structures\Message.js:501:44)
    at Timeout._onTimeout (C:\Users\Rick\Desktop\Discord Bot\KendanK\index.js:170:48)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7) {
  [Symbol(code)]: 'INVALID_TYPE'
}

有人知道我做错了什么,我应该怎么做吗?

1 个答案:

答案 0 :(得分:0)

问题来自此行:

setTimeout(() => msg.delete(msg.content.includes('I Have Deleted')), 5000)

您正在传递msg.delete(msg.content.includes('I Have Deleted')作为Message.delete()的参数,而您正在传递布尔值。
另外,我认为您应该使用Promises而不是依赖超时。

这是我要怎么做:

var BD = parseInt(args[1]) + 1

msg.channel.bulkDelete(BD)
  .then(async (deleted) => { // Use .then to run when the Promise is resolved

    // Save the response message directly from the .send method (using await)
    let response = await msg.channel.send(`${deleted.size} messages deleted.`)

    // There's a built-in timeout option, you don't need to set it up by yourself
    reponse.delete({ timeout: 5000 }) 
  })

如果您在努力理解承诺,异步/等待之类的东西,则可以从以下链接中读取一些内容: