当我在github上上传代码时,如何隐藏我的discord bot令牌?
我正在编写这个discordbot,并希望它位于github上。如何在不公开其令牌的情况下上载它当我在github上上传代码时,如何隐藏我的discord bot令牌?,git,Git,我正在编写这个discordbot,并希望它位于github上。如何在不公开其令牌的情况下上载它 const Discord = require('discord.js'); const client = new Discord.Client(); // Following is the part that matter, the code before is just to get some context const token = '[hidden for security reason
const Discord = require('discord.js');
const client = new Discord.Client();
// Following is the part that matter, the code before is just to get some context
const token = '[hidden for security reasons]';
我想我应该把它放在一个单独的文件中,但我不知道如何在index.js文件中引用它
如何解决这个问题?有很多方法可以从代码中获取机密信息 最简单的方法可能是使用环境变量。 例如,您可以在代码中使用:
const token = process.env.DISCORD_TOKEN;
然后在调用脚本之前设置env变量(假设为bash shell):
DISCORD_TOKEN=1234foobar1234节点myscript.js
为简化此过程,您可以使用以下软件包:
然后,您可以在项目根目录中创建一个.env
文件:.env
:
DISCORD_TOKEN=1234foobar1234
(请记住将此文件添加到
.gitignore
以避免意外提交)它肯定在做它应该做的事情,但它一直说提供的登录凭据无效。知道为什么会发生这种情况吗?@innis如果您的凭据包含空格或其他有趣的字符,那么如果您运行console.log(process.env.DISCORD_令牌),您需要@innis代码>它是否输出正确的值?抱歉,我选错了令牌。它正在工作
DISCORD_TOKEN=1234foobar1234