I am trying to make a command to send an embed based on given JSON data, and it works, sort of.
I give the bot the data, and if it is correct, it will send it to the channel, which is perfect, exactly what I need it to do. BUT, if someone tries to put text in a link section, it will crash the bot. I tried try catch but it wont catch for some reason.
Error: 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]})
}
Any suggestions as to how I can catch the error? Any help is much appreciated.
I believe Discord.JS's client has an error event which could just log the error instead of crashing the node instance
Example:
client.on('error', (err) => {
console.log(err)
})
Your code will still error, but your application will continue running. Not ideal, but a good solution if there is no other way.
You may also use process.on('error'), whichever floats your boat