首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我需要有关名为“ReferenceError: message is not defined”的错误的帮助。

我需要有关名为“ReferenceError: message is not defined”的错误的帮助。
EN

Stack Overflow用户
提问于 2019-06-11 02:09:37
回答 2查看 50关注 0票数 0

下面是我的代码:

let xpAdd = Math.floor(Math.random() * 7) + 8;
console.log(xpAdd);
if (!xp[message.author.id]) {
  xp[message.author.id] = {
    xp: 0,
    level: 1
  };
}

下面是错误:

ReferenceError:未定义message

EN

回答 2

Stack Overflow用户

发布于 2019-06-11 02:15:32

传递给xp的键未定义,因此您需要先定义它

let xpAdd = Math.floor(Math.random() * 7) + 8
var message = {
  author:{
    id: //*whatever id you want to access*
  }
}
console.log(xpAdd);
if(!xp[message.author.id]){
xp[message.author.id] = {
  xp: 0,
  level: 1
};
}
票数 0
EN

Stack Overflow用户

发布于 2019-06-11 02:16:27

在您所展示的非常有限的代码中,还没有定义message

如果您的代码在您自己的函数中,请将message作为参数传递:

function addXP(message) {
  ...
}

addXP(message);

否则,您的代码应该在客户端的message事件回调中。在这种情况下,您可以将message声明为消息参数:

client.on('message', message => {  // Client may be defined as 'bot' instead of 'client'
  ...
});

MDN文档:

parameters

scope

functions

Discord.js文档:

message Event

Message Class

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56531449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档