【问题标题】:Can't catch an Invalid Form Body error discord.js无法捕获无效的表单正文错误 discord.js
【发布时间】:2021-09-24 19:30:40
【问题描述】:

我正在尝试根据给定的 JSON 数据创建一个发送嵌入的命令,它确实有效。

我给机器人提供数据,如果它是正确的,它会将它发送到频道,这很完美,正是我需要它做的。但是,如果有人试图将文本放在链接部分,它会使机器人崩溃。我试过try catch,但由于某种原因它不会捕获。

错误:Scheme "(my string that is not a url)" is not supported. Scheme must be one of ('http', 'https').

        try {
            let json = message.content.split(" ")
            json.shift() //get rid of the command bit, leaving the JSON
            let data = json.join(" ")
            let embedjson = new Discord.MessageEmbed(JSON.parse(data))
            message.channel.send({ embeds: [embedjson]}) //this works, unless it meets the problem listed above.
        } catch (error) { // >:( wont catch it
            let errorembed = new Discord.MessageEmbed()
            console.log(error)
            .setTitle('Error!')
            .setDescription(`Something went wrong! There are a few possible issues:\n1. You tried to put text in a link option (Like putting 'hello' in the image option, or 'never gonna give you up' in the thumbnail option.)\n2. Something else\nI'll attatch the error below:`)
            .addField('Error Message', error)
            message.channel.send({ embeds: [errorembed]})
        }

关于如何发现错误有什么建议吗?非常感谢任何帮助。

【问题讨论】:

  • 我相信添加async-await 有效。测试它并告诉我会发生什么。
  • @MrMythical 好的,我将execute() 更改为async execute() 并执行await message.channel.send() 并再次运行它,但没有成功。接下来我将尝试 danky 的解决方案。

标签: javascript node.js discord discord.js


【解决方案1】:

我相信 Discord.JS 的客户端有一个错误事件,它可以只记录错误而不是使节点实例崩溃

例子:

client.on('error', (err) => {
    console.log(err)
})

您的代码仍会出错,但您的应用程序将继续运行。不是很理想,但如果没有其他办法,是一个很好的解决方案。

您也可以使用process.on('error'),以您的船漂浮为准

【讨论】:

  • 这不是最好的主意。此事件旨在正确处理错误,不退出进程可能会导致它被损坏,或者出现无限循环。
  • 无论如何都试过了,不幸的是,得到了同样的错误。不过谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 2021-01-25
  • 2022-01-15
  • 2010-10-02
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
相关资源
最近更新 更多