0

Attached below is my code. It works the first time it is run but not the second time.

const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
    intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages,         GatewayIntentBits.MessageContent],
});
const axios = require('axios');
const PREFIX = "!";


client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});


client.on('messageCreate', async (message) => {
  if (message.content.startsWith(PREFIX)) {
    const [command, username] = message.content
      .substring(PREFIX.length)
      .split(' ');

    if (command === 'id') {
      try {
        // Clear the cache before making the API request
        client.users.cache.clear();

        const lowercaseUsername = username.toLowerCase();
        const response = await fetch(`https://users.roblox.com/v1/users/search?    keyword=${encodeURIComponent(lowercaseUsername)}`);
        const { data } = await response.json();
        if (data.length === 0) {
          message.channel.send(`Could not find a user with the name "${username}".`);
        } else {
          const { id } = data[0];
          console.log(`The user "${username}" has the Roblox ID ${id}.`);
          message.channel.send(`The user "${username}" has the Roblox ID ${id}.`);
        }
      } catch (error) {
         console.error(error);
         message.channel.send(`There was an error searching for the user    "${username}".`);
      }
    }
   }
 });


client.login('Bot_Token');

It runs successfully the first time and display the roblox user's id, however the second time it just gives the error.

FrostZer0
  • 11
  • 3

1 Answers1

1

I tested your code, and I think the error is due to you are clearing the cache before you make the API call.

Because you clear the cache for all users, not just the user you are trying to look up, so when you are trying to look up the same user again, the cache will still be empty and you will receive an error.

Instead you should cache the user data after the first API call, a fix here:

const userCache = new Map();

client.on('messageCreate', async (message) => {
  if (message.content.startsWith(PREFIX)) {
    const [command, username] = message.content
      .substring(PREFIX.length)
      .split(' ');

    if (command === 'id') {
      try {
        let id;
        const lowercaseUsername = username.toLowerCase();

        if (userCache.has(lowercaseUsername)) {
          id = userCache.get(lowercaseUsername);
        } else {
          const response = await axios.get(`https://users.roblox.com/v1/users/search?    keyword=${encodeURIComponent(lowercaseUsername)}`);
          const { data } = response.data;
          if (data.length === 0) {
            message.channel.send(`Could not find a user with the name "${username}".`);
            return;
          } else {
            id = data[0].id;
            userCache.set(lowercaseUsername, id);
          }
        }

        console.log(`The user "${username}" has the Roblox ID ${id}.`);
        message.channel.send(`The user "${username}" has the Roblox ID ${id}.`);
      } catch (error) {
         console.error(error);
         message.channel.send(`There was an error searching for the user "${username}".`);
      }
    }
  }
});
Bryce Chan
  • 1,639
  • 11
  • 26
  • Hi! I tried this code out and now I get a status code 429 error with axios. Any advice? It's saying it's a bad request. – FrostZer0 May 02 '23 at 01:27