admin管理员组文章数量:1401613
I must start this question by saying that I have very little knowledge of javascript (I'm practiced in Java) and just wanted to make a (somewhat) simple Discord bot that would say messages at random times. I Frankensteined 2 pieces of code from various tutorials together and currently have this:
var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
//random bot code
var randomMessage;
var randOn = false;
var responseArray = [ //add more messages here
"Ayy, lmao!",
"Say what?",
"roflmaotntpmp"
];
var prefix = "!";
var timer = [5,10]; //set min and max in seconds for random messages
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', (msg) => {
if (msg.content.startsWith(prefix + "on")) {
if (randOn) {
msg.channel.sendMessage("Already running.");
}
else {
msg.channel.sendMessage("Random message started.")
randomMessage = setTimeout(function() {
randMsg(msg.channel);
}, 1000*timer[0]);
}
}
else if (msg.content.startsWith(prefix + "off")) {
if (randOn) {
clearTimeout(randomMessage);
msg.channel.sendMessage("Random message disabled.");
}
else {
msg.channel.sendMessage("Not running.");
}
}
});
function randomIntFromInterval(min, max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
function randMsg(msgChan) {
console.log("callback");
var interval = 1000*randomIntFromInterval(timer[0],timer[1]);
var rand = randomIntFromInterval(0,responseArray.length-1);
if(responseArray[rand]) {
msgChan.sendMessage(responseArray[rand]);
}
randomMessage = setTimeout(function() {
randMsg(msgChan);
}, interval);
}
The problem is occurring in this block:
bot.on('message', (msg) => {
if (msg.content.startsWith(prefix + "on")) {
if (randOn) {
msg.channel.sendMessage("Already running.");
}
Every time I attempt to mand the bot in my discord chat (!on) I get the error "TypeError: Cannot read property 'startsWith' of undefined" in Node.js/mand prompt. I've tried various things to fix it (removing "content" from both msg.content... statements - no plaints but absolutely nothing happens) but... I honestly have no idea what I'm doing. I've checked every post on the internet that deals with similar things and nothing has been able to answer this. I'm hoping it's a simple syntax thing/something not declared properly... If you have some time and pity for me, please help. I know I've gotten myself into a mess but I refuse to abandon it!
Let me know what other information I can provide to help.
I must start this question by saying that I have very little knowledge of javascript (I'm practiced in Java) and just wanted to make a (somewhat) simple Discord bot that would say messages at random times. I Frankensteined 2 pieces of code from various tutorials together and currently have this:
var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
//random bot code
var randomMessage;
var randOn = false;
var responseArray = [ //add more messages here
"Ayy, lmao!",
"Say what?",
"roflmaotntpmp"
];
var prefix = "!";
var timer = [5,10]; //set min and max in seconds for random messages
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', (msg) => {
if (msg.content.startsWith(prefix + "on")) {
if (randOn) {
msg.channel.sendMessage("Already running.");
}
else {
msg.channel.sendMessage("Random message started.")
randomMessage = setTimeout(function() {
randMsg(msg.channel);
}, 1000*timer[0]);
}
}
else if (msg.content.startsWith(prefix + "off")) {
if (randOn) {
clearTimeout(randomMessage);
msg.channel.sendMessage("Random message disabled.");
}
else {
msg.channel.sendMessage("Not running.");
}
}
});
function randomIntFromInterval(min, max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
function randMsg(msgChan) {
console.log("callback");
var interval = 1000*randomIntFromInterval(timer[0],timer[1]);
var rand = randomIntFromInterval(0,responseArray.length-1);
if(responseArray[rand]) {
msgChan.sendMessage(responseArray[rand]);
}
randomMessage = setTimeout(function() {
randMsg(msgChan);
}, interval);
}
The problem is occurring in this block:
bot.on('message', (msg) => {
if (msg.content.startsWith(prefix + "on")) {
if (randOn) {
msg.channel.sendMessage("Already running.");
}
Every time I attempt to mand the bot in my discord chat (!on) I get the error "TypeError: Cannot read property 'startsWith' of undefined" in Node.js/mand prompt. I've tried various things to fix it (removing "content" from both msg.content... statements - no plaints but absolutely nothing happens) but... I honestly have no idea what I'm doing. I've checked every post on the internet that deals with similar things and nothing has been able to answer this. I'm hoping it's a simple syntax thing/something not declared properly... If you have some time and pity for me, please help. I know I've gotten myself into a mess but I refuse to abandon it!
Let me know what other information I can provide to help.
Share Improve this question edited Aug 27, 2019 at 14:12 simhumileco 34.8k17 gold badges147 silver badges123 bronze badges asked Nov 16, 2018 at 2:56 lilslils 611 gold badge1 silver badge5 bronze badges 3-
what is the value of
msg
when the handler is called? can youconsole.log(msg)
it and share the output? – shkaper Commented Nov 16, 2018 at 3:09 - not sure if I did this right, but I placed "console.log(msg);" right before the start of the if statement containing msg.content...etc. when I ran the bot (node bot.js) and tried to use a mand in discord, it just printed my discord name in mand prompt right before it threw the error @shkaper – lils Commented Nov 16, 2018 at 3:16
- Put in a console.dir(msg) this will give you an exploded dump of the object, including all members and methods. Insert this after the line: bot.on('message', (msg) => { – SPlatten Commented Nov 16, 2018 at 10:00
2 Answers
Reset to default 2Your issue is, that you mix discord.js
with discord.io
discord.js
is object oriented where discord.io
is not, so in discord.io
your message is already a string!
Example discord.io message event.
bot.on('message', function(user, userID, channelID, message, event) {
if (message === "ping") {
bot.sendMessage({
to: channelID,
message: "pong"
});
}
});
Maybe jam something like if(!msg || !msg.content) return;
in there to bail out if the msg
object or its content
property is undefined.
bot.on('message', (msg) => {
if(!msg || !msg.content) return;
if (msg.content.startsWith(prefix + "on")) {
if (randOn) {
msg.channel.sendMessage("Already running.");
}
else {
msg.channel.sendMessage("Random message started.")
randomMessage = setTimeout(function() {
randMsg(msg.channel);
}, 1000*timer[0]);
}
}
else if (msg.content.startsWith(prefix + "off")) {
if (randOn) {
clearTimeout(randomMessage);
msg.channel.sendMessage("Random message disabled.");
}
else {
msg.channel.sendMessage("Not running.");
}
}
});
本文标签: nodejsJavaScript TypeError Cannot read property 39startsWith39 of undefineddiscord botStack Overflow
版权声明:本文标题:node.js - JavaScript TypeError: Cannot read property 'startsWith' of undefined - discord bot - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744317849a2600350.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论