Ask coding questions

← Back to all posts
Hide Discord Bot Token in Python
cL3v3r47

I've been searching everywhere and everyone is talking about github or .js files.. I want to hide my bot token in Python, but my main.py says

import discord
from discord.ext import commands
import random
from discord import Permissions
from colorama import Fore, Style
import asyncio

token = ""

I can't hide my token, can anyone help me? cuz im new in coding and these stuff.

Voters
cL3v3r47
Comments
hotnewtop
Coder100

try using secrets: https://docs.replit.com/programming-ide/storing-sensitive-information-environment-variables

replit is different from other ones, not sure what they mean by .js files, but certainly .gitignore files don't work on repl.

ch1ck3n

go to the sidebar and click on the lock

add a new key called EPIC_BOT_TOKEN and then for the value add your token

now go to your code, and import os like import os

then do this:

token = os.getenv['EPIC_BOT_TOKEN']

it will not be accessible by anyone so no worries

cL3v3r47

@ch1ck3n

import discord
from discord.ext import commands
import random
from discord import Permissions
from colorama import Fore, Style
import asyncio
import os

token = os.getenv['EPIC_BOT_TOKEN']

It says:
Traceback (most recent call last):
File "main.py", line 9, in <module>
token = os.getenv['EPIC_BOT_TOKEN']
TypeError: 'function' object is not subscriptable

ch1ck3n

@cL3v3r47

oh i am sorry token = os.getenv('EPIC_BOT_TOKEN')

cL3v3r47

@ch1ck3n Thx very much :)

ch1ck3n

@cL3v3r47 if you click on the checkmark next to my name you can pay respects

SixBeeps

@ch1ck3n smh imagine asking for answered status

ch1ck3n

@SixBeeps it is buisness