[英]JavaScript TypeError: Cannot read property 'startsWith' of undefined - discord bot
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. 我必须以说我对javascript的知识很少(我是用Java进行实践的)来开始这个问题的,只是想制作一个(有点)简单的Discord机器人,该机器人可以在随机时间发出消息。 I Frankensteined 2 pieces of code from various tutorials together and currently have this:
我弗兰肯斯坦(Frankenstein)将来自各种教程的2段代码放在一起,目前有:
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 command the bot in my discord chat (!on) I get the error "TypeError: Cannot read property 'startsWith' of undefined" in Node.js/command prompt. 每次尝试在不和谐聊天(!on)中命令bot时,都会在Node.js /命令提示符下收到错误“ TypeError:无法读取未定义的属性'startsWith'”。 I've tried various things to fix it (removing "content" from both msg.content... statements - no complaints but absolutely nothing happens) but... I honestly have no idea what I'm doing.
我已经尝试了各种方法来修复它(从msg.content ...语句中删除“内容” ...语句-没有投诉,但绝对没有发生任何事情)但是...老实说,我不知道我在做什么。 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. 让我知道我可以提供什么其他信息以帮助您。
Your issue is, that you mix discord.js
with discord.io
您的问题是,您将
discord.js
与discord.io
混合使用
discord.js
is object oriented where discord.io
is not, so in discord.io
your message is already a string! discord.js
是面向对象的,而discord.io
不是,因此,在discord.io
您的消息已经是字符串!
Example discord.io message event. 示例discord.io消息事件。
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;
也许卡住类似
if(!msg || !msg.content) return;
in there to bail out if the msg
object or its content
property is undefined. 如果
msg
对象或其content
属性未定义,则在其中进行救援。
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."); } } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.