Reputation: 33
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. 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
I have thought maybe it is a firewall issue on my laptop, but I wanted to get other opinions.
Upvotes: 3
Views: 5668
Reputation: 710
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.
Upvotes: 1
Reputation: 5174
The error is caused because you're essentially doing message.content.toLowerCase().content.startsWith()
This should work:
if (!msg.startsWith(config.prefix) || msg.author.bot) {return;}
Upvotes: 2