首页 > 解决方案 > 无法捕获无效的表单正文错误 discord.js

问题描述

我正在尝试创建一个命令来根据给定的 JSON 数据发送嵌入,并且它可以正常工作。

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

错误: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]})
        }

关于如何捕捉错误的任何建议?任何帮助深表感谢。

标签: javascriptnode.jsdiscorddiscord.js

解决方案


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

例子:

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

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

你也可以使用process.on('error'),无论哪个漂浮你的船


推荐阅读