/cogBot

A discord bot for federals!

Primary LanguagePythonBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Federal Discord Bot

Github Actions Gitlab Build Status Coverage

Overview

This bot is designed to facilitate Powerplay for the federal forces. The bot runs and monitors a discord channel and responds to commands.

Invite bot to server and grant following permissions to it in a channel:

  • Read/Send messages
  • Manage messages (will cleanup bad inocations)
  • Embed Links (embeds are used for !whois command)

Github vs Gitlab

There is a github repo and a gitlab repo. While the project started on github it will continue on gitlab at present and github will remain a mirror. If people want to make forks and discuss issues that's fine but PRs are likely to be done on gitlab.

Donate To Project

If you'd like to help cover server costs or buy me a coffe for development, see these buttons.

Buy Me A Coffee Donate using Liberapay

Install

This bot requires python >= 3.7. I suggest using pyenv to provide python and isolate from system.

  • Install pyenv however you prefer
  • sudo apt-get install libsqlite-dev
  • pyenv install 3.7.10
  • pyenv local 3.7.10 (in project root)

Now for project dependencies (you can use mysql instead of maria)

  • sudo apt-get install python3-dev libffi-dev libczmq-dev libczmq3mariadb-server mariadb-client apt-transport-https ca-certificates curl gnupg`
  • python setup.py deps

NB: libczmq3 may not be available on some systems. Not to be confused with similarly named zmq kernel messaging.

Next is the database. At a bare minimum you require a running mysql/mariadb instance with at least 1 user. That user must have access to a database named dev. This is the default database. The database used is tied to the TOKEN set in os.environ on startup. See below.

To complete installation, you need to fill the data directory with configuration files from separate dev kit. Inside the main config you will have to edit the information about connecting to your local db (user/pass). There may be additional details in the dev kit, please read them.

TOKEN Explained

The TOKEN environment variable does two main things. Let us consider the example:

TOKEN=live python -m cog.bot

  1. Selects the local database to use. In this case, the mysql driver will login locally and select live database.
  2. Tells bot on startup what bot account to login to discord with. This is important. In the config.yml file there is a key called discord. Under this key, is a series of key/value pairs. In our example, the running bot would log in with the discord token under the 'live' key.

If TOKEN variable doesn't exist, default is 'dev'. I suggest you use that to start with. The main reason token also selects a different database is to allow side by side execution of the bot.

Running Bot

To run the main bot do as follows. Take note of above, you'll be implying TOKEN=dev by default.

python -m cog.bot

Note that the scheduler depends on the web/app.py to be running and accepting requests from the hooked sheets. The bot can be run without it locally, you just won't get notified when sheet changes are made.

See web/app.py for more details.

Layout

Just a brief commentary on layout/modules.

  • cog: All control logic for the bot. See cog/bot.py
  • cogdb: All database code. Do not hard code SQL.
  • data: The data directory, contains all the configs. Do not commit secrets like your config.yml
  • extras: Catchall for leftovers, also generated UML diagrams.
  • tests: All test code mirrors the layout of the cog/cogdb. See conftest.py for fixtures.
  • web: The simple Flask app handling POSTs lives here.

Setup.py

This file has some useful commands. Notably it manages the dependencies and allows the project to be packaged for pip.

UML Diagrams UML diagrams of the overall modules and class diagrams are available. They are generated by library and dumped in extras/*.png.

python setup.py uml

Database ER Visualisation

Please install and use DBeaver. After setup of bot, import of eddb and running it once, just connect and see database tabs for ER.

Install Dependencies

python setup.py deps

Show Test Coverage

python setup.py coverage

Clean Project Note: This will remove all pyc objects, tox cache and other useless files.

python setup.py clean

Running Tests

Full Test Suite

tox

Only Tests Matching A String If py.test not available on the command line, do python -m pytest.

py.test -k test_inara

Run Parts Of Tox Tox runs 3 tests on code base. Pytest for unit tests, pylint and flake8 for static linting. The last two can't fail, some errors I ignore especially some linting problems in 'tests/'

tox -e flake8