无法读取“ lang”的属性

时间:2020-03-04 22:25:52

标签: javascript node.js discord.js

我正在尝试将文字转换为语音不和谐机器人。我是编码的新手,所以我不太了解这里发生了什么。我编写的代码用于执行一个名为“ lang”的简单命令,该命令将切换TTS语言。

const prefix = process.env.prefix;
const languages = require('../../data/languages.json');

module.exports = {
  name: 'lang',
  description: 'Change the TTS language.',
  emoji: ':map:',
  execute(message, options) {
    let [newLang] = options.args;
    const { ttsPlayer } = message.guild;

    if (!newLang) {
      message.reply(`to set-up the TTS language, run: **${prefix}lang <lang_code>**
      To see a list of the available lang codes, run: **${prefix}langs**.
      The current language is set to: **${languages[ttsPlayer.lang]}**.`);
      return;
    }

    newLang = newLang.toString().toLowerCase();

    ttsPlayer.setLang(newLang)
      .then((setLang) => {
        message.reply(`language has been set to **${setLang}**.`);
      })
      .catch((error) => {
        message.reply(error);
      });
  }
}

我得到的错误是:

2020-03-04T22:04:23.278707+00:00 app[worker.1]: [31m(10:04:23 PM) - [ERROR] - TypeError: Cannot read property 'lang' of null
2020-03-04T22:04:23.278709+00:00 app[worker.1]:     at Object.execute (/app/src/commands/lang.js:15:63)
2020-03-04T22:04:23.278710+00:00 app[worker.1]:     at executeCommand (/app/src/common/utils.js:46:13)
2020-03-04T22:04:23.278710+00:00 app[worker.1]:     at Object.handleMessage (/app/src/events/handlers/app.js:50:3)
2020-03-04T22:04:23.278711+00:00 app[worker.1]:     at Client.<anonymous> (/app/index.js:33:55)
2020-03-04T22:04:23.278711+00:00 app[worker.1]:     at Client.emit (events.js:323:22)
2020-03-04T22:04:23.278712+00:00 app[worker.1]:     at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
2020-03-04T22:04:23.278713+00:00 app[worker.1]:     at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
2020-03-04T22:04:23.278715+00:00 app[worker.1]:     at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
2020-03-04T22:04:23.278715+00:00 app[worker.1]:     at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:435:22)
2020-03-04T22:04:23.278715+00:00 app[worker.1]:     at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我想问题是变量ttsPlayer为空。 请检查message.guild中是否存在此类属性?