|
1 |
| -# chdb-discord-bot |
2 |
| -Discord bot running chdb/ClickHouse queries, powered by Deta.space |
| 1 | +# Information |
| 2 | +This Discord bot is **SERVERLESS** which means it can run for **FREE** and be **ALWAYS online** on [Deta Space](https://deta.space)! |
| 3 | +You can also treat this repository as a template for making serverless bots with the [discohook](https://github.com/jnsougata/discohook) library. |
| 4 | + |
| 5 | +### Table of Contents |
| 6 | +- [Information](#information) |
| 7 | +- [Table of Contents](#table-of-contents) |
| 8 | +- [Features](#features) |
| 9 | +- [File Structure](#file-structure) |
| 10 | +- [Requirements](#requirements) |
| 11 | +- [Running Online](#running-online) |
| 12 | +- [Running Locally](#running-locally) |
| 13 | +- [Links and Resources](#links-and-resources) |
| 14 | + |
| 15 | +## Features |
| 16 | +- `/ping` - a simple command that tells you the bot's latency. |
| 17 | +- `/query <query> [format]` - a command that queries [chDB API](https://chdb.io) (ClickHouse). |
| 18 | +- A status message `Listening to /query! | chDB` via [scheduled actions](https://deta.space/docs/en/basics/micros#scheduled-actions). |
| 19 | +- And you can easily create and add more commands yourself! |
| 20 | + |
| 21 | +## File Structure |
| 22 | +``` |
| 23 | +. |
| 24 | +├─ src/ # Source code |
| 25 | +│ ├─ actions/ # Files used for scheduled actions |
| 26 | +│ │ └─ presence.py # Presence updater (bot status) |
| 27 | +│ ├─ assets/ # All asset files |
| 28 | +│ │ └─ logo.png # Logo used for space app |
| 29 | +│ ├─ commands/ # All command files |
| 30 | +│ │ ├─ query.py # Query command |
| 31 | +│ │ └─ ping.py # Ping command |
| 32 | +│ ├─ micros/ # Micro files; each file is a micro |
| 33 | +│ │ ├─ bot.py # /bot route, contains the discohook bot |
| 34 | +│ │ └─ main.py # / route, contains the actions handler |
| 35 | +│ └─ utils/ # Contains any extra utility files |
| 36 | +│ │ └─ helpers.py # Useful functions |
| 37 | +├─ .gitignore # Hides certain files |
| 38 | +├─ Discovery.md # Defines app's space discovery page |
| 39 | +├─ LICENSE # License |
| 40 | +├─ README.md # Defines this README page |
| 41 | +├─ Spacefile # Space app configuration |
| 42 | +├─ example.env # Example of an .env file |
| 43 | +└─ requirements.txt # Library dependencies |
| 44 | +``` |
| 45 | + |
| 46 | +## Requirements |
| 47 | +- **Discord Application:** Create an app for **FREE** at [Discord Developer Portal](https://discord.com/developers/applications). |
| 48 | +- **Deta Space account:** Create an account for **FREE** at [Deta Space](https://deta.space/), username + password. |
| 49 | +- **chDB API URL:** Use the public service, or point at your HTTP/S chDB/ClickHouse compatible API.. |
| 50 | +- [**discohook**](https://github.com/jnsougata/discohook): A github library used to make async serverless Discord bots. |
| 51 | +- [**deta**](https://github.com/jnsougata/discohook): A github library used to make async [Deta Space's Base HTTP API](https://deta.space/docs/en/reference/base/HTTP) requests. |
| 52 | + - The database is only used to store the websocket resume data for the status message. |
| 53 | +- [**uvicorn**](https://pypi.org/project/uvicorn/): A PyPI library used to run an ASGI webserver. |
| 54 | +- [**python-dotenv**](https://pypi.org/project/python-dotenv/): A PyPI library used to help load variables from an `.env` file. |
| 55 | + |
| 56 | +## Running Online |
| 57 | +1. Install the space app from the [app's discovery page](https://deta.space/discovery/@imp1/chatgpt). |
| 58 | + Alternatively you could build the space app yourself: |
| 59 | + 1. [Clone](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) this repository. |
| 60 | + 2. Install the [Space CLI](https://deta.space/docs/en/basics/cli). |
| 61 | + 3. Make sure you're in the project folder: `$cd <folder>` |
| 62 | + 4. Create a space app: `$space new` |
| 63 | + 5. Push the space app: `$space push` |
| 64 | +2. Enter the environment variables (Space App Settings ➔ Configuration). |
| 65 | + - `DISCORD_APPLICATION_ID` - Your discord app's ID. |
| 66 | + - `DISCORD_PUBLIC_KEY` - Your discord app's public key. |
| 67 | + - `DISCORD_BOT_TOKEN` - Your bot's token. |
| 68 | + - Other environment variables are optional. |
| 69 | +3. Set `Interactions Endpoint URL` to `<micro_url>/bot/interactions`. |
| 70 | + - This is located in: `https://discord.com/developers/applications/{application_id}/information` |
| 71 | + - A Micro URL looks like this: `https://chatgpt-1-a1234567.deta.app` |
| 72 | +4. Visit `<micro_url>/bot/api/dash/<discord_bot_token>` to register the slash commands for the first time. |
| 73 | +5. Run `/ping` to make sure it's working! Enjoy! |
| 74 | + |
| 75 | +## Running Locally |
| 76 | +You only need to run the bot locally if you plan to **develop new commands** for the bot. |
| 77 | +This is because `$space push`-ing each time would make development take forever. |
| 78 | +1. [Clone](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) this repository. |
| 79 | +2. Install the [Space CLI](https://deta.space/docs/en/basics/cli). |
| 80 | +3. Make sure you're in the project folder: `$cd <folder>` |
| 81 | +4. Install the library dependencies. |
| 82 | + 1. Make a virtual environment: `$python -m venv venv` |
| 83 | + 2. Enter the virtual environment: `$source venv/bin/activate` |
| 84 | + 3. Install requirements: `$pip install -r requirements.txt` |
| 85 | + 4. To leave the virtual environment later run `$deactivate`. |
| 86 | +5. Rename `example.env` to `.env` file and update its contents. |
| 87 | + - Environment variables in comments are optional. |
| 88 | +6. Run `$space dev` to start both `main` and `bot` micros. |
| 89 | +7. In another terminal, start a reverse proxy/tunnel because your `https://localhost` can't be accessed by Discord. |
| 90 | + - **Via [Deta Space](https://deta.space)**: |
| 91 | + - Run `space reverse proxy` and the URL is your micro's URL: `https://<name>-1-a1234567.deta.app` |
| 92 | + - If you have a main bot and a test bot, use `space link` to switch to a test space app. |
| 93 | + - **Via [Ngrok](https://ngrok.com)**: |
| 94 | + - [Setup Ngrok](https://ngrok.com/docs/getting-started). Create an account, install CLI, set auth-token. |
| 95 | + - Run `ngrok http 4200` to get a URL like `https://a1b2-34-567-890-123.ngrok-free.app`. |
| 96 | + - Note the Free Tier has a ratelimit of 60 requests per minute and URLs are ephemeral. |
| 97 | + - **Via [Cloudflare](https://cloudflare.com)**: |
| 98 | + - [Setup Cloudflare](https://developers.cloudflare.com/pages/how-to/preview-with-cloudflare-tunnel). Install CLI and optionally link an account. |
| 99 | + - Run `cloudflared tunnel --url https://localhost:4200` to get a URL like `https://ab-quick-brown-fox.trycloudflare.com`. |
| 100 | + - No request ratelimit (AFAIK) but URLs are still ephemeral. |
| 101 | + - Note it's technically against their ToS to host anything other than basic HTML pages on the free plan. |
| 102 | + - Use this for development only if you need a higher request ratelimit. |
| 103 | + - **List of [other solutions](https://github.com/anderspitman/awesome-tunneling)**. |
| 104 | + - For all the above, you can do `CTRL+C` to stop them. |
| 105 | +8. Set the `Interactions Endpoint URL` to `<url>/bot/interactions`. |
| 106 | + - This is located in: `https://discord.com/developers/applications/{application_id}/information` |
| 107 | + - The URL is from the previous step, for space it's this: `https://<name>-1-a1234567.deta.app` |
| 108 | +9. Finally you can now start live editing. |
| 109 | + Uvicorn is set to `--reload` so any edits you make automatically restarts the webserver. |
| 110 | +10. To stop running do `CTRL+C`. |
| 111 | + |
| 112 | +When you're ready, you can run `$space push` to update the space app. |
| 113 | + |
| 114 | +## Links and Resources |
| 115 | +- **Deta Space Documentation:** https://deta.space/docs |
| 116 | +- **Deta Discord:** https://discord.gg/deta |
| 117 | +- **Discohook Discord:** https://discord.gg/xEEpJvE9py |
| 118 | +- **Discord API Documentation:** https://discord.com/developers/docs |
| 119 | +- **Space App Discovery Page:** https://deta.space/discovery/@imp1/chatgpt |
0 commit comments