|
| 1 | +--- |
| 2 | +id: blockbot |
| 3 | +aliases: |
| 4 | + - Blockbot |
| 5 | +tags: |
| 6 | + - webgroup |
| 7 | + - discord |
| 8 | +title: Blockbot |
| 9 | +--- |
| 10 | + |
| 11 | +# Blockbot |
| 12 | + |
| 13 | +Blockbot is a Discord bot, written in Python, that is maintained by the Redbrick Webgroup. This project uses [`hikari`](https://github.com/hikari-py/hikari/), an opinionated microframework, to interface with the Discord API. [`hikari-arc`](https://github.com/hypergonial/hikari-arc) is the command handler and [`hikari-miru`](https://github.com/hypergonial/hikari-miru) is the component handler of choice. |
| 14 | + |
| 15 | +## File Structure |
| 16 | + |
| 17 | +All bot files are under `src/`. |
| 18 | + |
| 19 | +- `bot.py` |
| 20 | + - Contains the bot configuration and instantiation (e.g. loading the bot extensions). |
| 21 | +- `extensions/` |
| 22 | + - Contains extensions (files) that are loaded when the bot is started. Extensions are a way to split bot logic across multiple files, commonly used to group different features. Extensions can contain plugins, command, event listeners and other logic. [Read more](https://arc.hypergonial.com/guides/plugins_extensions/). |
| 23 | +- `examples/` |
| 24 | + - Contains example extensions with commands, components and more as reference for developers. |
| 25 | +- `config.py` |
| 26 | + - Configuration secrets and important constants (such as identifiers) are stored here. The secrets are loaded from environment variables, so you can set them in your shell or in a `.env` file. |
| 27 | +- `utils.py` |
| 28 | + - Utility functions are stored here, that can be reused across the codebase. |
| 29 | + |
| 30 | +## Installation |
| 31 | + |
| 32 | +> [!TIP] |
| 33 | +> Docker Compose for local development is highly reccommended. This is similar to how it is deployed on Redbrick. |
| 34 | +
|
| 35 | +### Discord Developer Portal |
| 36 | + |
| 37 | +As a prerequisite, you need to have an application registered on the Discord developer portal. |
| 38 | + |
| 39 | +1. Create a Discord application [here](https://discord.com/developers/applications/). |
| 40 | +2. Go to *"OAuth2 > URL Generator"* on the left sidebar, select the `bot` and `applications.commands` scopes, and then select the bot permissions you need (for development, you can select `Administrator`). |
| 41 | +3. Go to the generated URL and invite the application to the desired server. |
| 42 | + |
| 43 | +#### Bot Token |
| 44 | + |
| 45 | +1. Open the application on the Discord developer portal. |
| 46 | +2. Go to *"Bot"* on the left sidebar, click `Reset Token`. |
| 47 | +3. Copy the newly generated token. |
| 48 | + |
| 49 | +### Source Code |
| 50 | + |
| 51 | +1. `git clone` and `cd` into the [blockbot repository](https://github.com/redbrick/blockbot). |
| 52 | +2. It is generally advised to work in a Python [virtual environment](https://docs.python.org/3/library/venv.html): |
| 53 | + |
| 54 | + ```sh |
| 55 | +python3 -m venv .venv |
| 56 | +source .venv/bin/activate |
| 57 | + ``` |
| 58 | + |
| 59 | +3. Rename `.env.sample` to `.env` inside the repo folder and fill in the environment variables with your secrets. e.g.: |
| 60 | + |
| 61 | + ``` |
| 62 | + TOKEN=<Discord bot token here> |
| 63 | + ``` |
| 64 | + |
| 65 | +4. Run `pip install -r requirements.txt` to install the required packages. |
| 66 | +5. Start the bot by running `python3 -m src`. |
| 67 | + |
| 68 | +### Docker |
| 69 | + |
| 70 | +1. `git clone` and `cd` into this repository. |
| 71 | +2. Create a new file called `.env` inside the repo folder and paste your bot token into the file as such: |
| 72 | +``` |
| 73 | +TOKEN=<Discord bot token here> |
| 74 | +``` |
| 75 | +3. Build the docker image `docker build --tag "blockbot-testing" .` |
| 76 | +4. Run this image `docker run "blockbot-testing"` |
| 77 | +
|
| 78 | +### Docker Compose |
| 79 | +1. `git clone` and `cd` into this repository. |
| 80 | +2. Create a new file called `.env` inside the repo folder and paste your bot token into the file as such: |
| 81 | +``` |
| 82 | +TOKEN=<Discord bot token here> |
| 83 | +``` |
| 84 | +3. Run the docker-compose.yml file `docker-compose up -d` |
| 85 | +
|
| 86 | +## Library Resources |
| 87 | +
|
| 88 | +- [`hikari` Documentation](https://docs.hikari-py.dev/en/latest/) |
| 89 | +- [`hikari` Examples](https://github.com/hikari-py/hikari/tree/master/examples) |
| 90 | +- [`hikari-arc` Documentation](https://arc.hypergonial.com/) |
| 91 | +- [`hikari-arc` Examples](https://github.com/hypergonial/hikari-arc/tree/main/examples/gateway) |
| 92 | +- [`hikari-miru` Documentation](https://miru.hypergonial.com/) |
| 93 | +- [`hikari-miru` Examples](https://github.com/hypergonial/hikari-miru/tree/main/examples) |
| 94 | +
|
| 95 | +## Usage Guides |
| 96 | +
|
| 97 | +### hikari |
| 98 | +
|
| 99 | +* [Getting Started](https://hg.cursed.solutions/#getting-started) - first steps of using `hikari` |
| 100 | +* [Events](https://hg.cursed.solutions/01.events/) - understanding receiving events from Discord with `hikari` |
| 101 | +
|
| 102 | +### hikari-arc |
| 103 | +
|
| 104 | +* [Getting Started](https://arc.hypergonial.com/getting_started/) - first steps of using `hikari-arc` |
| 105 | +* [Guides](https://arc.hypergonial.com/guides/) - various guides on aspects of `hikari-arc` |
| 106 | +
|
| 107 | +### hikari-miru |
| 108 | +
|
| 109 | +* [Getting Started](https://miru.hypergonial.com/getting_started/) - first steps of using `hikari-miru` |
| 110 | +* [Guides](https://miru.hypergonial.com/guides/) - various guides on aspects of `hikari-miru` |
| 111 | +
|
| 112 | +## What's the difference between `hikari`, `hikari-arc` and `hikari-miru`? |
| 113 | +
|
| 114 | +* `hikari` - the Discord API wrapper. Can be used to, for example: |
| 115 | + * [add roles to server members](https://docs.hikari-py.dev/en/stable/reference/hikari/api/rest/#hikari.api.rest.RESTClient.add_role_to_member) |
| 116 | + * [create threads](https://docs.hikari-py.dev/en/stable/reference/hikari/api/rest/#hikari.api.rest.RESTClient.create_thread) |
| 117 | + * [send individual messages](https://docs.hikari-py.dev/en/stable/reference/hikari/api/rest/#hikari.api.rest.RESTClient.create_message) |
| 118 | + * [fetch guild (server) information](https://docs.hikari-py.dev/en/stable/reference/hikari/api/rest/#hikari.api.rest.RESTClient.fetch_guild) |
| 119 | + * update member roles ([add role](https://docs.hikari-py.dev/en/stable/reference/hikari/api/rest/#hikari.api.rest.RESTClient.add_role_to_member), [remove role](https://docs.hikari-py.dev/en/stable/reference/hikari/api/rest/#hikari.api.rest.RESTClient.remove_role_from_member), [edit roles](https://docs.hikari-py.dev/en/stable/reference/hikari/api/rest/#hikari.api.rest.RESTClient.edit_member)) |
| 120 | + * listen for events from Discord, like [message edits](https://docs.hikari-py.dev/en/stable/reference/hikari/events/message_events/#hikari.events.message_events.MessageUpdateEvent) |
| 121 | +* `hikari-arc` - the command handler. Can be used to: |
| 122 | + * create application commands ([slash](https://arc.hypergonial.com/guides/options/#declaring-options), [message & user commands](https://arc.hypergonial.com/guides/context_menu/)) |
| 123 | + * respond to command interactions |
| 124 | +* `hikari-miru` - the component handler. Can be used to: |
| 125 | + * create message components (buttons, select menus & modals) |
| 126 | + * respond to component interactions (button clicks, select menu selections & modal submissions) |
| 127 | +
|
| 128 | +## Why use a `hikari.GatewayBot` instead of a `hikari.RESTBot`? |
| 129 | +
|
| 130 | +**TL;DR:** `RESTBot`s do not receive events required for some blockbot features (e.g. starboard), so `GatewayBot` must be used instead. |
| 131 | +
|
| 132 | +`GatewayBot`s connect to Discord via a websocket, and Discord sends events (including interactions) through this websocket. `RESTBot`s run a web server which Discord sends only interactions to (not events) via HTTP requests. These events are required for specific blockbot features, like starboard (which uses reaction create/remove events). |
| 133 | +
|
| 134 | +**Further reading:** <https://arc.hypergonial.com/getting_started/#difference-between-gatewaybot-restbot> |
| 135 | +
|
| 136 | +## What's the difference between `hikari.GatewayBot`, `arc.GatewayClient` and `miru.Client`? |
| 137 | +
|
| 138 | +* `hikari.GatewayBot` is the actual Discord bot. It: |
| 139 | + * manages the websocket connection to Discord |
| 140 | + * sends HTTP requests to the Discord REST API |
| 141 | + * caches information received in events sent by Discord |
| 142 | +* `arc.GatewayClient` adds additional functionality to `hikari.GatewayBot` for: |
| 143 | + * splitting the bot across multiple files using [extensions](https://arc.hypergonial.com/guides/plugins_extensions/#extensions) |
| 144 | + * grouping commands using [plugins](https://arc.hypergonial.com/guides/plugins_extensions/#plugins) |
| 145 | + * easily creating and managing commands and [command groups](https://arc.hypergonial.com/guides/command_groups/) |
| 146 | + * accessing objects globally (in any extension, plugin or command) using [dependency injection](https://arc.hypergonial.com/guides/dependency_injection/) (e.g. a database connection) |
| 147 | +* `miru.Client` adds additional functionality to `hikari.GatewayBot` for: |
| 148 | + * creating message components using [views](https://miru.hypergonial.com/getting_started/#first-steps) |
| 149 | + * creating [modals](https://miru.hypergonial.com/guides/modals/) |
| 150 | + * creating [navigators](https://miru.hypergonial.com/guides/navigators/) and [menus](https://miru.hypergonial.com/guides/menus/) using views |
| 151 | +
|
| 152 | +## Do's and Don'ts |
| 153 | +
|
| 154 | +* Always try to get data from the cache before fetching it from the API. |
0 commit comments