【问题标题】:Have an issue with message not being defined - Discord JS未定义消息时遇到问题 - Discord JS
【发布时间】:2018-07-02 03:37:37
【问题描述】:

我在尝试制作 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");

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

【问题讨论】:

  • 您尚未在message.channel.send() 的调用中定义消息变量。尝试将其更改为message.channel.send("Your text here")

标签: javascript discord


【解决方案1】:

你应该把你的 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");

【讨论】:

  • 你的变量输入没用
  • 不是我的:) - 作者的:)
  • 谢谢 simpleDmitry!我现在没有弹出任何错误,并且未定义的消息似乎已经消失。目前,它似乎会起作用。再次感谢您的帮助!
猜你喜欢
  • 2020-09-17
  • 2020-11-07
  • 2021-08-12
  • 2020-11-17
  • 2021-04-29
  • 2021-12-26
  • 2012-12-07
  • 1970-01-01
  • 2021-10-28
相关资源
最近更新 更多