Ask coding questions

← Back to all posts
Anyone know how to use .env?(discord.py)
tristandasavage

I'm trying to hide my token in a .env file so people can't get my bot's token, and I don't know how to get my token variable from the file.
ENV:
TOKEN=da token(not telling u lol)
PYTHON:
token = os.getenv("TOKEN")

I have a feeling that my code is wrong, but I don't know what the right code is.

Answered by AloegelhiPlaysR [earned 5 cycles]
View Answer
Voters
tristandasavage
Comments
hotnewtop
AloegelhiPlaysR

UH.. Delete secrets.env NOW. Your token is public right now! Make a new file called .env and do token=YOUR TOKEN

We have full access to it.

and yes, os.getenv("token") is correct.

tristandasavage
KHZ

@TristanMurphy You don't need to name the .env file. While adding a new file to the repl you just write as .env as the filename not vars.env.

AloegelhiPlaysR

Did I fix the issue? @TristanMurphy

AloegelhiPlaysR

Also please go here and regenerate your bot token! @AloegelhiPlaysR

tristandasavage

@AloegelhiPlaysR lol ik how to regen tokens

AloegelhiPlaysR

Just looking out for ya! @TristanMurphy

EpicRaisin

Yes, I know that this question has already been answered, but no one has explicitly explained why your file didn't work, so I'm gonna answer that.

So .env files, of course, can only be seen by the repl owner. When making a .env file, only name it .env. It's not supposed to be an extension name.

When you call for a variable from .env with os.getenv or os.environ.get, it'll look file a file called .env. So make sure the file is called that. Repl.it will automatically hide that file if it has the correct name.

luigirandoli

@EpicRaisin idk if you are still getting replies, but when i try and create a file thats only called this, it does not let me.

EpicRaisin

@luigirandoli Repl.it changed the system so now you don't need to make a file. Instead, you just go to the Secrets tab (the one with the lock) to make secrets, and it'll still work with the same code.

AloegelhiPlaysR

@TristanMurphy Here, join the team I invited you to. I'll help ya out for a sec. You'll be surprised.

tristandasavage

@AloegelhiPlaysR didnt get an invite

tristandasavage

@AloegelhiPlaysR btw is there a chance that you know how I can run it 24/7 using uptimerobot? I already have it configured in the dashboard, but I paused it so I could fix this. I noticed that when I run it, I get an error after it says updating discord.py:
Traceback (most recent call last):
File "main.py", line 169, in <module>
client.run(token)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 708, in run
return future.result()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 687, in runner
await self.start(args, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 650, in start
await self.login(
args, bot=bot)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 499, in login
await self.http.static_login(token.strip(), bot=bot)
AttributeError: 'NoneType' object has no attribute 'strip'

tristandasavage

lol nvm im an idiot and put DISCORD_BOT_TOKEN instead of DISCORD_BOT_SECRET which was in the variable lol

AloegelhiPlaysR
InvisibleOne
tristandasavage

@InvisibleOne lol I saw that and did it accordingly, but it didn't work when I did it that way

Coder100

It is already hidden. Only you can see it.