Compare commits

...

23 Commits

Author SHA1 Message Date
semantic-release-bot
042b155b5e chore(release): 1.0.0-dev.6 [skip ci]
# @revanced/discord-bot [1.0.0-dev.6](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.5...@revanced/discord-bot@1.0.0-dev.6) (2024-07-23)

### Bug Fixes

* **bots/discord:** ci issues causing database to not be auto generated ([673aa18](673aa189be))
2024-07-23 20:59:49 +00:00
PalmDevs
673aa189be fix(bots/discord): ci issues causing database to not be auto generated 2024-07-24 03:57:33 +07:00
PalmDevs
c503a86c53 ci(release): also update bun lockfile to prevent install freezes 2024-07-24 03:36:32 +07:00
semantic-release-bot
1bd973ea6c chore(release): 1.0.0-dev.5 [skip ci]
# @revanced/discord-bot [1.0.0-dev.5](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.4...@revanced/discord-bot@1.0.0-dev.5) (2024-07-23)
2024-07-23 20:31:04 +00:00
semantic-release-bot
4bb965e9ff chore(release): 1.0.0-dev.5 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.5](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.4...@revanced/bot-websocket-api@1.0.0-dev.5) (2024-07-23)
2024-07-23 20:30:32 +00:00
PalmDevs
a8ceeb29ae chore: update lockfile 2024-07-24 03:29:32 +07:00
PalmDevs
96a6540434 build(Needs bump): revert building with bun explicitly
Building with only Bun causes compatibility issues, like Drizzle Kit not being to generate any schema for the database of the Discord bot.
2024-07-24 03:25:30 +07:00
PalmDevs
e02c86a9c4 ci(release): add time limit for job 2024-07-24 03:05:54 +07:00
semantic-release-bot
e82f2ab34b chore(release): 1.0.0-dev.4 [skip ci]
# @revanced/discord-bot [1.0.0-dev.4](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.3...@revanced/discord-bot@1.0.0-dev.4) (2024-07-23)

### Bug Fixes

* **bots/discord:** wrong database schema path ([875bd20](875bd209b2))
2024-07-23 20:04:31 +00:00
PalmDevs
2a6f3c3013 chore: update lockfile 2024-07-24 03:03:19 +07:00
PalmDevs
875bd209b2 fix(bots/discord): wrong database schema path 2024-07-24 01:12:30 +07:00
semantic-release-bot
2b601b1a1d chore(release): 1.0.0-dev.3 [skip ci]
# @revanced/discord-bot [1.0.0-dev.3](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.2...@revanced/discord-bot@1.0.0-dev.3) (2024-07-23)

### Bug Fixes

* **bots/discord:** revert dist denesting, fixes config not found ([0d4898d](0d4898dae8))
2024-07-23 15:42:43 +00:00
semantic-release-bot
164570d176 chore(release): 1.0.0-dev.4 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.4](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.3...@revanced/bot-websocket-api@1.0.0-dev.4) (2024-07-23)

### Bug Fixes

* **apis/websocket:** hardcoded paths in tesseract worker builds ([38e00eb](38e00eb4e5))
2024-07-23 15:42:10 +00:00
PalmDevs
0d4898dae8 fix(bots/discord): revert dist denesting, fixes config not found 2024-07-23 22:41:15 +07:00
PalmDevs
38e00eb4e5 fix(apis/websocket): hardcoded paths in tesseract worker builds 2024-07-23 22:38:23 +07:00
semantic-release-bot
3117af5497 chore(release): 1.0.0-dev.2 [skip ci]
# @revanced/discord-bot [1.0.0-dev.2](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.1...@revanced/discord-bot@1.0.0-dev.2) (2024-07-23)

### Features

* **bots/discord:** don't nest builds in src directory, autogen db when missing ([4834685](4834685186))
2024-07-23 14:16:41 +00:00
PalmDevs
6a87464b40 chore(bots/discord): fix build script oversight 2024-07-23 21:15:07 +07:00
semantic-release-bot
5db076aee5 chore(release): 1.0.0-dev.3 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.3](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.2...@revanced/bot-websocket-api@1.0.0-dev.3) (2024-07-23)

### Bug Fixes

* **apis/websocket:** build and runtime issues ([89d8ab1](89d8ab1ee5))
2024-07-23 14:05:57 +00:00
PalmDevs
12f5aaf70f chore: update dependencies 2024-07-23 21:01:05 +07:00
PalmDevs
94d0dcc32b fix(packages/api): misleading errors being thrown 2024-07-23 21:01:04 +07:00
PalmDevs
4834685186 feat(bots/discord): don't nest builds in src directory, autogen db when missing 2024-07-23 21:01:04 +07:00
PalmDevs
ffe0a3ddf5 chore(bots/discord): change config to a js file 2024-07-23 21:01:03 +07:00
PalmDevs
89d8ab1ee5 fix(apis/websocket): build and runtime issues 2024-07-23 21:01:02 +07:00
21 changed files with 186 additions and 67 deletions

View File

@@ -14,6 +14,7 @@ jobs:
permissions:
contents: read
packages: write
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v4

View File

@@ -1,3 +1,19 @@
# @revanced/bot-websocket-api [1.0.0-dev.5](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.4...@revanced/bot-websocket-api@1.0.0-dev.5) (2024-07-23)
# @revanced/bot-websocket-api [1.0.0-dev.4](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.3...@revanced/bot-websocket-api@1.0.0-dev.4) (2024-07-23)
### Bug Fixes
* **apis/websocket:** hardcoded paths in tesseract worker builds ([38e00eb](https://github.com/revanced/revanced-helper/commit/38e00eb4e59c763bd74d27b9b9b482ea66e4dcf4))
# @revanced/bot-websocket-api [1.0.0-dev.3](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.2...@revanced/bot-websocket-api@1.0.0-dev.3) (2024-07-23)
### Bug Fixes
* **apis/websocket:** build and runtime issues ([89d8ab1](https://github.com/revanced/revanced-helper/commit/89d8ab1ee58278a9a96cdc31c679d0a0a0d865af))
# @revanced/bot-websocket-api [1.0.0-dev.2](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.1...@revanced/bot-websocket-api@1.0.0-dev.2) (2024-07-22)

View File

@@ -6,7 +6,7 @@ FROM base AS build
WORKDIR /build
COPY . .
RUN bun install --frozen-lockfile
RUN cd apis/websocket && bun --bun run build
RUN cd apis/websocket && bun run build
FROM base AS release

View File

@@ -2,7 +2,7 @@
"name": "@revanced/bot-websocket-api",
"type": "module",
"private": true,
"version": "1.0.0-dev.2",
"version": "1.0.0-dev.5",
"description": "🧦 WebSocket API server for bots assisting ReVanced",
"main": "dist/index.js",
"scripts": {

View File

@@ -1,29 +1,34 @@
import { createLogger } from '@revanced/bot-shared'
import { cp, rm } from 'fs/promises'
async function build(): Promise<void> {
const logger = createLogger()
const logger = createLogger()
logger.info('Cleaning previous build...')
await rm('./dist', { recursive: true })
logger.info('Cleaning previous build...')
await rm('./dist', { recursive: true })
logger.info('Building Tesseract.js worker...')
await Bun.build({
entrypoints: ['../../node_modules/tesseract.js/src/worker-script/node/index.js'],
target: 'bun',
outdir: './dist/worker',
})
logger.info('Building WebSocket API...')
await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist',
target: 'bun',
minify: true,
sourcemap: 'external',
})
logger.info('Copying Tesseract.js WASM...')
await cp('../../node_modules/tesseract.js-core', './dist/worker/core', { recursive: true })
logger.info('Building Tesseract.js worker...')
await Bun.build({
entrypoints: ['../../node_modules/tesseract.js/src/worker-script/node/index.js'],
external: ['tesseract.js-core/*'],
target: 'bun',
outdir: './dist/worker',
minify: true,
sourcemap: 'external',
})
logger.info('Building WebSocket API...')
await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist',
target: 'bun',
})
}
// Tesseract.js is really bad for minification
// It forcefully requires this core module to be present which contains the WASM files
logger.info('Copying Tesseract.js Core...')
await cp('../../node_modules/tesseract.js-core', './dist/node_modules/tesseract.js-core', { recursive: true })
await build()
logger.info('Copying config...')
await cp('config.json', 'dist/config.json')

View File

@@ -55,12 +55,9 @@ export interface WitMessageResponse {
const TesseractWorkerDirPath = joinPath(import.meta.dir, 'worker')
const TesseractWorkerPath = joinPath(TesseractWorkerDirPath, 'index.js')
const TesseractCorePath = joinPath(TesseractWorkerDirPath, 'core')
export const tesseract = await createTesseractWorker(
'eng',
OEM.DEFAULT,
(await pathExists(TesseractWorkerDirPath))
? { workerPath: TesseractWorkerPath, corePath: TesseractCorePath }
: undefined,
(await pathExists(TesseractWorkerDirPath)) ? { workerPath: TesseractWorkerPath } : undefined,
)

View File

@@ -181,6 +181,7 @@ config.ts
*.db
*.sqlite
*.sqlite3
.drizzle
# Auto-generated files
src/commands/index.ts

View File

@@ -1,3 +1,33 @@
# @revanced/discord-bot [1.0.0-dev.6](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.5...@revanced/discord-bot@1.0.0-dev.6) (2024-07-23)
### Bug Fixes
* **bots/discord:** ci issues causing database to not be auto generated ([673aa18](https://github.com/revanced/revanced-helper/commit/673aa189bef1009a3e32ba3b1291a5ee84f2def3))
# @revanced/discord-bot [1.0.0-dev.5](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.4...@revanced/discord-bot@1.0.0-dev.5) (2024-07-23)
# @revanced/discord-bot [1.0.0-dev.4](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.3...@revanced/discord-bot@1.0.0-dev.4) (2024-07-23)
### Bug Fixes
* **bots/discord:** wrong database schema path ([875bd20](https://github.com/revanced/revanced-helper/commit/875bd209b252566414bf89349839cabc01697e1c))
# @revanced/discord-bot [1.0.0-dev.3](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.2...@revanced/discord-bot@1.0.0-dev.3) (2024-07-23)
### Bug Fixes
* **bots/discord:** revert dist denesting, fixes config not found ([0d4898d](https://github.com/revanced/revanced-helper/commit/0d4898dae8b26f8466d3f6b8f62875866f581644))
# @revanced/discord-bot [1.0.0-dev.2](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.1...@revanced/discord-bot@1.0.0-dev.2) (2024-07-23)
### Features
* **bots/discord:** don't nest builds in src directory, autogen db when missing ([4834685](https://github.com/revanced/revanced-helper/commit/48346851864c4d4b6276388644dd24ce16222b3e))
# @revanced/discord-bot 1.0.0-dev.1 (2024-07-22)

View File

@@ -1,12 +1,17 @@
# This file should be triggered from the monorepo root
FROM oven/bun:latest AS base
FROM node:latest AS base
# Install Bun
RUN apt-get update && apt-get install -y curl unzip
RUN curl -fsSL https://bun.sh/install.sh | bash
ENV PATH="/root/.bun/bin:$PATH"
FROM base AS build
WORKDIR /build
COPY . .
RUN bun install --frozen-lockfile
RUN cd bots/discord && bun --bun run build
RUN cd bots/discord && bun run build
FROM base AS release

View File

@@ -1,5 +1,8 @@
import type { Config } from './config.schema'
// @ts-check
/**
* @type {import('./config.schema').Config}
*/
export default {
owners: ['USER_ID_HERE'],
guilds: ['GUILD_ID_HERE'],
@@ -70,4 +73,4 @@ export default {
disconnectLimit: 3,
disconnectRetryInterval: 10000,
},
} satisfies Config as Config
}

View File

@@ -29,9 +29,8 @@ The distribution files will be placed inside the `dist` directory. Inside will i
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
1. [Build the bot as seen in the previous step](#-building)
2. 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.**
@@ -40,7 +39,7 @@ To deploy the bot, you'll need to:
bun run scripts/reload-slash-commands.ts
```
4. Copy contents of the `dist` directory
3. Copy contents of the `dist` directory
```sh
# For instance, we'll copy them both to /usr/src/discord-bot
@@ -48,18 +47,10 @@ To deploy the bot, you'll need to:
cp -R ./dist/* /usr/src/discord-bot
```
5. Copy the empty database (or use your own existing database)
```sh
# By default, the build script creates the database called "db.sqlite3"
# Unless you specify otherwise via the "DATABASE_PATH" environment variable
cp ./db.sqlite3 /usr/src/discord-bot
```
6. Configure environment variables
4. 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
5. Finally, run the bot
```sh
cd /usr/src/discord-bot

View File

@@ -3,6 +3,7 @@ import { defineConfig } from 'drizzle-kit'
export default defineConfig({
dialect: 'sqlite',
schema: './src/database/schemas.ts',
out: './.drizzle',
dbCredentials: {
url: process.env['DATABASE_PATH'] ? `file:./${process.env['DATABASE_PATH']}` : 'file:./db.sqlite3',
},

View File

@@ -2,17 +2,16 @@
"name": "@revanced/discord-bot",
"type": "module",
"private": true,
"version": "1.0.0-dev.1",
"version": "1.0.0-dev.6",
"description": "🤖 Discord bot assisting ReVanced",
"main": "src/index.ts",
"scripts": {
"register": "bun run scripts/reload-slash-commands.ts",
"start": "bun prepare && bun run src/index.ts",
"dev": "bun prepare && bun --watch src/index.ts",
"build:config": "bun build config.ts --outdir=dist",
"build": "bun prepare && bun build:config && bun build src/index.ts -e ./config.js --target=bun --outdir=dist/src",
"build": "bun prepare && bun run scripts/build.ts",
"watch": "bun dev",
"prepare": "bun run scripts/generate-indexes.ts && drizzle-kit push"
"prepare": "bun run scripts/generate-indexes.ts && bunx drizzle-kit generate --name=schema"
},
"repository": {
"type": "git",

View File

@@ -0,0 +1,23 @@
import { createLogger } from '@revanced/bot-shared'
import { cp, rename, rm } from 'fs/promises'
const logger = createLogger()
logger.warn('Cleaning previous build...')
await rm('./dist', { recursive: true })
logger.info('Building bot...')
await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist/src',
target: 'bun',
external: ['./config.js'],
minify: true,
sourcemap: 'external',
})
logger.info('Copying config...')
await cp('config.js', 'dist/config.js')
logger.info('Copying database schema...')
await rename('.drizzle', 'dist/.drizzle')

View File

@@ -1,4 +1,6 @@
import { Database } from 'bun:sqlite'
import { existsSync, readFileSync, readdirSync } from 'fs'
import { join } from 'path'
import { Client as APIClient } from '@revanced/bot-api'
import { createLogger } from '@revanced/bot-shared'
import { ActivityType, Client as DiscordClient, Partials } from 'discord.js'
@@ -29,7 +31,33 @@ export const api = {
disconnectCount: 0,
}
const db = new Database(process.env['DATABASE_PATH'])
const DatabasePath = process.env['DATABASE_PATH']
const DatabaseSchemaDir = join(import.meta.dir, '..', '.drizzle')
let dbSchemaFileName: string | undefined
if (DatabasePath && !existsSync(DatabasePath)) {
logger.warn('Database file not found, trying to create from schema...')
try {
const file = readdirSync(DatabaseSchemaDir, { withFileTypes: true })
.filter(file => file.isFile() && file.name.endsWith('.sql'))
.sort()
.at(-1)
if (!file) throw new Error('No schema file found')
dbSchemaFileName = file.name
logger.debug(`Using schema file: ${dbSchemaFileName}`)
} catch (e) {
logger.fatal('Could not create database from schema, check if the schema file exists and is accessible')
logger.fatal(e)
process.exit(1)
}
}
const db = new Database(DatabasePath)
if (dbSchemaFileName) db.run(readFileSync(join(DatabaseSchemaDir, dbSchemaFileName)).toString())
export const database = drizzle(db, {
schema: schemas,

View File

@@ -9,6 +9,7 @@
"composite": false,
"exactOptionalPropertyTypes": false,
"esModuleInterop": true,
"allowJs": true,
"paths": {
"$/*": ["./src/*"],
"$constants": ["./src/constants"],

BIN
bun.lockb

Binary file not shown.

View File

@@ -41,11 +41,11 @@
"@tsconfig/strictest": "^2.0.5",
"@types/bun": "^1.1.6",
"conventional-changelog-conventionalcommits": "^7.0.2",
"lefthook": "^1.7.4",
"lefthook": "^1.7.5",
"portainer-service-webhook": "https://github.com/newarifrh/portainer-service-webhook#v1",
"semantic-release": "^24.0.0",
"turbo": "^2.0.9",
"typescript": "^5.5.3"
"typescript": "^5.5.4"
},
"trustedDependencies": [
"@biomejs/biome",

View File

@@ -53,23 +53,34 @@ export class ClientWebSocketManager {
}
}, this.timeout)
this.#socket.on('open', () => {
this.disconnected = false
clearTimeout(timeout)
this.#listen()
rs()
})
this.#socket.on('error', err => {
clearTimeout(timeout)
const errorBeforeReadyHandler = (err: Error) => {
cleanup()
throw err
})
}
this.#socket.on('close', (code, reason) => {
const closeBeforeReadyHandler = (code: number, reason: Buffer) => {
clearTimeout(timeout)
this._handleDisconnect(code, reason.toString())
throw new Error('WebSocket connection closed before ready')
})
}
const readyHandler = () => {
this.disconnected = false
cleanup()
this.#listen()
rs()
}
const cleanup = () => {
this.#socket.off('open', readyHandler)
this.#socket.off('close', closeBeforeReadyHandler)
this.#socket.off('error', errorBeforeReadyHandler)
clearTimeout(timeout)
}
this.#socket.on('open', readyHandler)
this.#socket.on('error', errorBeforeReadyHandler)
this.#socket.on('close', closeBeforeReadyHandler)
} catch (e) {
rj(e)
}

View File

@@ -1,8 +1,14 @@
diff --git a/node_modules/@semantic-release/npm/.bun-tag-3853154e196b7721 b/.bun-tag-3853154e196b7721
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/node_modules/@semantic-release/npm/.bun-tag-550461f23a8ec245 b/.bun-tag-550461f23a8ec245
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/node_modules/@semantic-release/npm/.bun-tag-c9c8130945517add b/.bun-tag-c9c8130945517add
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/lib/prepare.js b/lib/prepare.js
index 3e76bec44cf595a1b4141728336bed904d4d518d..c6baf4e8de9bdf7536f9ad2e9eb9c360e031c7b5 100644
index 3e76bec44cf595a1b4141728336bed904d4d518d..4b25ca64879bbee2a600f2b23b738c86136ad9c6 100644
--- a/lib/prepare.js
+++ b/lib/prepare.js
@@ -1,6 +1,7 @@
@@ -14,7 +20,7 @@ index 3e76bec44cf595a1b4141728336bed904d4d518d..c6baf4e8de9bdf7536f9ad2e9eb9c360
export default async function (
npmrc,
@@ -11,19 +12,12 @@ export default async function (
@@ -11,19 +12,13 @@ export default async function (
logger.log("Write version %s to package.json in %s", version, basePath);
@@ -36,10 +42,11 @@ index 3e76bec44cf595a1b4141728336bed904d4d518d..c6baf4e8de9bdf7536f9ad2e9eb9c360
- await versionResult;
+ await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, detectIndent(pkgJsonRaw).indent))
+ await execa("bun", ["install"]);
if (tarballDir) {
logger.log("Creating npm package version %s", version);
@@ -38,7 +32,7 @@ export default async function (
@@ -38,7 +33,7 @@ export default async function (
// Only move the tarball if we need to
// Fixes: https://github.com/semantic-release/npm/issues/169
if (tarballSource !== tarballDestination) {

View File

@@ -31,7 +31,7 @@ const Options = {
[
'@semantic-release/git',
{
assets: ['CHANGELOG.md', 'package.json'],
assets: ['CHANGELOG.md', 'package.json', '../../bun.lockb'],
},
],
[