【问题标题】:UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied options is not an objectUnhandledPromiseRejectionWarning:TypeError [INVALID_TYPE]:提供的选项不是对象
【发布时间】:2020-08-10 08:40:20
【问题描述】:

我正在使用 Discord.JS 制作一个不和谐机器人,但是当我尝试执行命令时,它会显示以下错误:

(node:4) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied options is not an object. at Message.delete (/app/node_modules/discord.js/src/structures/Message.js:501:44) at /app/commands/warn.js:3:89.

warn.js 代码是:

module.exports.run = async (bot, message, args) => {
    let target = message.mentions.members.first()
    if(!target) return message.channel.send("Couldn't find that ID!").then(m => m.delete(15000))

    let reason = args.slice(1).join(" ")
    if(!reason) return message.channel.send(`Please give a reason to warn **${target.user.tag}**!`).then(m => m.delete(15000))

    message.channel.send(`Warned **${target.user.tag}**!`).then(m => m.delete(15000))
    // get role by name
let myRole = message.guild.roles.cache.find(role => role.name === "Warned");
target.addRole(role).catch(console.error);

}

module.exports.help = {
    name: "warn",
    aliases: []
}

我真的不知道为什么会这样,也找不到错误。 有人可以看看代码,看看有什么问题吗?我将不胜感激。

【问题讨论】:

    标签: javascript promise discord.js


    【解决方案1】:

    delete 函数的参数需要一个对象,您可以在其中指定超时时间。

        if(!reason) return message.channel.send(`Please give a reason to warn **${target.user.tag}**!`).then(m => m.delete({timeout: 15000}))
    

    【讨论】:

      【解决方案2】:

      在 javascript 中有多种方法可以处理异步代码。

      在您的示例中,您使用的是async 函数。这意味着您可以使用 async/await - 此功能构建在 Promise 之上并带有语法糖。
      它允许您以“同步”方式编写代码。 所以你可能想像这样重写你的 Promise 调用:

      
      module.exports.run = async (bot, message, args) => {
          let target = message.mentions.members.first()
          if(!target) {
            const message = await message.channel.send("Couldn't find that ID!"); 
            return message.delete(15000)
          }
      
      

      另外,错误UnhandledPromiseRejectionWarning 会提醒您在 Promise 执行中出现错误。这里给delete 函数的参数类型错误。需要一个对象,但收到一个数字。这是documentation of the message API

      更进一步,您可以使用 .catch 链接函数来处理 Promises 拒绝,它可以用于调试或记录错误。

      // Promises style
      let target = message.mentions.members.first()
      
      if(!target) {
        return message.channel.send("Couldn't find that ID!")
        .then(m => m.delete(15000))
        .catch(error => console.log(error))
      }
      
      

      或者使用 async/await 语法:

      // async/await style
      let target = message.mentions.members.first()
      
      if(!target) {
        try {
          const message = await message.channel.send("Couldn't find that ID!"); 
          return message.delete(15000)
        } catch(error) {
          console.log(error)
        }
      }
      

      【讨论】:

      • 我改成那个了,现在又显示一个新错误:TypeError: target.addRole is not a function。
      • 它来自代码target.addRole(role).catch(console.error);我不知道discordJS的详细信息,但看起来你的目标对象没有addRole方法。您可能想阅读文档或只记录目标对象
      • 也许你正在寻找这种方法:discord.js.org/#/docs/main/master/class/…target.edit({ roles: [...] })
      猜你喜欢
      • 2020-07-22
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 2021-08-13
      • 2020-12-25
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多