/klima-discord-bots

Discord Bots

Primary LanguagePythonMIT LicenseMIT

discord-bots

Custom Discord Bots powering the KlimaDAO community.

This is a monorepo containing multiple bots.

Layout

src/

  • each folder corresponds to a specific bot

Setup

Each bot requires a separate application to be defined in the Discord Developer Portal. For each, do the following:

  1. Access the Discord developer portal: https://discord.com/developers/applications
  2. Create a new application: enter in the name of the bot (e.g. next-rebase)
  3. Create a bot: Click on "bot" in the sidebar and define a bot with the corresponding username.
    1. Check the "presence" and "server members" intents.
  4. Authorise the bot:
    1. Click on OAuth2 -> URL Generator in the sidebar.
    2. Select the "bot" scope.
    3. Copy the generated URL and open it.
    4. Select your server from the list and click on "Authorize". (You'll know that this has worked, as the bot user will appear in your Discord server.)
  5. Obtain the token for the bot: Settings -> Bot -> Token -> Copy

API Keys

This project requires some API keys to run. When developing locally, you can create your own API keys from the following services:

Deployment

The bots are hosted on Digital Ocean App Platform under a single app. Upon any commit to the main or staging branches, the bots are deployed automatically.

Before this, the app must be created. It can be done through the DigitalOcean App Platform web interface, or through the make create command.

Deployment follows this process:

  • A Docker image is built and pushed to DOCR using the Dockerfile.
    • Note: This Docker image contains the source code to the bots. Do NOT include any confidential or proprietary information in the repository or build artifacts.
  • The app is deployed to Digital Ocean, using the built Docker image as the basis.

To deploy manually, run:

DIGITALOCEAN_APP_ID=<INSERT ID> DIGITALOCEAN_ACCESS_TOKEN=<INSERT TOKEN> make deploy

Required Environment

GitHub Actions

The following environment variables must be defined in GitHub Actions:

These variables must also be defined, and will be used to replace variables in the app-spec.yml file:

  • POLYGONSCAN_API_KEY
  • WEB3_PROVIDER_ETH_URL
  • WEB3_PROVIDER_POLYGON_URL
  • DISCORD_BOT_TOKEN_REBASE
  • DISCORD_BOT_WEBHOOK_REBASE
  • DISCORD_BOT_TOKEN_KLIMA_PRICE
  • DISCORD_BOT_TOKEN_BCT_PRICE
  • DISCORD_BOT_TOKEN_MCO2_PRICE
  • DISCORD_BOT_TOKEN_STAKING_REWARDS

Digital Ocean App Platform

The above environment variables are used in the Digital Ocean App Platform environment. They are injected into the app-spec.yml file for the following reasons:

  • It is cumbersome to include the secret in the configuration file, deploy it and then copy/paste the encrypted value into the file again.
  • Updating secrets becomes cumbersome as well.

Instead, it is much easier to define and rotate secrets through the GitHub Actions secrets. We use the envsubst tool to achieve this.

Multiple Deployments

Each Docker image is tagged with the GitHub commit SHA, which prevents parallel deployments (main and staging) from clobbering each other.

In order for parallel deployments to work, however, the following must be implemented:

  • Define the "default" (non-production) environment variables for the GitHub repository. This ensures that any new branches will use non-production variables by default.
  • Create an environment called "main" and define any variables that differ from the default variables. At a minimum, this should include:
    • DIGITALOCEAN_APP_ID (or else different branches will be the same DigitalOcean app and clobber each other).
    • All of the DISCORD_BOT_* tokens (or else those bots will be clobbered).