消息未定义有问题-Discord JS

时间:2018-06-28 13:35:01

标签: javascript discord

我在尝试制造Discord机器人时遇到困难。我已经在机器人的实际功能上取得了进步,并已将其添加到服务器上,但是我在如何实际运行它方面遇到了问题。

我不断收到此错误:

if (message.content.includes("i'm home")) {message.channel.send(message = "Welcome home!");}
^

ReferenceError: message is not defined
    at Object.<anonymous> (C:\Users\Rick\Documents\CiscoBot\mybot.js:7:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

我并没有完全理解问题所在,特别是当考虑到我的代码(全部)时:

let Discord = require ("discord.js");

let client = new Discord.Client();

client.on("message", function(message) { let input = message.content()})
{
if (message.content.includes("i'm home")) {message.channel.send(message = "Welcome home!");}
}
{
if (message.content.includes("can we talk")) {message.channel.send(message = "Of course, what's the matter?");}
}
{
if (message.content.includes("that's it")) {message.channel.send(message = "Sorry to hear. Tomorrow is a new day!");}
}
{
if (message.content.includes("i need a hug")) {message.channel.send(message = "Gladly! [Hugs] :3");}
}
{
if (message.content.includes("whisper dirty things to me")) {message.channel.send(message = "[Whispers] The dishes are nasty. Clean them.");}
}
{
if (message.content.includes("i'm leaving now")) {message.channel.send(message = "Okay! See you later! :heart:");}
};
client.login("My App Token");

该如何解决?我对此非常陌生,真的想制造这个机器人。我决心花一点时间完成它。目的是在检测到带有自己短语的某些单词时做出响应。我已经修复了我所知道的语法错误,这只是目前的问题,只是“消息未定义”。

2 个答案:

答案 0 :(得分:0)

您应该将IF语句放在client.message函数的回调中。

client.on('message', function(message) {
  if (message.content.includes('i\'m home')) {
    message.channel.send(message = 'Welcome home!');
  }

  if (message.content.includes('can we talk')) {
    message.channel.send(message = 'Of course, what\'s the matter?');
  }

  if (message.content.includes('that\'s it')) {
    message.channel.send(message = 'Sorry to hear. Tomorrow is a new day!');
  }

  if (message.content.includes('i need a hug')) {
    message.channel.send(message = 'Gladly! [Hugs] :3');
  }

  if (message.content.includes('whisper dirty things to me')) {
    message.channel.send(message = '[Whispers] The dishes are nasty. Clean them.');
  }

  if (message.content.includes('i\'m leaving now')) {
    message.channel.send(message = 'Okay! See you later! :heart:');
  }
});
client.login("My App Token");

答案 1 :(得分:0)

一种更简单,更整洁的书写方式是这样的:

secret

请记住,这仅在message.content仅是您要查找的消息时才有效,例如:

示例1 玩家发送:“你好,我回来了!”。 这是行不通的。

示例2 玩家发送:“我回家”。 这将起作用。