javascript - 无法捕获无效的表单正文错误 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]})
}
关于如何捕捉错误的任何建议?任何帮助深表感谢。
解决方案
我相信 Discord.JS 的客户端有一个错误事件,它可以只记录错误而不是使节点实例崩溃
例子:
client.on('error', (err) => {
console.log(err)
})
您的代码仍会出错,但您的应用程序将继续运行。不理想,但如果没有其他方法,这是一个很好的解决方案。
你也可以使用process.on('error')
,无论哪个漂浮你的船
推荐阅读
- flutter - 没有为“对象”类型定义运算符“[]”。尝试定义运算符'[]'。根据日期对列表进行分组中的 Dart 问题
- javascript - 匹配/合并两个具有关系的数据数组的最简单方法是什么?
- asp.net-core - ASP.NET Core Razor 页面中的 AAD-B2C 授权
- regex - 使用具有不同键值对的目标文本创建正则表达式?
- amazon-web-services - AWS Simple Scaling - 横向扩展后会自动缩回吗?
- python - 如何处理 SimpleImputer 中的日期时间或分类?
- kubernetes - 如何将 kubectl 命令用于在 docker 运行的 Rancher 中实现的 kubernetes?
- docker - docker - 如何隐藏构建参数
- python - 检测到值错误循环引用 Python
- windows - 自动锁定的 Docker Swarm:重新启动 Docker 后应用程序无法启动(Windows)