[英]node.js/discord.js: TypeError: Cannot read property 'setPresence' of null
Trying a simple test, but I'm getting an error from this line: client.user.setPresence('game', { type: 'PLAYING' });尝试一个简单的测试,但我从这一行得到一个错误:client.user.setPresence('game', { type: 'PLAYING' });
VSCode's logs: VSCode 的日志:
C:\Users\dvmvged\Documents\mao>node . whoa.js
C:\Users\dvmvged\Documents\mao\whoa.js:9
client.user.setPresence('game', { type: 'PLAYING' });
^
TypeError: Cannot read property 'setPresence' of null
at Object.<anonymous> (C:\Users\dvmvged\Documents\mao\whoa.js:9:13)
←[90m at Module._compile (internal/modules/cjs/loader.js:956:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:812:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:724:14)←[39m
←[90m at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)←[39m
←[90m at internal/main/run_main_module.js:17:11←[39m
whoa.js哇.js
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log("Logged in.");
client.login("token");
});
client.user.setPresence('test', { type: 'PLAYING' });
package.json: package.json:
{
"name":"test-bot",
"version":"1.0.0",
"description":"simple test",
"main":"whoa.js",
"dependencies":{
"discord.js":"^11.5.1"
},
"devDependencies":{
},
"scripts":{
"test":"echo \"Error: no test specified\" && exit 1"
},
"author":"no one",
"license":"ISC"
}
NPM version: 6.11.3 NPM 版本:6.11.3
Node.js version: 12.12.0 Node.js 版本:12.12.0
Discord.js version: 11.5.1 Discord.js 版本:11.5.1
EDIT: Just to let you know, I tried.setActivity and.setGame.编辑:只是为了让你知道,我试过.setActivity 和.setGame。 same error.
同样的错误。
I think what's happening here is a consequence of the Node.js event loop.我认为这里发生的事情是 Node.js 事件循环的结果。 You call an asynchronous function,
.login()
, and immediately attempt client.user.setPresence()
before the response from .login()
arrives.您调用异步 function,
.login()
,并在.login()
的响应到达之前立即尝试client.user.setPresence()
。
Try尝试
.login("token")
.then(client.user.setPresence("game", { type: "PLAYING" }));
See Node Event Loop请参阅节点事件循环
.user
has not yet been populated when you called him.当您呼叫他时,
.user
尚未填充。 I'm gonna annotate the order at which things happen here.我要注释这里发生的事情的顺序。
const Discord = require('discord.js');
const client = new Discord.Client();
// #1
client.on('ready', () => {
// #3
console.log("Logged in.");
client.login("token");
});
// #2
client.user.setPresence('test', { type: 'PLAYING' });
You only have a guarrantee of.user existing after the client is ready.在客户端准备好后,您只有一个 .user 存在的保证。
Whenever you use a callback in node (not just in node,).每当您在节点中使用回调时(不仅仅是在节点中)。 you pass a function as an argument to something.
您将 function 作为参数传递给某些东西。
That callback may be scheduling that function to run on a later time.该回调可能正在安排function 稍后运行。 On this specific case case, it schelules the function which has
console.log
to run on a future event of the client "being ready".在这个特定的案例中,它安排了 function,它有
console.log
在客户端“准备好”的未来事件上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.