Skip to content

helltf/helltfbot-v2

Repository files navigation

2nd version of my Twitchbot 🤖

Tables of Content

Add the bot to your channel

To add the bot to your channel, use the command

~join me

in any of the connected channels.

The bot should now trigger on sending commands in your own chat. If there is no response to your messages, you may need to allow sending messages by using another command in your own chat:

~allow

If the bot is still not responding, ask for help via Twitch or Discord (hell#9902).

Run the bot yourself

Clone the repository to your own local system:

git clone git@github.com:helltf/helltfbot-v2.git && cd helltfbot-v2

Create a .env file at the root of the newly created directory, which will be used to store sensitive and environment specific configuration options.

Copy the following snippet into that file and fill in your own values:

NODE_ENV=<env>
MAIN_USER=<your_username>
MAIN_USER_ID=<your_user_id>
BOT_CHANNEL=<default_channel>
DEBUG=<bool>
PREFIX=<bot_prefix>
REDIS_URL=<redis_db_url>
DB_USERNAME=<db_username>
DB_PASSWORD=<db_password>
DB_DATABASE=<db_table>
DATABASE_URI=<db_connection_uri>
TEST_DATABASE_URI=<test_db_connection_uri>
CLIENT_ID=<twitch_client_id>
CLIENT_SECRET=<twitch_client_secret>
TWITCH_OAUTH=<twitch_client_oauth>
SEVENTV_GQL_TOKEN= <7tv-Bearer>
ENCRYPT_KEY=<key_to_encrypt>
WEBHOOK_PORT=<port>
WEBHOOK_SECRET=<secret>
GITHUB_TOKEN=<api_token>
STARTUP_MESSAGE=[message]

Install all dependencies using yarn install and build the project with yarn build, then if nothing went wrong, you can now run the bot:

yarn start

You might encounter an error while querying the GitHub API, since you are not permitted to see details of GitHub Actions on my other projects.

About the bot

Chat Interaction

The bot will listen to every chat message starting with the prefix ~, there are rare exceptions where the bot might react to messages without that prefix. No response will be sent if the bot is not allowed to send messages.

Commands

Right now there is a small list containing all available commands. See here In the future there will be a documented list of commands on the associated website.

Every existing command has its own separate configuration and can be called either by its name or registered aliases. Some commands are exclusive to roles with higher permission levels.

Permissions

Each user has two different permission levels, of which one regulates the users database permissions and the other one his chat permissions. The level of access is indicated by a numeric value, the higher the number, the more access any given user has. Possible values at the moment are the following:

BLOCKED = -1
USER = 0
SUB = 1
VIP = 2
MOD = 3
BROADCASTER = 4
DEV = 5
ADMIN = 100

Access level for any user inside of the database always defaults to 0, while the chat permissions of a user are automatically set to either USER, SUB, VIP, MOD or BROADCASTER depending on his role in a given twitch chat. Therefore permissions of a single user can vary between different chats.
Users with the DEV or ADMIN role are permitted to execute most/all commands in any chat, while negative permissions are reserved for blocked users, preventing someone from interacting with the bot at all.

Development

Database

Mariadb was the initial database for this project but has been changed to postgres. Thus allowing to work with the diesel library in rust for the backend.

An additional Redis layer has been added in v1.2.0 to store very frequently accessed data in-memory.

CI-CD

Every push to the feature or master branches, as well as any pull request will trigger a workflow of 4 jobs, consisting of linting, building, unit-testing and integration-testing. Pull requests on feature or master are required to pass all checks before merging.

Testing (TDD)

My aim for this is towards building a maintainable and well-developed project, which is why I have chosen to try following test-driven development using Jasmine and cover as much of the code as possible with tests. At this point there are only unit and integration tests implemented, something that might change in the future. In version 1.4.0 Jasmine has been replaced by Jest, which will be the test framework for the future.

Unit Tests

All unit tests for this project are stored inside of ./test/unit. They are supposed to test small features/functions fairly quickly and without any connection to outside services and should be the preferred method for testing if possible.

Integration Tests

Integration tests are testing components and their connection to other services, e.g. a database. They are stored inside of ./test/integration. To test properly against a database I am running a MariaDB docker container, containing a suitable test database.

Important libraries

These are some of the important libraries used, changing how this project is developed or making it possible in the first place.

tmi.js

tmi.js is used to communicate with the twitch IRC server.

typescript

TypeScript provides strong typing for scalable JavaScript apps.

typeorm

TypeORM acts as an abstraction layer between the application and the database, to avoid writing manual SQL queries.

Jest

Jest is, as mentioned above, the test framework used for this project.

Related repositories

Website

The bot will have an associated website used for things like a command list. The repository contains a svelte app for a website.

Backend API

I am also working on a rust backend to provide an API for querying data collected by the bot.