# Self-hosting the bot

You can get a custom bot for free by self-hosting it, but keep in mind:

✅ Pros of Self-Hosting:

* Full control over your data and customization.
* No recurring subscription fees.
* Can be deployed on your own infrastructure.

⚠️ Cons of Self-Hosting:

* Requires technical expertise for setup and maintenance.
* You are responsible for updates, security, and backups.
* Hosting costs (servers, bandwidth, etc.) may apply.
* If hosting on a personal computer, it must remain powered on 24/7 for the bot to be available at all times, which also increases electricity costs.
* No official support beyond the provided setup instructions, or for specific additional software installation; self-troubleshooting is required.

For a hassle-free experience with automatic updates, security patches, and support, consider using our managed [custom bots!](https://docs.membercounter.app/readme/custom-bot)

## Recommendations <a href="#recommendations" id="recommendations"></a>

* Have patience and read carefully this guide
* Be familiarized with the CLI (cmd.exe, bash, zsh)
* If you already know how to use the CLI and you still need help, feel free to ask in this [discord server](https://membercounter.app/support/) in [#support](https://discord.com/channels/614777317733957632/1361732323686486106)

## Creating a bot account <a href="#creating-a-bot-account" id="creating-a-bot-account"></a>

Go to the [Discord Developer Portal](https://discord.com/developers/applications/), and click "New Application"

<figure><img src="https://1582170640-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lu2S5r7lKtu4IK5ucu9%2Fuploads%2Fgit-blob-274e3d97ed6886da72b5ccaf97c0cefe29d8edcd%2Foutput%20(13).png?alt=media" alt=""><figcaption><p>Discord Developer Portal Homepage</p></figcaption></figure>

A dialog box will appear asking for a name. Type the name of your new bot and click "Create"

<figure><img src="https://1582170640-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lu2S5r7lKtu4IK5ucu9%2Fuploads%2Fgit-blob-12c5581e4756da4d1e9c7cec7ec962948ddfcdfb%2Foutput%20(12).png?alt=media" alt=""><figcaption><p>Create Application Dialog Box</p></figcaption></figure>

Now let's configure your new Discord Bot

1. Head over the Bot tab
2. Enable the necessary privileged intents outlined, you can also upload the bot's icon and banner, or change its username
3. Save the changes
4. Click on "Reset Token", you will be prompted if you really want to proceed, click "Yes, do it!", the new token will be revealed, copy it and store it in a safe location
5. Head over the OAuth2 tab
6. Copy the client ID and store it in a safe location
7. Click on "Reset Secret", you will be prompted if you really want to proceed, click "Yes, do it!", the new secret will be revealed, copy it and store it in a safe location
8. Add another redirect URL
9. Paste `http://localhost:3000/api/auth/callback`. You can change this to fit your needs
10. Save the changes

<figure><img src="https://1582170640-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lu2S5r7lKtu4IK5ucu9%2Fuploads%2Fgit-blob-9c98e3234a5333c1e9fd49a19c46aa968b3c62f9%2Foutput%20(14).png?alt=media" alt=""><figcaption><p>Bot's Bot Tab</p></figcaption></figure>

<figure><img src="https://1582170640-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lu2S5r7lKtu4IK5ucu9%2Fuploads%2Fgit-blob-d954bb065d283f0149ba7c94ab423ddbff00faff%2Foutput%20(15).png?alt=media" alt=""><figcaption><p>Bot's OAuth2 Tab</p></figcaption></figure>

## **Install docker** <a href="#install-docker" id="install-docker"></a>

Download and install Docker for your OS: <https://docs.docker.com/engine/install/>

## Install git <a href="#install-git" id="install-git"></a>

Download and install git for your OS: <https://git-scm.com/download>

## Install Member Counter <a href="#install-member-counter" id="install-member-counter"></a>

Open your terminal (cmd.exe, Terminal.app, etc) and run this command to download Member Counter

{% tabs %}
{% tab title="HTTPS" %}

```bash
git clone --depth 1 --recurse-submodules --shallow-submodules -b master https://github.com/member-counter/bot.git
```

{% endtab %}

{% tab title="SSH" %}

```bash
git clone --depth 1 --recurse-submodules --shallow-submodules -b master git@github.com:member-counter/bot.git
```

{% endtab %}
{% endtabs %}

Now open the downloaded directory

```bash
cd bot
```

### Configuring Member Counter <a href="#configuring-member-counter" id="configuring-member-counter"></a>

Open the bot folder in your terminal and create a `.env` file from the `.env.example` file

```bash
cp .env.example .env
```

A new `.env` file will appear in the folder, open it with your favorite text editor (like notepad.exe, vim, vscode, etc)

{% hint style="info" %}
You might have to toggle the hidden files visibility in your file explorer to see this new file

macOS: <kbd>⇧ Shift</kbd> + <kbd>⌘ Cmd</kbd> + <kbd>.</kbd> while in Finder

Windows: <kbd>Alt</kbd>+<kbd>V</kbd>, then <kbd>H</kbd>, then <kbd>H</kbd> again, while in File Explorer

Linux: <kbd>Ctrl</kbd>+<kbd>H</kbd>, <kbd>Alt</kbd>+<kbd>.</kbd> in KDE
{% endhint %}

You will see a bunch of variables to set, but you only need to set the following block of variables at the start of the file:

* `COOKIE_SECRET`: Choose some 32 character long password for encrypting session cookies
* `DISCORD_CLIENT_ID`: Paste here your Discord Bot Client ID
* `DISCORD_CLIENT_SECRET`: Paste here your Discord Bot Secret
* `DISCORD_BOT_INSTANCE_TOKEN`: Paste your Discord Bot Token
* `WEBSITE_URL`: You will need to change this to allow using the website properly behind a reverse proxy or over other IP/hostname
* `DISCORD_OAUTH2_REDIRECT_URI`: You will need to change this to allow using the website properly behind a reverse proxy or over other IP/hostname

#### Optional configuration <a href="#optional-configuration" id="optional-configuration"></a>

In order to use some additional counters, you must set the following variables:

* `YOUTUBE_API_KEY` Paste here your YouTube API key, you can get it [here](https://console.developers.google.com)
* `TWITCH_CLIENT_ID` and `TWITCH_CLIENT_SECRET` Paste here your Twitch app credentials, you can get them [here](https://dev.twitch.tv/console/apps)
* `MEMERATOR_API_KEY` Paste here your [Memerator](https://memerator.me) API key, you can get it [here](https://memerator.me/api/auth), remember to check mark "Get Profiles - View Profiles."

### Prepare the database

```bash
docker compose up -d --scale bot=0 --scale website=0
```

#### Apply database migrations

```bash
docker compose run -T db-utils npm run db:migrate up
```

#### Apply the database schema

```bash
docker compose run -T db-utils npm run db:push
```

### Start Member Counter <a href="#start-member-counter" id="start-member-counter"></a>

Open again your terminal in the bot directory and use this command

```bash
docker compose up --attach website --attach bot
```

If you see a message like this, that means you have successfully installed Member Counter, else, it will give you errors that you must fix (stop the bot with <kbd>Ctrl</kbd>+<kbd>C</kbd> and check the previous steps)

```log
website  |   ▲ Next.js 14.2.3
website  |   - Local:        http://localhost:3000
website  |   - Network:      http://0.0.0.0:3000
website  | 
website  |  ✓ Starting...
website  |  ✓ Ready in 212ms
bot      | info: Bot starting...
bot      | info: Bot ready
bot      | info: Logged in as Member Counter#1234
bot      | info: Invite link: https://discord.com/oauth2/authorize?client_id=1234567890&permissions=269569044&scope=bot
bot      | info: Requesting members
bot      | info: Sending bot stats...
bot      | info: Bot stats has been sent
bot      | info: Members received
```

If the bot is working well, let's make the bot auto start when your PC or server boots up, focus your terminal window and hit <kbd>Ctrl</kbd>+<kbd>C</kbd> to stop the bot and then run it again in the background with this:

```bash
docker compose up -d
```

The bot will run in background, to stop it you can use

```bash
docker compose down


```
