Discord command unable to read array arguments, gives a console error "Cannot read properties of undefined"

Viewed 68

I'm just playing around with a discord bot command idea that I've had. Basically it uses the axios packages to sent viewers to an ebay listing (entertainment purposes). I'm using discordjs v13.6.0 and the latest axios packages. The command is a slash based command that goes /ebay => listing => viewers. Unsure where exactly it crashes, but I'm under the assumption it's crashing when I input the specified viewers sent because it'll give an error in the terminal "if (args.length < 3 { TypeError: Cannot read properties of undefined (reading 'length') I'm assuming that's because length isn't define, but unsure exactly how I'll go about fixing that. Here's my following code.

const Discord = require("discord.js")
const axios = require("axios")
const { CommandInteraction, MessageEmbed } = require('discord.js');

module.exports = {
    name: "ebay",
    description: "Use this command to send viewers to your ebay listing",
    permission: "USE_APPLICATION_COMMANDS",
    options: [
        {
            name: "listing",
            description: "Input the link to your listing",
            type: "STRING",
            required: true
        },
        {
            name: "viewers",
            description: "Input the amount of viewers",
            type: "INTEGER",
            required: true
        }
    ],

    /**
     * 
     * @param {CommandInteraction} interaction 
     */
    async execute (interaction, msg) {
        const { options } = interaction;
        const listing = options.getString("listing")
        const viewers = options.getInteger("viewers")
        let args;
        if (msg && msg.content) {
            args = msg.content.split(' ');
        }

        if (args.length < 3) {
            errorEmbed = new MessageEmbed()
            .setColor("#E80E0E")
            .setTitle('Error Found! ❌')
            .setDescription(`Viewers cannot be sent at this time, make sure you're inputting the correct link`)
            interaction.reply({ embeds: [ebayEmbed] });
            return;
        }

        for (let i = 0; i < args[2]; i++) {
            await axios.get(args[1], {
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.5195.102 Safari/537.36'
                }
            });
        }

        ebayEmbed = new MessageEmbed()
            .setColor("#5104DB")
            .setTitle("Successfully Sent Viewers! ")
            .setDescription(`**${viewers}** have been successfully sent to **${listing}**!`)

        interaction.reply({ embeds: [ ebayEmbed ] });
    }
}
0 Answers
Related