简体   繁体   English

node.js/discord.js:类型错误:无法读取 null 的属性“setPresence”

[英]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.

相关问题 使用 NPM、node.js 和 discord.js,我不断收到 TypeError: Cannot read property 'setPresence' of Z37A6259CC0C1DAE0294 - Using NPM, node.js and discord.js, I keep getting TypeError: Cannot read property 'setPresence' of null 类型错误:无法读取 null discord.js v13 的属性“setPresence” - TypeError: Cannot read property 'setPresence' of null discord.js v13 Node.js Discord.js UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“calculatedPosition” - Node.js Discord.js UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'calculatedPosition' of undefined 类型错误:无法读取未定义 Discord.js node.js v12.16.3 的属性“第一个” - TypeError: Cannot read property 'first' of undefined Discord.js node.js v12.16.3 JavaScript,Discord.js,Node.js TypeError:无法读取未定义的属性“执行” - JavaScript,Discord.js, Node.js TypeError: Cannot read property 'execute' of undefined Discord.js | 类型错误:无法读取 null 的属性“禁令” - Discord.js | TypeError: Cannot read property 'ban' of null Discord.js:Linkblocker TypeError:无法读取 null 的属性“角色” - Discord.js: Linkblocker TypeError: Cannot read property 'roles' of null Discord.JS | TypeError:无法读取null的属性“运行” - Discord.JS | TypeError: Cannot read property 'run' of null Discord.js 类型错误:无法读取 null 的属性“setActivity” - Discord.js TypeError: Cannot read property 'setActivity' of null 类型错误:无法读取 null 的属性“用户”| discord.js - TypeError: Cannot read property 'user' of null | discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM