chore: use alternative ways to bundle

This commit is contained in:
PalmDevs
2024-07-04 21:02:01 +07:00
parent ebf1ac7c08
commit 9b6ba56d99
50 changed files with 443 additions and 399 deletions

View File

@@ -1,18 +1,22 @@
# ⚙️ Configuration
This page tells you how to configure the bot.
## 📄 JSON config
See [`config.ts`](../config.ts).
---
### `config.owners`
#### `config.owners`
User IDs of the owners of the bot. Only add owners when needed.
### `config.guilds`
#### `config.guilds`
Servers the bot is allowed to be and register commands in.
### `config.logLevel`
#### `config.logLevel`
The level of logs to print to console. If the level is more important or equally important to set level, it will be forwarded to the console.
@@ -26,14 +30,42 @@ The possible levels (sorted by their importance descendingly) are:
- `log`
- `debug`
### `config.api.websocketUrl`
#### `config.api.url`
The WebSocket URL to connect to (including port). Soon auto-discovery will be implemented.
WebSocket URL to connect to (including port). Soon auto-discovery will be implemented.
### `config.messageScan`
#### `config.api.disconnectLimit`
Amount of times to allow disconnecting before exiting with code `1`.
#### `config.messageScan`
[Please see the next page.](./2_adding_autoresponses.md)
#### `config.moderation`
TBD.
#### `config.rolePresets`
TBD.
## 🟰 Environment variables
See [`.env.example`](../.env.example).
You can set environment variables in your shell or use a `.env` file which **Bun will automatically load**.
---
#### `DISCORD_TOKEN`
The Discord bot token.
#### `DATABASE_URL`
The database URL, since we're using SQLite, we'll be using the `file` protocol.
Example values are: `file:./revanced.db`, `file:./db.sqlite`, `file:./discord_bot.sqlite`
## ⏭️ What's next
The next page will tell you how to configure auto-responses.

View File

@@ -86,4 +86,4 @@ filterOverride: {
The next page will tell you how to run and bundle the bot.
Continue: [🏃🏻‍♂️ Running the bot](./3_running.md)
Continue: [🏃🏻‍♂️ Running the bot](./3_running_and_deploying.md)

View File

@@ -1,24 +0,0 @@
# 🏃🏻‍♂️ Running the bot
There are two methods to run the bot. Choose one that suits best for the situation.
## 👷🏻 Development mode (recommended)
There will be no compilation step, and Bun will automatically watch changes and restart the bot for you.
You can quickly start the bot by running:
```sh
bun dev
```
## 📦 Building
There's unfortunately no way to build/bundle the bot yet due to how dynamic imports currently work, though we have a few ideas that may work.
As a workaround, you can zip up the whole project, unzip, and run it in development mode using Bun.
## ⏭️ What's next
The next page will tell you how to add commands and listen to events to the bot.
Continue: [✨ Adding commands and listening to events](./4_commands_and_events.md)

View File

@@ -0,0 +1,68 @@
# 🏃🏻‍♂️ Running and deploying
There are two methods to run the bot. Choose one that suits best for the situation.
## 👷🏻 Development mode (recommended)
There will be no compilation step, and Bun will automatically watch changes and restart the bot for you.
You can quickly start the bot by running:
```sh
bun dev
```
## 📦 Building
To build the bot, you can run:
```sh
bun run build
```
The distribution files will be placed inside the `dist` directory. Inside will include:
- The default configuration for the bot
- An empty database for the bot with schemas configured
- Compiled source files of the bot
## ✈️ Deploying
To deploy the bot, you'll need to:
1. Replace the `config.ts` file with your own configuration _(optional)_
2. [Build the bot as seen in the previous step](#-building)
3. Run the `reload-slash-commands` script
This is to ensure all commands are registered, so they can be used.
**It may take up to 2 hours until **global** commands are updated. This is a Discord limitation.**
```sh
# Assuming you're in the workspace's root (NOT REPOSITORY ROOT)
bun run scripts/reload-slash-commands.ts
```
4. Copy contents of the `dist` directory
```sh
# For instance, we'll copy them both to /usr/src/discord-bot
# Assuming you're in the workspace's root (NOT REPOSITORY ROOT)
cp -R ./dist/* /usr/src/discord-bot
```
5. Replace the default empty database with your own _(optional)_
6. Configure environment variables
As seen in [`.env.example`](../.env.example). You can also optionally use a `.env` file which **Bun will automatically load**.
7. Finally, run the bot
```sh
cd /usr/src/discord-bot
bun run src/index.js
```
## ⏭️ What's next
The next page will tell you how to add commands and listen to events to the bot.
Continue: [✨ Adding commands and listening to events](./4_commands_and_events.md)

View File

@@ -74,34 +74,49 @@ export default {
Events are a bit different. We have 2 different event systems for both Discord API and our own bot API. This means the [`src/events`](../src/events) directory will have 2 separate directories inside. They are specific to the respective API, but the utility functions make the experience with both of them very similar.
To start adding events, you can use this template:
To start adding events, you can use these templates:
##### Discord event template
```ts
// For Discord events (remove functions you do not use)
import { on, once } from '$utils/discord/events'
import { on, once, withContext } from '$utils/discord/events'
// You will have auto-complete and types for all of them, don't worry!
// WARNING: The first argument is the `context` object for Discord events
// This is intended by design because Discord events usually always use it.
on('eventName', async (context, arg1, arg2, ...) => {
// Do something in here when the event is triggered
on('eventName', async (arg1, arg2, ...) => {
// Do something when the event is triggered
})
once('eventName', async (arg1, arg2, ...) => {
// Do something for only a single time after it's triggered, never again
})
withContext(on, 'eventName', async (context, arg1, arg2, ...) => {
// Do some other thing that requires the context object
})
```
##### API events template
```ts
// For "Helper" events (remove functions you do not use)
import { on, once } from '$utils/api/events'
// You will have auto-complete and types for all of them, don't worry!
on('eventName', async (arg1, arg2, ...) => {
// Do something in here when the event is triggered
// Do something when the event is triggered
})
once('eventName', async (arg1, arg2, ...) => {
// Do something for only a single time after it's triggered, never again
})
```
API events are stored in [`src/events/api`](../src/events/api), and Discord events are in [`src/events/discord`](../src/events/discord).
### 📛 Event file naming conventions
Since a single event file can have multiple listeners, you should name exactly what the file handles.
For example, when a nickname change happens, a member joins, or a member sends a message, the bot is required to cure their nickname. Therefore we would name the event file `curedRequired.ts`.
> [!NOTE]
> If you need multiple event listeners for the same exact event, you can put them in a directory with the event name and rename the listeners to what they handle specifically. You can see how we do it in [`src/events/discord/interactionCreate`](../src/events/discord/interactionCreate).
> If you need multiple event listeners for the same exact event **but also need more abstraction**, you can put them in a directory with the event name and rename the listeners to what they handle specifically. You can see how we do it in [`src/events/discord/interactionCreate`](../src/events/discord/interactionCreate).
## ⏭️ What's next

View File

@@ -1,88 +0,0 @@
# 🫙 Storing data
We use SQLite to store every piece of persistent data. By using Bun, we get access to the `bun:sqlite` module which allows us to easily do SQLite operations.
## 🪄 Creating a database
You can easily create a database by initializing the `BasicDatabase` class:
```ts
interface MyDatabase {
field: string
key: string
}
const db = new BasicDatabase<MyDatabase>(
// File path
'database_file.db',
// Database schema, in SQL
`field TEXT NOT NULL, key TEXT PRIMARY KEY NOT NULL`,
// Custom table name (optional, defaults to 'data'),
'data'
)
```
## 📝 Writing data
Initializing `MyDatabase` will immediately create/open the `database_file.db` file. To write data, you can use the `insert` or `update` method:
```ts
const key = 'my key'
const field = 'some data'
// Order is according to the schema
// db.insert(...columns)
db.insert(field, key)
const field2 = 'some other data'
// db.update(data, filter)
db.update({
field: field2
}, `key = ${key}`)
```
You can also delete a row:
```ts
db.delete(`key = ${key}`)
console.log(db.select(`key = ${key}`)) // null
```
## 👀 Reading data
To get data using a filter, you can use the `select` method:
```ts
// We insert it back
db.insert(field, key)
const data = db.select('*', `key = ${key}`)
console.log(data) // { key: 'my key', field: 'some other data' }
const { key: someKey } = db.select('key', `field = '${field2}'`)
console.log(someKey) // 'my key'
```
If the existing abstractions aren't enough, you can also use the `run`, `prepare`, or `query` method:
```ts
// Enable WAL
db.run('PRAGMA journal_mode=WAL')
const selectFromKey = db.prepare('SELECT * FROM data WHERE key = $key')
console.log(
selectFromKey.get({
$key: key
})
) // { key: 'my key', field: 'some other data' }
console.log(
selectFromKey.get({
$key: 'non existent key'
})
) // null
```