1

I am making a Discord bot that states accounts in it, but for some reason, my code is outputting undefined errors in it. I want to make it so there are no undefined errors. It is not an error necessarily, but it says Undefined along with what we want in there, and we want to remove the Undefined message COMPLETELY. Images are linked below along with the not working code.

if(msg.startsWith(`${prefix}gen choice`)) {
        if(message.channel.type == "text") {
            let result3 = Math.floor((Math.random() * accounts3.length))
            let acc3embed = new Discord.MessageEmbed()
            .setAuthor('Test Bot', 'https://cdn.discordapp.com/attachments/421820457587703812/736328588500140102/264855.png')
            .setColor('#49a5bf')
            .setTitle("Choice Succesfully generated!")
            .setDescription('Here is your choice!')
            .setTimestamp()
            .addField('' + accounts3[result3] + "")
            .addField('-------------------------------------------------', "**Want cool things? Click the link below!**\n Enter Website Here")
            .setFooter("Test Bot");

        message.author.send(acc3embed);
        message.reply("I've sent you the choice! Please check your DMs!!")
        }[enter image description here][1]
    }

Here is the image: https://i.sstatic.net/ayR69.jpg

Please help me! Thank you!

6
  • What does console.log(accounts3[result3]) produce? Commented Jul 25, 2020 at 6:47
  • the image implies accounts3[result3] is undefined, so figure out why that is undefined if it's supposed to be something, or remove that line completely. Commented Jul 25, 2020 at 6:49
  • I do not know why it is undefined. The accounts3[results3] produces the code in BOLD, but it STILL says Undefined. @dikuw Commented Jul 25, 2020 at 6:52
  • Also, @Unkwn the console.log(accounts3[result3]) produces the BOLD code you see in the screenshot. Commented Jul 25, 2020 at 6:58
  • can you provide a screenshot of console.log(accounts3[result3])? Commented Jul 25, 2020 at 6:59

1 Answer 1

1

This is because the addField method requires two string arguments (a title, currently defined as accounts3[result3] and a value, currently set as... undefined, because you didn't provide it). Update your code like that:

if(msg.startsWith(`${prefix}gen choice`)) {
        if(message.channel.type == "text") {
            let result3 = Math.floor((Math.random() * accounts3.length))
            let acc3embed = new Discord.MessageEmbed()
            .setAuthor('Test Bot', 'https://cdn.discordapp.com/attachments/421820457587703812/736328588500140102/264855.png')
            .setColor('#49a5bf')
            .setTitle("Choice Succesfully generated!")
            .setDescription('Here is your choice!')
            .setTimestamp()
            .addField('' + accounts3[result3] + "", '\u200B')
            .addField('-------------------------------------------------', "**Want cool things? Click the link below!**\n Enter Website Here")
            .setFooter("Test Bot");

        message.author.send(acc3embed);
        message.reply("I've sent you the choice! Please check your DMs!!")
        }
    }

It will replace undefined with spaces.

If you want to remove the spaces, you can use the following code:

if(msg.startsWith(`${prefix}gen choice`)) {
        if(message.channel.type == "text") {
            let result3 = Math.floor((Math.random() * accounts3.length))
            let acc3embed = new Discord.MessageEmbed()
            .setAuthor('Test Bot', 'https://cdn.discordapp.com/attachments/421820457587703812/736328588500140102/264855.png')
            .setColor('#49a5bf')
            .setTitle("Choice Succesfully generated!")
            .setDescription('Here is your choice!')
            .setTimestamp()
            .addField('' + accounts3[result3] + "", '-------------------------------------------------')
            .addField('Want cool things? Click the link below!', "Enter Website Here")
            .setFooter("Test Bot");

        message.author.send(acc3embed);
        message.reply("I've sent you the choice! Please check your DMs!!")
        }
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you so much! But there is another issue, is it possible if I can remove this big gap here? It is very distracting. I want to be the choice and then the long dashes. Thank you! IMAGE: imgur.com/a/liTlblQ
Unfortunately, it's difficult. I've edited my post to offer you another solution.
Alright. Thank you so much for your help! How difficult would it be to do it so the second field is still the dashes and each one is spaced evenly? Would I use addFields or...? Thanks!
You can use addField('\u200B', '\u200B') to create a totally blank field. You can add it between all your field to space them evenly.
Alright! Thank you so much for your help! I will mess around with the code and see what I can do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.