“错误:提供了无效的令牌。”即使设置了Heroku变量

时间:2019-05-01 08:28:45

标签: node.js heroku discord.js

我正在创建一个不和谐的bot,并尝试将其托管在Heroku中。安装所有依赖项后,我尝试在部署之前进行测试,但出现以下错误:

nicolasperez@nicolasperez-Nitro-AN515-51:~/Documents/DMGBOT$ heroku local
3:00:24 AM worker.1 |  > dmgbot@1.0.0 start /home/nicolasperez/Documents/DMGBOT
3:00:24 AM worker.1 |  > node bot.js
3:00:25 AM worker.1 |  (node:7624) UnhandledPromiseRejectionWarning: Error: An invalid token was provided.
3:00:25 AM worker.1 |      at Promise (/home/nicolasperez/Documents/DMGBOT/node_modules/discord.js/src/client/rest/RESTMethods.js:33:44)
3:00:25 AM worker.1 |      at new Promise (<anonymous>)
3:00:25 AM worker.1 |      at RESTMethods.login (/home/nicolasperez/Documents/DMGBOT/node_modules/discord.js/src/client/rest/RESTMethods.js:32:12)
3:00:25 AM worker.1 |      at Client.login (/home/nicolasperez/Documents/DMGBOT/node_modules/discord.js/src/client/Client.js:277:30)
3:00:25 AM worker.1 |      at Object.<anonymous> (/home/nicolasperez/Documents/DMGBOT/bot.js:194:5)
3:00:25 AM worker.1 |      at Module._compile (module.js:652:30)
3:00:25 AM worker.1 |      at Object.Module._extensions..js (module.js:663:10)
3:00:25 AM worker.1 |      at Module.load (module.js:565:32)
3:00:25 AM worker.1 |      at tryModuleLoad (module.js:505:12)
3:00:25 AM worker.1 |      at Function.Module._load (module.js:497:3)
3:00:25 AM worker.1 |      at Function.Module.runMain (module.js:693:10)
3:00:25 AM worker.1 |      at startup (bootstrap_node.js:188:16)
3:00:25 AM worker.1 |      at bootstrap_node.js:609:3
3:00:25 AM worker.1 |  (node:7624) 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(). (rejection id: 1)
3:00:25 AM worker.1 |  (node:7624) [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.
[DONE] Killing all processes with signal  SIGINT
3:00:25 AM worker.1 Exited Successfully

我不明白为什么会这样,因为我已经在Heroku的应用程序设置中设置了配置变量。

这是僵尸程序的代码:重要的一行是最后一行,该僵尸程序尝试使用有效令牌登录。

const botconfig = require("./botconfig.json");
const Discord = require("discord.js");
const Gamedig = require('gamedig');
const bot = new Discord.Client({disableEveryone: true});

bot.on("ready", async message => {...});

bot.on("message", async message => {...});

bot.login(process.env.BOT_TOKEN);

我已经尝试过多次重置机器人令牌,用引号将它们括起来,并在令牌中包含.json,但到目前为止没有任何效果。
为什么配置var不知道它们已设置为有效的discord bot令牌而无法工作?还有什么其他解决方案可以帮助我的机器人正常工作?

2 个答案:

答案 0 :(得分:0)

heroku local命令使应用程序在您的PC上运行:这意味着process.env中出现的环境变量就是您的PC,而不是您在Heroku中设置的环境变量。

为了将Heroku变量加载到本地实例中,您需要使用一个.env文件:Heroku将读取该文件,并将其插入环境中。您可以在Heroku的"Running Apps Locally"中找到有关此内容的更多信息。
"Copy Heroku config vars to your local .env file"部分中,还有有关如何将现有变量复制到.env文件中的指南。

答案 1 :(得分:0)

我相信您没有使用令牌保存文件。请保存所有文件,然后再次键入。希望对您有所帮助。