【问题标题】:Error deleting a Discord message with timeout删除带有超时的 Discord 消息时出错
【发布时间】:2021-05-30 22:21:28
【问题描述】:

代码:

const discord = require('discord.js')

module.exports.run = async (bot, message, args) =>{
    message.delete()
    if(!message.member.roles.some(r=> ["TESTER"].includes(r.name)) ) {
        return message.reply(`<:prohibited:814861316715839488> você não tem permissão de fazer isso!`)
    }
    let mensagem = args.join(' ')
    if(!args[0]) {
        return message.reply(`<:prohibited:814861316715839488> você não adicionou algo para ser avisado!`)
    }
    var embed = new discord.RichEmbed()
    .setColor("RANDOM")
    .setAuthor('SERVER - AVISO', bot.user.avatarURL)
    .setDescription(mensagem)
    .setFooter(message.author.username, message.author.avatarURL)
    .setTimestamp()
    .setThumbnail(bot.user.avatarURL)

    bot.channels.cache.get(`786364360071053403`).send('@everyone').then(m =>{
        m.delete(100)
    })
    bot.channels.cache.get(`786364360071053403`).send(embed);
}

module.exports.config = {
    name: 'anuncio',
    aliases: ['anunciar', 'sendbell']
}

错误:

(node:3196) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied options is not an object.   
    at Message.delete (C:\Users\Pc\Desktop\RemakeTO\node_modules\discord.js\src\structures\Message.js:577:60)
    at C:\Users\Pc\Desktop\RemakeTO\comandos\staffbell.js:21:13
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3196) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3196) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我做错了什么?有人能帮我吗?向 Discord.JS v12 的过渡让我很困惑!发送和删除“@everyone”时出现错误。我不知道如何解决它。

【问题讨论】:

  • How do I ask a good question?:“写一个总结具体问题的标题”
  • 错误很明显:“提供的选项不是对象”Message.delete。只有一个地方您尝试删除消息...

标签: javascript node.js discord discord.js


【解决方案1】:

从 Discord.js v13 开始,the timeout option has been removed。理由是该实现容易出错,例如不检查消息是否已被删除,并且它是唯一具有timeout 选项的方法。

您可以使用以下命令删除超时消息:

setTimeout(() => {
  m.delete().catch(error => {
    // If the message is deleted, the Discord API will return an unknown message error
    if (
      error instanceof DiscordAPIError &&
      error.code === Constants.APIErrors.UNKNOWN_MESSAGE
    )
      return
    // handle error
    console.error(error)
  })
}, 100)

还有Messagedeleted 属性,但从我的测试来看,如果在超时处理程序触发之前删除消息,它通常不会及时设置为true

【讨论】:

    【解决方案2】:

    您当前的方法m.delete(100) 已弃用,此方法已在 v11 中使用。当前使用超时选项的稳定版本 v12 如下所示

    m.delete({timeout: 100})
    

    但是,此方法也将在下一个即将发布的 DJS 版本 v13 中弃用。我强烈建议您使用 v13 的推荐方法并使用 setTimeout() 函数

    setTimeout(() => {
       m.delete();
    }, 100);
    

    【讨论】:

      【解决方案3】:

      message.delete() 接受一个选项对象,但您只传递了一个数字。如果你想延迟方法执行,你可以像这样使用timeout 选项:

      bot.channels.cache.get(`786364360071053403`)
        .send('@everyone')
        .then(m => {
          m.delete({timeout: 100})
        })
      

      【讨论】:

        猜你喜欢
        • 2021-01-28
        • 2021-06-17
        • 2021-10-18
        • 2020-08-26
        • 2019-10-25
        • 1970-01-01
        • 1970-01-01
        • 2020-08-24
        • 2019-03-18
        相关资源
        最近更新 更多