简体   繁体   English

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

[英]TypeError: Cannot read property 'startsWith' of undefined node.js V12.18.2

This is my first question on here so I apologize if I miss some information/formating.这是我在这里的第一个问题,所以如果我错过了一些信息/格式,我深表歉意。 I am unable to understand why my Node.js doesn't seem to work on my laptop, but works perfectly fine on another computer.我无法理解为什么我的 Node.js 似乎无法在我的笔记本电脑上运行,但在另一台计算机上运行良好。 This is my code:这是我的代码:

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

This is the error I get:这是我得到的错误:

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)

I am using Node.Js V 12.18.2 on both computers and NPM v 6.14.6我在两台计算机上都使用 Node.Js V 12.18.2 和 NPM v 6.14.6

I have thought maybe it is a firewall issue on my laptop, but I wanted to get other opinions.我认为这可能是我笔记本电脑上的防火墙问题,但我想获得其他意见。

The error is caused because you're essentially doing message.content.toLowerCase().content.startsWith()错误是因为你本质上是在做message.content.toLowerCase().content.startsWith()

This should work:这应该有效:

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

Instead of if (.msg.content.startsWith(config.prefix) , it should be if (.msg.startsWith(config.prefix) ||... , since you are already performing the step var msg = message.content.toLowerCase(); previously.而不是if (.msg.content.startsWith(config.prefix) ,它应该是if (.msg.startsWith(config.prefix) ||... ,因为您已经在执行步骤var msg = message.content.toLowerCase();以前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取未定义 Discord.js node.js v12.16.3 的属性“第一个” - TypeError: Cannot read property 'first' of undefined Discord.js node.js v12.16.3 Node.js和jdbc:TypeError:无法读取undefined的属性&#39;url&#39; - Node.js and jdbc : TypeError: Cannot read property 'url' of undefined 类型错误:无法读取 Node.js 和 puppeteer 中未定义的属性“匹配” - TypeError: Cannot read property 'match' of undefined in Node.js and puppeteer Node.js和mongodb:TypeError:无法读取未定义的属性&#39;findAll&#39; - Node.js and mongodb: TypeError: Cannot read property 'findAll' of undefined “类型错误:无法读取未定义的属性‘状态’”node.js - "TypeError: Cannot read property 'status' of undefined" node.js TypeError:无法读取未定义的属性“getProperty”? Node.js,傀儡师 - TypeError: Cannot read property 'getProperty' of undefined? Node.js, Puppeteer Node.js TypeError:无法读取未定义的属性“作者” - Node.js TypeError: Cannot read property 'author' of undefined 类型错误:无法读取未定义的属性“findAll” - Node.js + Postgresql - TypeError: Cannot read property 'findAll' of undefined - Node.js + Postgresql JavaScript(discord.js)TypeError:无法读取未定义的属性“startsWith” - JavaScript(discord.js) TypeError: Cannot read property 'startsWith' of undefined Node.js 抛出 TypeError:无法读取未定义的属性“testModule” - Node.js throwing TypeError: Cannot read property 'testModule' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM