Search code examples
pythondiscorddiscord.py

How do I run my discord.py bot without a hardcoded token?


I have been coding a discord bot for a while now, but have not figured out how to load my bot's token from somewhere else than by bot's python file. I'd want to use a text file (config.txt) for this, but anything of the sort works.

I have python 3.11.1 if that matters :D

imports blahblah
intents blahblah
...
client.run('token here')

Solution

  • Found a better solution on my own:

    to your imports add

    import json
    

    add this code

    with open("./config.json") as config:
      configData = json.load(config)
    token = configData["Token"]
    

    to the bottom

    client.run(token)
    

    this is the config.json

    {"Token": "token here"}