Skip to content

TripSit/TripBot

Repository files navigation

TripBot 🤖
(TripSit-Discord-Bot)

Table of contents

About

TripBot is multi-platform open-source bot that is a major part of the TripSit project. It is primarily a discord bot, but has been built modularly to allow for easy expansion to other platforms such as IRC, Matrix, and Telegram. This bot's code is open source in the interest of helping people learn code, and to encourage people to contribute to the project.

The goals with this bot are:

  1. Provide for the needs of TripSit's community such as drug information, harm reduction, and community support.
  2. Spread access to TripSit's Harm Reduction information by encouraging other Discord guilds to use our bot.
  3. Get traffic back to our discord, ultimately to gather more information that can be spread further and help more people!

Invite

  • We run an instance of the bot that you can add to your guild following this link

Development

Want to help out?

Contributing to TripBot is super appreciated, and it's easy to get involved: Join our Discord guild and check out the Development sections to get started.

Features

Fully Typescript Postgres Relational Database Prisma ORM and Client Discord.js Jest Testing SonarQube Code Quality Docker Containerized ESlint Linting Nodemon Hot Reloading TSC Auto Building Drone Continuous Integration

How to build

Requirements

  1. Docker. You can get it here.
  2. A discord bot application. You can get one here.

Optional

  1. VSCode. You can get it here.
  2. ESLint extension for VSCode. You can get it here.
  3. Invite the discord bot to the tripsit dev guild. Ask Moonbear for an invite link. The bot will not work properly without doing this.

Setup the bot

Clone the repository

git clone https://github.com/TripSit/TripBot.git

Copy env.example to .env and fill in these fields. You only /need/ the discord stuff like the token and client ID

Install the dependencies locally so that VSCode can use them for linting and intellisense.

npm install

Start the bot

This will start up the tripbot_database, build the bot, and run the bot in a container. The database container just basically sits there being a stable database, you don't need to check it's logs or anything.

npm run tripbot:start

You will likely want to immediately check the logs of the container to make sure it started up correctly.

View logs

npm run tripbot:logs

This will show you the logs of tripbot from inside the container.

Deploy commands

This command will happen automatically when you build the TripBot container and tells discord what commands are available.

npm run tripbot:deployCommands

If you create a new command, or modify the options in an existing command, you'll need to deploy commands again.

Develop

The container will watch the /src folder for changes and rebuild the bot when you save a file.

Lint

It's /highly/ recommended to use the eslint extension, and allow vscode the auto-fix your linting errors. Jest tests are a WIP.

npm run tripbot:lint

Test

Jest tests are a WIP and while appreciated it's not required to add tests to your code

npm run tripbot:test

Folder Structure

src/         : This is where all the code lives
src/api      : External API endpoints, see "API" below
src/api/v1   : Legacy json endpoint
src/api/v2   : New prisma postgres endpoint
src/discord  : Discord specific code, see "How commands work" below
src/docker   : Dockerfiles for the various containers
src/global   : Global functions that are used by multiple commands. See "How commands work" below
src/irc      : IRC specific code, see "How commands work" below.
src/jest     : Jest testing code.
src/matrix   : Matrix specific code, see "How commands work" below.
src/pgadmin4 : PGAdmin4 webserver, used in database development, see "Database Development" below.
src/postgres : Postgres startup script, used in database development, see "Database Development" below.
src/prisma   : Prisma database code, see "Database Development" below.
src/telegram : Telegram specific code, see "How commands work" below.

How Commands Work

Most commands are built with a global core function, and then have a UI function that interacts with that global function. For example: We have the /birthday command on discord. When someone runs the /birthday set command on discord, they enter their birth month and day. When they hit enter, d.birthday gets the user's input values (month and day), and sends them to g.birthday g.birthday talks to the database and sets the values, and tells d.birthday if it succeeded or not d.birthday then responds to the user that they successfully set their birthday

This might seem complicated, but this allows us to re-use commands depending on the service that calls them. All m.birthday needs to do is take input from the user however they can from the Matrix side, and send the same standard values to g.birthday. If you wanted to do this in telegram, you would figure out how to take input values from users in telegram, and then send those to g.birthday.

+---------+ +------------+ +------------+ +-------------+ +---------+ | Discord | | | | | | | | Matrix | | User | <---> | d.birthday | <---> | g.birthday | <---> | m.birthday | <---> | User | | | | | | | | | | | +---------+ +------------+ +------------+ +-------------+ +---------+

Database Development

TripBot's database runs on Postgres, which is a relational database, not a JSON file.

When TripBot starts, it will automatically create the tripbot_database container and run the migrations.

This gives you a copy of the production table schema on your local machine.

To make changes to the database, you need to use the Prisma ORM and make changes to the schema.prisma file.

Run the db:migrateDev to create new migration files based on the changes you made to the schema.prisma file.

When these migration files are pushed to production, the database will be updated automatically.

Prisma documentation is excellent, check out guides like: https://www.prisma.io/docs/concepts/components/prisma-migrate/migrate-development-production

Postgres

npm run postgres

We use postgres for our database.

This is just a simple container that basically sits there being a stable database.

By itself, it does nothing, you need another utility, like Prisma, to interact with it.

PG Admin

npm run pgadmin

This is a webserver that allows you to view the database.

It's not necessary, but it's nice to have.

You can access it via http://localhost:80 (maybe idk)

API

npm run api

This is a simple API that allows you to interact with the database.

V1 : legacy tripbot APi that interacts with static .json files

V2 : the new prisma api that interacts with postgres for the appeal system

About

The discord bot on TripSit.Me

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published