OHSHIET
OHSHIET

Reputation: 11

Discord.js client.token returns "null" and doesn't work

I'm having a problem with discord.js: "client.token" doesn't work, even though it's provided in documentations. Well, I can just copy-paste the actual token like I did before, but let's say I want to make my code open-source. My bot is hosted on Heroku and I heard there was a way to hide your token and I even tried to do it, but seems like it doesn't work when you run your bot locally - only when bot is on hosting. And sometimes I just need to run my bot locally, but it's inconvenient to always change from Heroku hidden token to actual "string" token. Is there a universal way to hide one's token?

I've tried writing "client.user.token" (silly, but I had to try) and "var" instead of "const" (see the code below). Also tried to console.log it - the output was "null".

const token = client.token;

console.log(token); // "null"!

// ...some other code stuff

client.login(token);

The bot is expected to read the token and login, but I get this error: (node:2080) UnhandledPromiseRejectionWarning: Error: An invalid token was provided.

Upvotes: 1

Views: 2126

Answers (3)

user12042113
user12042113

Reputation:

You can use .env file which will keep your token hidden from anyone.

Steps:

  1. Open your bot folder.

  2. Make a new file in your code editor called ".env"

  3. Write: token: "INSERT TOKEN HERE"

  4. Open your main file and type in client.login function: "process.env.token" And that's pretty much it.

If you didn't understand something click me.

Sorry for this poor explanation, I'm kind-of tired so yeah just deploy it to Heroku and it'll work both locally and Heroku.

Upvotes: 0

Saddy
Saddy

Reputation: 1581

I think you are setting up your .env file wrong. It should be

KEY=variable

For example:

#.env
TOKEN=m1i1m312im

#app.js
token = process.env.TOKEN
client.login(token)

Upvotes: 0

Ethan Mitchell
Ethan Mitchell

Reputation: 602

The reason it is returning null is because you haven't provided a token in the first place, client.token returns the token the bot is actively logged into, which you need to provide. client.token is set when you run client.login() with your token found at your Discord developer portal

Upvotes: 2

Related Questions