0
        var color = Math.floor(Math.random() * 16777215).toString(16);
        var hex = Number.parseInt(color, 16);
        console.log(hex);

        message.channel.send({embed: {
            color: hex, //This is the place I attempt to implement it. It doesn't accept strings.
            author: {
          name: message.author.username + "'s Profile",
          icon_url: message.author.avatarURL
        },
        description: ":white_small_square: **About:** " + message.author.bio,
        fields: [{
            name: "Client Info",
            value: ":white_small_square: **User ID** - " + "`" + message.member.id + "`" + "\n:white_small_square: **Status** - " + "`" + message.author.presence.status.toUpperCase() + "`" + " | ***Is*** " + occupation
          },
          {
            name: "Server Info",
            value: ":white_small_square: **Joined at** - "  /*+ "`" + new Date(guild.detailsOfUser(message.author.user).joinedAt).toUTCString() + "`" */+ "\n:white_small_square: **Avowed Roles** - " + ranks
          }
        ],
        timestamp: new Date(),
        footer: {
          text: message.author.username,
        }
      }
    });
    }

I have been looking around and found a forum post in which a group of people successfully created a Hexadecimal generator in one line. This works perfectly fine, however, I do not want it to be a string. I have attempted to parseInt after the line but it just returns as "undefined" and when put into a spot of a number it says it is not an integer. If anyone has any ideas on how to parse it as an Int, please tell me.

EDIT: Prior to my explanation of why I am closing this off, thank you to everyone who helped me attempt to achieve repairing this problem. I was just experimenting and realised that the .toString(16) was never actually needed and that the resulting number was a valid color. Thanks to everyone once more for helping me

Sean
  • 767
  • 1
  • 6
  • 19
  • According to the doc I see [here](https://anidiotsguide_old.gitbooks.io/discord-js-bot-guide/content/examples/using-embeds-in-messages.html) `color` should be an integer value (or _probably_ a string in a format `"#00AE86"`). I'm not sure where do you get the requirement of it being some mysterious "hex" format . `0x00AE86` is just a way of writing the same number as `44678`. So the crucial part still missing in your question is: what happens if you remove the `toString(16)` part? How exactly it doesn't work? Is it an error? Is it something else? – SergGr May 25 '18 at 00:39
  • Ok, so you're implying a string would work as well if it had a # before it? If so, I'll modify the code and hopefully it should have the outcome I'm looking for :) – Sean May 25 '18 at 00:45
  • According to the answer on this question https://stackoverflow.com/questions/46734519/how-to-use-local-file-as-thumbnail-in-discordjs-embedded-message, it does indeed need to be a hexadecimal value. But, as @SergGr says, it's just an integer number. Even when you put `0x00AE86` into your web console, it shows you an integer – NocNit May 25 '18 at 00:45
  • Just a side note, are you sure your random number is a valid color value? – NocNit May 25 '18 at 00:46
  • 1
    Yeah, judging by [Embed Visualizer](https://leovoel.github.io/embed-visualizer/) the `color` has to be integer and not string. String is probably supported only by `RichEmbed` builder. Still "hexadecimal" is not a property of integer. It is the way to represent an integer. `10` and `0xA` is the same integer after interpreter/compiler has parsed the code. – SergGr May 25 '18 at 00:47
  • Alright, so I'm not quite sure what exactly is wrong here. Unfortunately, I cannot confirm any of the code quite yet as I am at school and cant perform it. And yes @NocNit I have entered my returned values of the Hex generator and they are in fact valid colors – Sean May 25 '18 at 00:51
  • @Masterthias I just played around with the visualiser @SergGr provided. You're correct, they are indeed valid values. Your use of `16777215` is spot on. According to the visualiser however, any integer value I enter that's below that number generates a different color for me – NocNit May 25 '18 at 00:54
  • 1
    @NocNit, since `16777215` is actually `0xFFFFFF`, yes any value generated by that code should be in the range of valid colors. @Masterthias, you probably should get back here when you have more details on what exactly goes wrong if you remove the `.toString(16)` part. As a simple check that the `color` is actually the reason of the issue, you can for test purpose hardcode some color like `0x112233` and check if all other fields are OK. – SergGr May 25 '18 at 00:54
  • @SergGr Yup, I will do so. Thanks for tagging along, I will be back in about 4 hours when I am back at home or if you have anything more to say :) Once more, thanks for the help SergGr and NocNit – Sean May 25 '18 at 00:58

1 Answers1

1

You can reverse the process by using, as noted here:

parseInt(hex, 16);

Or alternatively:

Number.parseInt(hex, 16);

This will change it back into an int you can store.

NocNit
  • 280
  • 1
  • 7
  • or better yet, use [Number.parseInt(n,radix)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseInt) – code_monk May 24 '18 at 23:48
  • This seems to work as my program is detecting it as a number, however, it doesn't return as a hexadecimal which was what the toString(16); was for. I didn't get rid of it but it seems to bypass. – Sean May 24 '18 at 23:50
  • @Masterthias, if the answer helps you - mark it as Accepted. P.S. By the way, if you don't need string why you just don't remove the `.toString(16)` part and use `var hex = Math.floor(Math.random() * 16777215)`? – SergGr May 24 '18 at 23:55
  • Im trying to make a hexadecimal. The .toString(16) returns it as one. I reckon its the most crucial and vital part of the whole line. It doesn't work btw so I haven't accepted it yet. – Sean May 24 '18 at 23:58
  • @Masterthias, when you say "_Im trying to make a hexadecimal_" and at the same time "_I do not want it to be a string_" what exactly do you mean? On the hardware level numbers a stored in a binary format and you probably can't change that. So what exactly do you want? Assuming the "random" part of the generator produced integer value of `1234`. What exactly value (and of what type) do you want to get in such case? – SergGr May 25 '18 at 00:05
  • @SergGr I'm trying to make a Hexadecimal that will fit into the spot of a number. The exact use I am using it for is a color for an embedded message in Discord, it uses Hexadecimal. Manually putting it in i.e. 0x897a67 would work, but I'm trying to make it a random color everything time through this line. It doesn't accept strings but for some reasons accept those single letters within a Hexadecimal. I'm quite confused myself which is why I planned to reach out. – Sean May 25 '18 at 00:22
  • @Masterthias, I think you should edit your question to provide a piece of code with the context of how exactly you are going to use the `hex` value after all. For now this is not clear and thus the requirements are not clear. Ideally you should provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – SergGr May 25 '18 at 00:27
  • @SergGr I have done that, hopefully it will give you a bit more context – Sean May 25 '18 at 00:34