Home > Back-end >  Binary64 can't handle special characters discord.js
Binary64 can't handle special characters discord.js

Time:06-08

I was trying to create a command on where the bot should encode and decode the words. But the problem is when the bot detect a special characters, The bot shutdown.

Here's my code:

const messages = args.slice(1).join(" ")
    Axios.get(`https://some-random-api.ml/base64?encode=${messages}`)
    .then((res) => {
         const embed = new MessageEmbed()
         .setTitle('Decoder')
         .setDescription(res.data.text)
         .setColor('RANDOM')
         .setTimestamp()

    message.channel.send({embeds: [embed]})
})

const messages = args.slice(1).join(" ")
    Axios.get(`https://some-random-api.ml/base64?decode=${messages}`)
    .then((res) => {
         const embed = new MessageEmbed()
         .setTitle('Decoder')
         .setDescription(res.data.text)
         .setColor('RANDOM')
         .setTimestamp()

    message.channel.send({embeds: [embed]})
})

CodePudding user response:

You can use encodeURI and decodeURI to make your command works Might look at here as well

const messages = args.slice(1).join(" ")
    const encodedURI = encodeURI(messages)
    Axios.get(`https://some-random-api.ml/base64?encode=${encodedURI}`)
    .then((res) => {
         const embed = new MessageEmbed()
         .setTitle('Decoder')
         .setDescription(res.data.text)
         .setColor('RANDOM')
         .setTimestamp()

    message.channel.send({embeds: [embed]})
})

const messages = args.slice(1).join(" ")
const decodedURI = decodeURI(messages)
    Axios.get(`https://some-random-api.ml/base64?decode=${decodedURI}`)
    .then((res) => {
         const embed = new MessageEmbed()
         .setTitle('Decoder')
         .setDescription(res.data.text)
         .setColor('RANDOM')
         .setTimestamp()

    message.channel.send({embeds: [embed]})
})

CodePudding user response:

As mentioned by 新Acesyyy, URL-encoding your string helps work around the "special character" problem, but that might not be the whole picture of the problem.

I suggest considering validating (or at least sanitizing) client input before allowing them enter the back-end application and database logic. Because I sense a overlook of security in your application.

I also suggest taking a look at the OWASP Cheetsheet when you have time.

  • Related