# Initialize Discord Bot

## Preview

You will be creating the code that will make your bot startup and output a startup message.

## Coding

### Creating environment variables

You will need to create an [*environment variable*](https://sbdojo.gitbook.io/sushi-cards/2021-2022/session-1/definition-concept-guide#environment-variables) to house your Discord bot's token. This token is what tells the Discord API which Discord bot is yours. Keep your token a secret, as it can allow others to take control of your bot. This is why we will be storing it in an [*environment variable*](https://sbdojo.gitbook.io/sushi-cards/2021-2022/session-1/definition-concept-guide#environment-variables).

Head back to the Discord developer page where you created the bot. Under the *bot* tab click "COPY" under the token header.

Now head back to your Repl, and select the lock icon on the left side. This is where you can add, delete, or edit your environment variables. Set the key as "token" and set the value as the token you copied and select "Add New Secret".

![](https://5148195-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MgIjc7Q-SbTUf-t61Rv%2F-MinLMOgA9_l2yfcZ6N2%2F-MinahH0sx-HdmC5d9p2%2Fimage.png?alt=media\&token=1ddf3d38-c0ec-4ae1-8c6d-dd8175327e9a)

Now you will need to reference this token in your code. This is an example of a [*variable*](https://sbdojo.gitbook.io/sushi-cards/2021-2022/session-1/definition-concept-guide#variable).&#x20;

The word inside the brackets and quotes indicates the key of the environment variable. The os.environ will retrieve the token for the specified key and store it to the token variable.

```python
token = os.environ["token"]
```

### Creating a connection to the Discord [*API*](https://sbdojo.gitbook.io/sushi-cards/2021-2022/session-1/definition-concept-guide#api)

To create a connection to the Discord [*API*](https://sbdojo.gitbook.io/sushi-cards/2021-2022/session-1/definition-concept-guide#api) you need to create a client, this is what is doing the communicating. You can name "client" anything you want, but for now keep it as client as it will easier to follow along with the rest of the sushi card. Client will now be how you reference the connection, either to start it or to get information from it.

In between the parenthesis you can set specific settings for your client, such as the prefix for the command and if there is case sensitivity.

This is an example of a [*variable*](https://sbdojo.gitbook.io/sushi-cards/2021-2022/session-1/definition-concept-guide#variable).&#x20;

```python
client = commands.Bot(command_prefix = "!", case_insensitive=True)
```

### On ready

When the bot has finished setting up and connecting to the Discord [*API*](https://sbdojo.gitbook.io/sushi-cards/2021-2022/session-1/definition-concept-guide#api), it will fire an event. When using the @client.event handler, whenever an event happens, it will check the code following the event handler. If the event is defined as *onready* the code after that will fire, in this case, printing "The Discord bot is up!" to the console. There a bunch of different events that can happen, such as on\_message, on\_message\_delete, etc. You will be using events like these in the future.

```python
@client.event
async def on_ready():
    print("The Discord bot is up!")
```

### Starting the bot

Now we need to actually start the Discord bot. At the current moment it's not doing anything. Using the client [*variable*](https://sbdojo.gitbook.io/sushi-cards/2021-2022/session-1/definition-concept-guide#variable) we established before, we can start the bot by giving it the bot token, so Discord knows which bot to start up and what bot we are controlling. You can do this by adding the following code to the bottom of your Repl.

```python
client.run(token)
```

Now press the green run button on the top of your Repl. You should see "The Discord bot is up!" be printed into your console. Now if you look at your Discord server, the bot account should be online and on the right hand side of the your screen you should see something like this:

![This is your console. This is where messages you print will be shown.](https://5148195-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MgIjc7Q-SbTUf-t61Rv%2F-MinLMOgA9_l2yfcZ6N2%2F-MinbjW5oCvZ6QeVPZZA%2Fimage.png?alt=media\&token=961f3e74-00be-4d64-98ad-5f68287a1fb9)

![](https://5148195-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MgIjc7Q-SbTUf-t61Rv%2F-Mhyr5lfGxEEabXIDJds%2F-MhysBe5HcYPROEKVdgr%2Fimage.png?alt=media\&token=ee955329-2c49-41d2-a879-d4e2b7ca9388)

## Code Checkup

Your code should look something like the following at this point in the sushi card:

```python
import os
import discord
from discord.ext import commands

token = os.environ['token']

client = commands.Bot(command_prefix = "!", case_insensitive=True)

@client.event
async def on_ready():
    print("The Discord bot is up!")

client.run(token)
```

If your code is not like this or is not working, please read through the sushi card again OR ask a mentor for guidance.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sbdojo.gitbook.io/sushi-cards/2021-2022/session-1/discord-bot/day-1-setup/day-1-initialize-discord-bot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
