Skip to content
Advertisement

Discord bot not fetching user data even when user id is there

I am using discord v13.6.0 . I have a 2d array in which data looks like this

[
 [ 'userID', 0 ],
 [ 'userID2', 0 ],
]

Here is my code .

  let index;
        for(index=0;index<items.length;index++){
          if(items[index][0]==msg){
            break;
          }
  }
        console.log("In is ",index);
        if(index==0){
          rank = 1;
          let j=index+2;
          let k = index + 1;
          let currentUserId = items[index][0];
          let nextUserId = items[k][0];
          console.log(nextUserId);
          let nextUserId2 = items[j][0];


          let currentUserTag = client.users.cache.get(currentUserId);
          let currentName = currentUserTag.username;
          let nextUserTag = client.users.cache.get(nextUserId);
          let nextUserName = nextUserTag.username;


          console.log(currentName);
          console.log(nextUserName);

Now I am getting a error here. My code shows me curretName but it is giving me a error for the nextUserName. My error is this

TypeError: Cannot read properties of undefined (reading ‘username’) at E:DiscordBotTestingLeaderBoardLeaderboardRankingsystem.js:217:42

I am fetching array data from firebase and making it a 2d array. That value in 2d array is being used to fetch user data in discord server where I am trying to get username.

Please help why I am getting the currentname but not for nextUsername. Funny thing is same code is working for discord v12 but fails for discord v13.

Advertisement

Answer

Maybe not using the cache, but not sure:

let index;
        for(index=0;index<items.length;index++){
          if(items[index][0]==msg){
            break;
          }
  }
        console.log("In is ",index);
        if(index==0){
          rank = 1;
          let j=index+2;
          let k = index + 1;
          let currentUserId = items[index][0];
          let nextUserId = items[k][0];
          console.log(nextUserId);
          let nextUserId2 = items[j][0];


          let currentUserTag = client.users.cache.get(currentUserId);
          let currentName = currentUserTag.username;
          let nextUserTag = async id => client.users.fetch(id)
          let nextUserName = nextUserTag.username;


          console.log(currentName);
          console.log(nextUserName);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement