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

Posted

技术标签:

【中文标题】“错误:提供了无效的令牌。”即使设置了 Heroku 变量【英文标题】:"Error: An invalid token was provided." even if Heroku variables are set 【发布时间】:2019-09-19 20:37:11 【问题描述】:

我正在创建一个不和谐的机器人,并尝试在 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,但到目前为止没有任何效果。 为什么配置变量知道它们设置为有效的不和谐机器人令牌而无法工作?还有哪些其他解决方案可以帮助我的机器人正常工作?

【问题讨论】:

asymc 函数应该返回一个承诺,我在你的代码中没有看到这样做。先尝试解决这个问题,要么返回一个承诺,要么干脆不使用async 【参考方案1】:

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

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

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于“错误:提供了无效的令牌。”即使设置了 Heroku 变量的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 mongoDB 托管 mongoid.yml 的 uri 设置无法正常工作?

为啥即使设置了 Effects = DragDropEffects.None 也允许放置?

Go build:“找不到包”(即使设置了 GOPATH)

Meteor:即使设置了 ANDROID_HOME 也没有设置

即使我设置了写入权限,也无法写入 C:\ 中的文件

我没有设置 CORS 标头,即使它已设置。为啥?