无法读取 discord.js 中未定义的属性“缓存”

时间:2021-05-17 14:33:42

标签: javascript node.js discord.js

我正在编写一些代码来分析用于衡量毒性的消息(使用 Perspective API),并为此采取行动。我需要它的日志。我有下面的代码来测量所有消息和日志结果而不采取任何行动,但它给出了这个错误:

Cannot read property 'cache' of undefined

这是给出错误的代码的一部分:

const {google} = require('googleapis');
const fs = require('fs');

client.on('message', (msg) => {
  if(msg.author.id === client.user.id) return;
  API_KEY = process.env['PERSPECTIVE_API_KEY'];
  DISCOVERY_URL = 'https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1';
   google.discoverAPI(DISCOVERY_URL)
     .then(client => {
        const analyzeRequest = {
          "comment": {
           "text": msg.content,
          },
          "requestedAttributes": {
            "TOXICITY": {},
          },
          "doNotStore": true
        };
         client.comments.analyze(
            {
             key: API_KEY,
             resource: analyzeRequest,
            },
            (err, response) => {
              if (err) throw err;
              let raw_analyzeResult = JSON.stringify(response.data, null, 2)
              const analyzeResult = Math.round(JSON.parse(raw_analyzeResult).attributeScores.TOXICITY.summaryScore.value * 100)
              console.log("Message: " + msg.content + "\nSent by: <@" + msg.author.id + ">\n Toxicity Level: %" + analyzeResult);
              client.channels.cache.get("836242342872350790").send("Message: " + msg.content + "\nSent by: <@" + msg.author.id + ">\n Toxicity Level: %" + analyzeResult)
            });
      })
     .catch(err => {
        throw err;
      });
});

另外,这是我的变量部分:

let discord = require("discord.js")
let client = new discord.Client()
let prefix = ";"

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您正在混合两个 client 变量。您将一个用于机器人 (let client = new discord.Client()),另一个用于 Google API (.then(client => {)。从 client 返回的 discoverAPI 没有 channels 属性,只有机器人有。

由于 client.channels 未定义,您收到错误“无法读取未定义的属性 'cache'”。

尝试重命名其中之一,例如google.discoverAPI().then((discoveryClient) => ...)

client.on('message', (msg) => {
  if (msg.author.id === client.user.id) return;

  API_KEY = process.env['PERSPECTIVE_API_KEY'];
  DISCOVERY_URL = 'https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1';

  google
    .discoverAPI(DISCOVERY_URL)
    .then((discoveryClient) => {
      const analyzeRequest = {
        comment: { text: msg.content },
        requestedAttributes: { TOXICITY: {} },
        doNotStore: true,
      };
      discoveryClient.comments.analyze(
        {
          key: API_KEY,
          resource: analyzeRequest,
        },
        (err, response) => {
          if (err) throw err;
          let raw_analyzeResult = JSON.stringify(response.data, null, 2);
          const analyzeResult = Math.round(
            JSON.parse(raw_analyzeResult).attributeScores.TOXICITY.summaryScore
              .value * 100,
          );
          console.log(
            'Message: ' +
              msg.content +
              '\nSent by: <@' +
              msg.author.id +
              '>\n Toxicity Level: %' +
              analyzeResult,
          );
          client.channels.cache
            .get('836242342872350790')
            .send(
              'Message: ' +
                msg.content +
                '\nSent by: <@' +
                msg.author.id +
                '>\n Toxicity Level: %' +
                analyzeResult,
            );
        },
      );
    })
    .catch((err) => {
      throw err;
    });
});