NodeJS 无法读取undefined(阅读'length')discord.js的属性

1u4esq0p  于 2023-05-06  发布在  Node.js
关注(0)|答案(1)|浏览(136)

下面是我的代码。它在第一次运行时有效,但在第二次运行时无效。

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');

它第一次运行成功并显示roblox用户的id,但第二次它只是给出错误。

laik7k3q

laik7k3q1#

我测试了你的代码,我认为错误是由于你在调用API之前清除该高速缓存。
因为您清除了所有用户该高速缓存,而不仅仅是您试图查找的用户,所以当您试图再次查找同一用户时,缓存仍然是空的,并且您将收到错误。
相反,你应该在第一次API调用后缓存用户数据,这里有一个修复:

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}".`);
      }
    }
  }
});

相关问题