'Confused with discordjs + twitter api

to get straight to the point, ive never been so confused... this command was mapped out to be easy and simple which is exactly how it went until this issue.

when grabbing a name on discord ex < $twit jack > it works just fine but when its faced with longer ids like mine, it just throws 3 random numbers at the end

ex: my actual twitter id (1054084643346137089) | what my bot displays (1054084643346137100)

as you see it just throws random numbers for the last 3

you could test it yourself on https://tweeterid.com/

code -

const Discord = require("discord.js");
const request = require("node-superfetch");
const { stripIndents } = require("common-tags");
const bearer =
  ""; // empty only for this

module.exports = {
  name: "twit",
  aliases: ["twitter", "twitid"],
  description: "This command allows admins to grab twitter user ids.",
  /**
   * @param {Client} client
   * @param {Message} message
   * @param {String[]} args
   */
  run: async (client, message, args) => {
    const username = args[0];
    if (!username)
      return message.channel.send("You must provide a username to search for.");

    try {
      const { body } = await request
        .get("https://api.twitter.com/1.1/users/show.json")
        .set({ Authorization: `Bearer ${bearer}` })
        .query({ screen_name: username });

      message.channel.send(
        `Twitter ID For ${username}: http://www.twitter.com/I/user/${body.id}`
      );
    } catch (error) {
      if (error.status === 403)
        return message.channel.send(
          "This user either went private or deactivated their account."
        );
      else if (error.status === 404)
        return message.channel.send("Couldn't find this user.");
      else return message.channel.send(`Unknown Error: ${error.message}`);
    }
  },
};


Solution 1:[1]

The problem is that Twitter is using a signed 64 bit integer for storing the user ID. This number is greater than 53 bits (MAX_SAFE_INTEGER) and JavaScript has difficulty interpreting it. It can only safely represent integers between -(253 - 1) and 253 - 1.

Luckily, Twitter's user object also provides the string representation of the unique identifier as id_str. As the docs mention; "Implementations should use this rather than the large, possibly un-consumable integer in id".

So all you need to do is use id_str instead of id:

const body = {
  id: 1054084643346137089,
  id_str: '1054084643346137089'
}

console.log(`Twitter ID: http://www.twitter.com/I/user/${body.id}`)
console.log(`Twitter ID: http://www.twitter.com/I/user/${body.id_str}`)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zsolt Meszaros