javascript TypeError:无法读取未定义的属性'startsWith' - discord bot [英] javascript TypeError: Cannot read property 'startsWith' of undefined - discord bot

查看:181
本文介绍了javascript TypeError:无法读取未定义的属性'startsWith' - discord bot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须开始这个问题,说我对javascript(我在Java中练习)知之甚少,只想制作一个(有点)简单的Discord机器人,它会在随机时间说出消息。我将各种教程中的2段代码拼凑在一起,目前有这个:

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

此块出现问题:

 bot.on('message', (msg) => {

      if (msg.content.startsWith(prefix + "on")) {
            if (randOn) {
                msg.channel.sendMessage("Already running.");
            }

每当我尝试在我的不和谐聊天(!on)中命令机器人时,我在Node中得到错误TypeError:无法读取属性'startsWith'of undefined。 js /命令提示符。我已经尝试了各种方法来修复它(从msg.content ...语句中删除内容 - 没有抱怨,但绝对没有任何反应)但是...我真的不知道我在做什么我已经检查了互联网上处理类似事情的所有帖子,但没有任何东西能够回答这个问题。我希望这是一个简单的语法事物/没有正确陈述的东西....如果你有时间和遗憾我,请帮忙。我知道我已经把自己变成了一个人ss但我拒绝放弃它!
让我知道我可以提供哪些其他信息来帮助。

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. 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. 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.

推荐答案

你的问题是,你混合 discord.js discord.io

Your issue is, that you mix discord.js with discord.io

discord.js 是面向对象的,其中 discord.io 不是,所以在 discord.io 您的消息已经是一个字符串!

discord.js is object oriented where discord.io is not, so in discord.io your message is already a string!

示例discord.io消息事件。

Example discord.io message event.

bot.on('message', function(user, userID, channelID, message, event) {
    if (message === "ping") {
        bot.sendMessage({
            to: channelID,
            message: "pong"
        });
    }
});

这篇关于javascript TypeError:无法读取未定义的属性'startsWith' - discord bot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆