TypeError:无法读取未定义的node.js V12.18.2的属性“ startsWith”

时间:2020-07-13 23:34:52

标签: javascript node.js discord.js

这是我的第一个问题,因此,如果我错过一些信息/格式,我深表歉意。 我无法理解为什么Node.js不能在笔记本电脑上正常工作,而在另一台计算机上也可以正常工作。这是我的代码:

const Discord = require('discord.js');
const client = new Discord.Client();
const config = require("./config.json");
const random = require("./random.js");
const constants = require("./constants.js");
const Sequelize = require('sequelize');


//Listener event for when the bot sends a ready event (ex: logging in)
client.on('ready', () => {
     console.log(`Logged in as ${client.user.tag}!`);
 });

 //Listener Event which runs whenever a message with prefix '!' is received
client.on('message', message => {

     var msg = message.content.toLowerCase(); //Turns message to uppercase, to make commands not case sensitive
     var sender = message.author; //Author of the message is set to the sender
     
     console.log(msg);
     if (!msg.content.startsWith(config.prefix) || msg.author.bot) {return;}

     if (msg.content.startsWith(config.prefix + 'pray')) {
          msg.reply('Your prayers have been heard... Lord Gaybel is pleased!'); 
     }
    
     if(msg.content.startsWith(config.prefix + "fortune")) {
           msg.reply('The fortune trumpet is not function currently... we are working hard to make sure that your fortunes are told.');
           msg.reply(random.getRandomInt(constants.MIN, constants.MAX));
     }
 });


//Discord Login Token
client.login(config.TOKEN);

这是我得到的错误:

c:\Users\Curtis\Documents\GitHub\MABEL-BOT\main.js:38
     if (!msg.content.startsWith(config.prefix) || msg.author.bot) {return;}
                      ^

TypeError: Cannot read property 'startsWith' of undefined
    at Client.<anonymous> (c:\Users\Curtis\Documents\GitHub\MABEL-BOT\main.js:38:23)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (c:\Users\Curtis\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (c:\Users\Curtis\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (c:\Users\Curtis\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (c:\Users\Curtis\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (c:\Users\Curtis\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (c:\Users\Curtis\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (c:\Users\Curtis\node_modules\ws\lib\websocket.js:797:20)

我在计算机和NPM v 6.14.6上都使用Node.Js V 12.18.2

我曾经以为这可能是笔记本电脑上的防火墙问题,但我想征询其他意见。

2 个答案:

答案 0 :(得分:1)

由于您实际上是在做message.content.toLowerCase().content.startsWith()

而导致错误

这应该有效:

if (!msg.startsWith(config.prefix) || msg.author.bot) {return;}

答案 1 :(得分:1)

代替 if (!msg.content.startsWith(config.prefix),应该是 if (!msg.startsWith(config.prefix) ||...,因为您之前已经在执行步骤 var msg = message.content.toLowerCase();