## Pre-read

- Any instructions specific for `wsl` will be marked with a bolded **WSL:**. These are not needed for a standard Linux environment, and are also generally imperfect.
- Anything that you need to replace with the appropriate value will be marked as `[value]`, where the text inside the brackets is what you need to put. EG:  `ls [home_dir]` would be `ls /`
- The folder that all work is done in will be further referenced as `~/ts`, if you picked a different location, remember to replace it with the appropriate directory
- A few distinctions to be made. There are two `docker-compose.yml` and `.env` file locations. They will be clarified as *environment*, (`~/ts/`, the location where the docker file and files for each of the docker containers (the bot, mongodb, postgres, and rabbitmq) are stored, and *bot* (`~/ts/discordBot/`).

## Preparation

- In the Linux environment, install `docker` (if you do not use Docker's repository, you may also need to install `docker-compose`). See [their documentation](https://docs.docker.com/engine/install/debian/) for more details on installation 
- Add your user to the docker group with `sudo usermod -aG docker [user]` or run all `docker` commands with `sudo`
- **WSL:** WSL does not have `systemd` support, so you need to manually start `dockerd`. I found it simplest to run `sudo dockerd` then open a new terminal window. This is fairly jank and it's recommended to find a better method. You can also attempt to start it and hide all output (be sure that sudo does not ask for a password prompt when doing this): `sudo dockerd & 1>/dev/null 2>/dev/null`
- **WSL:** I experienced issues storing the project on Windows-side storage, and found it worked best when storing the project on the Linux volume. (YMMV)


## Deployment

### Deploying the environment

1: Create a directory to be used as a working environment. This will be referred to as `~/ts`
```
mkdir ~/ts
```

2: Enter the environment:
```
cd ~/ts
```

3: Clone the `TechSupportBot` repository into `~/ts`:
```
git clone https://github.com/r-Techsupport/TechSupportBot 
```
(If you plan to contribute changes, it's recommended to fork the repository and make changes to the fork)

4: The *environment* `docker-compose.yml` file (to be created later) assumes the bot directory is named `discordBot`, so rename it or change the config file:
```
mv TechSupportBot discordBot
```

5: Set up the *environment*`.env` file for (`~/ts`) to look like below (these options can be configured, they should be if you plan to use this as a production environment):
```
RABBITMQ_DEFAULT_USER=root
RABBITMQ_DEFAULT_PASS=root
POSTGRES_DB_USER=root
POSTGRES_DB_PASSWORD=root
POSTGRES_DB_NAME=TechSupport
MONGO_DB_USER=root
MONGO_DB_PASSWORD=root
```
[source](https://discord.com/channels/749314018837135390/1058882783713444020/1059268094045999114)

6: Put the *environment* `docker-compose.yml` into `~/ts` :
```
wget -O docker-compose.yml https://cdn.discordapp.com/attachments/1058882783713444020/1059269470822072360/docker.compose1.yml
```
(If you download the file manually, remember it **must** be named `docker-compose.yml`)
[source](https://discord.com/channels/749314018837135390/1058882783713444020/1059269471140843560)
### Deploying the bot

7: Enter the `discordBot` directory:
```
cd discordBot
```

8: Copy `config.default.yml` to `config.yml`:
```
cp config.default.yml config.yml
``` 

9: Populate the config options using the values from the *environment* `.env` file, and Discord. 
	9a: It's assumed that you already made a Discord bot and added it to a server, so from the [Discord Developer Dashboard](https://discord.com/developers), select the application you created, and get the `Token` (found under the Bot submenu).
	9b: Take the token located in `9a` and fill in the `auth_token` field of `config.yml`.
	9c: Populate the `ids` field as shown below with your user ID (right click profile -> `Copy ID` with developer mode enabled)
Shown below is the minimum `config.yml` needed to start the bot, the rest of the fields can be populated as you see fit.
```
main:
    auth_token: "He3YtherE7HiS-isMy3xAmpLeaUtHtOkEN"
    admins:
        ids: ["1234576890945"]
        roles: []
    postgres:
        user: root
        password: root
        name: "tsbot"
        host: postgres
        port: 5432
    mongodb:
        user: root
        password: root
        name: "tsbot"
        host: mongodb
        port: 27017
    rabbitmq:
        user: root
        password: root
        host: "rabbitmq"
        vhost: "/"
        port: 5672
... (continued)
```
Remember to use the correct values if you modified the *environment* `.env` file. 

10: Create/remake a Docker image of the bot (this needs to be done after all changes to the bot's code):
```
make prod
```
This must be run from `discordBot`, ensure the operation completes **without any fatal errors**

### Start the bot

11: Enter the *environment* directory:
```
cd ~/ts
```

12: Start all containers:
```
docker-compose up -d
```

13: Ensure the bot is running correctly:
```
docker logs discordBot
```
(You may also get spammed by Discord PMs)

## Post deployment steps

Update your guild config:
```
.config patch
```
(by default all extensions are disabled, you may need to fill this out fully or borrow someone's)
