首页 > 解决方案 > 对不和谐机器人的编码感到困惑

问题描述

当我正确完成所有操作并正确编写脚本时,当我放置 node 时就会出现。在命令提示符下我什至有一个有效的令牌!!::

(node:8744) UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided. at Client.login (C:\Users\User\Documents\GitHub TigerDuo\TigerDuo\node_modules\discord.js\src\client\Client.js:204:52) at Object.<anonymous> (C:\Users\User\Documents\GitHub TigerDuo\TigerDuo\index.js:47:5) at Module._compile (internal/modules/cjs/loader.js:1133:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10) at Module.load (internal/modules/cjs/loader.js:977:32) at Function.Module._load (internal/modules/cjs/loader.js:877:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47 (node:8744) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=严格(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:8744) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这是我的脚本,更不用说我设置了heroku和github:

const Discord = require('discord.js');
const bot = new Discord.Client();

const token = 'MyNeverToBeSharedToken';

bot.on('ready', () => {
    console.log('Online.')
    bot.user.setActivity('the Tiger Duo server!', {
        type: "WATCHING"
    })
    bot.login(process.env.token);

标签: botsdiscord

解决方案


尝试安装 dotenv 包

require("dotenv").config()

并将其添加到代码的顶部


推荐阅读