Skip to main content

Initial files

Once you add your bot to a server, the next step is to start coding and get it online! Let's start by creating a .env file for your bot token and a main file for your bot application.

Creating configuration files

As explained in the "What is a token, anyway?" section, your token is essentially your bot's password, and you should protect it as best as possible. This can be done through a .env file, or by using environment variables.

Open your application in the Discord Developer Portal and go to the Bot page to copy your token.

Using environment variables

Environment variables are special values for your environment (e.g., terminal session, docker container, or environment variable file). You can pass these values into your code's scope so that you can use them.

note

When referring to a .env file, keep in mind that you can name this file whatever you prefer. For example, the file can be named token.env or secret.env.

Storing data in a .env file is a common way of keeping your sensitive values safe. Create a .env file in your project directory and paste in your token. You can access your token inside other files by using os.environ.

# Importing "os" module.
import os

# Getting .env value.
# You can name this variable in the script however you like.
YOUR_BOT_TOKEN = os.environ["YOUR_BOT_TOKEN"]
danger

If you're using Git, you should not commit this file and should ignore it via .gitignore. The values of a .env file are still accessible to anyone if they are able to view the file.

You can use the python-dotenv package to either load the env variables into the environment, or make a config dict out of the env values.

import os
from dotenv import load_dotenv

load_dotenv() # Take environment variables from .env.

# Using the variables in your application, which uses environment variables
# (e.g. from 'os.environ()' or 'os.getenv()')
# as if they came from the actual environment.
YOUR_BOT_TOKEN = os.environ["YOUR_BOT_TOKEN"]

Keep in mind that the values imported from the .env file are in string format. Therefore if you would like to, say, use them for calculations, you'll have to convert them via int()

Online editors (Glitch, Replit, etc.)

While we generally do not recommend using online editors as hosting solutions, but rather invest in a proper virtual private server, these services do offer ways to keep your credentials safe as well! Please see the respective service's documentation and help articles for more information on how to keep sensitive values safe:

Git and .gitignore

Git is a fantastic tool to keep track of your code changes and allows you to upload progress to services like GitHub, GitLab, or Bitbucket. While this is super useful to share code with other developers, it also bears the risk of uploading your configuration files with sensitive values!

You can specify files that Git should ignore in its versioning systems with a .gitignore file. Create a .gitignore file in your project directory and add the names of the files and folders you want to ignore:

__pycache__/
secrets.env
config.json
tip

__pycache__/ has been included in .gitignore as it is simply cache that helps loading and running your script faster (this is an oversimplification). As it is of no particular importance, and is recompiled every time a change is made in the script, it is better to not commit the directory.

Also, you can specify certain extensions/directories in .gitignore files, as per the requirements of your project - here is an example. Check out the Git documentation on .gitignore for more information!

Creating the main file

Open your code editor and create a new file. We suggest that you save the file as main.py or bot.py, but you may name it whatever you wish.

Here's the base code to get you started:

main.py
# Import the necessary libraries.
import disnake
from disnake.ext import commands

# Creating a commands.Bot() instance, and assigning it to "bot"
bot = commands.Bot()

# When the bot is ready, run this code.
@bot.event
async def on_ready():
print("The bot is ready!")


# Login to Discord with the bot's token.
bot.run("YOUR_BOT_TOKEN")

This is how you create a bot instance for your Discord bot and login to Discord. Open your terminal and run python3 main.py to start the process. If you see "The bot is ready!" after a few seconds, you're good to go!

tip

After closing the process with Ctrl + C, you can press the up arrow on your keyboard to bring up the latest commands you've run in the terminal. Pressing up and then enter after closing the process is a quick way to start it up again.

Resulting code

The code showcased in this section can be found on our GitHub repository here.