I placed a line called TOKEN into the .ENV file, so that the token of my Discord bot could be private.
When trying to reference process.env.TOKEN in a JSON file, it says that it had a token “p” that doesn’t belong. I’m not sure if I’m calling it wrong somehow, so I’ll include a reference below.
“token”: process.env.TOKEN,
If I’m doing it wrong, I’d appreciate a little guidance. Thanks in advance!
Hello
I’m not quite sure i understand what you, want. From what I understand you want to have a JSON
file containing your token, but the JSON
file should also remain private. This is possible by creating a directory called .data
and then put your JSON
in there. For an example you can have a look at my code below.
// <= config.js
// Dependencies
const fs = require('fs')
const path = require('path')
// Path to .data
const Data = path.join(__dirname, '.data')
// Path to config.json
const Config = path.join(Data, 'config.json')
// Verify that the .data directory exists,
// create it if it doesn't exist already.
if (!fs.existsSync(Data))
{
fs.mkdirSync(Data)
}
// Verify that the config.json file exists
// within the .data folder, create it if it
// doesn't already exists.
if (!fs.existsSync(Config))
{
fs.writeFileSync(Config, JSON.stringify({
token: ""
}))
}
// Import the config.
const config = require(Config)
// Export the config.
module.exports = config
And then in index.js
you could do something like this:
// <= index.js
// Dependencies
const Discord = require('discord.js')
// Imports
const config = require('./config')
// Verify the token
if (!config.token)
{
// Tell the user to edit the bot token.
console.log('Go to .data/config.json and add your token to it.')
// Exit the process, because there is
// no valid token to start the bot with.
}
// Create a new Discord Client
const Client = new Discord.Client()
// ... [code]
// Login with the token
Client.login(config.token)
.then(token => console.log('Token worked!')) // Successfully logged in
.catch(error => {
console.log('Failed to start the bot:', error.message) // Print some error message.
process.exit(0) // Exit, because the bot failed to start.
})
# Environment Config
# store your secrets and config variables in here
# only invited collaborators will be able to see your .env values
# reference these in your code with process.env.SECRET
TOKEN=
SECRET=
MADE_WITH=
# note: .env is a shell file so there can't be spaces
around =
the above is my env file, notice the field TOKEN which i left blank
{
"token": process.env.TOKEN,
"defaultprefix": "$>",
}
the above is a file i named “config.json” which tries to reference token but fails
I see that you are trying to reference a Node.js
global variable in your JSON file, which wont be possible because the JSON file is a static file that can store and contain data. However it is not able to dynamically call data as javascript can. JSON was made to hold and serve data from a file, it was not made as a scripting language. However you don’t need to store your token in your JSON file, instead just call process.env.TOKEN
where you’d usually call config.token
. For example in Client.login
Client.login(process.env.TOKEN)
// ... [CODE]
Okay, thank you for clearing that up!
No worries, feel free to ask me for help at any time.