简体   繁体   中英

Property of 'lang' cannot be read

I am trying to make a text to speech discord bot. I'm more of a newbie to coding so I don't exactly understand what is going on here. The code I wrote is for executing a simple command called 'lang' which will switch the TTS language.

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

The error I get is:

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)

Can someone help me out?

I suppose problem is that variable ttsPlayer is null. Please check if such property is present at message.guild?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM