Compare commits

...

124 Commits

Author SHA1 Message Date
Bas950
f339035463 chore: release v0.0.18 2024-11-17 21:02:17 +01:00
Bas950
b67226dcdd feat: fix presence endpoints 2024-11-17 21:02:07 +01:00
Bas950
154acbc802 chore: release v0.0.17 2024-11-17 18:42:52 +01:00
Bas950
a1ed87eccf fix: add dbName 2024-11-17 18:42:43 +01:00
Florian Metz
cb4ecf7c95 chore: release v0.0.16 2024-11-17 02:12:40 +01:00
Florian Metz
26a0fee323 chore: fix type 2024-11-17 02:12:10 +01:00
Bas950
b48dc12c8f chore: release v0.0.15 2024-11-17 01:09:33 +01:00
Bas950
dcf3218d0b fix(api): metadata types 2024-11-17 01:09:09 +01:00
Bas950
bdcc05a300 chore: release v1.0.5 2024-11-17 01:02:43 +01:00
Bas950
51b4508a1c chore: change info message 2024-11-17 01:02:39 +01:00
Bas950
378671b267 chore: release v1.0.4 2024-11-17 00:58:11 +01:00
Bas950
53a02c98e0 fix: get presence list correctly 2024-11-17 00:57:26 +01:00
Bas van Zanten
b60c991fea Update presence.ts (#1072) 2024-11-10 16:56:18 +01:00
Florian Metz
3b178d70b9 chore: release v1.0.3 2024-10-08 09:56:28 +02:00
veryCrunchy
ef377d3f68 fix(discord-bot): update developer roles constant (#1070)
* chore: disable commitlint

* fix(discord-bot): update developer roles constant

* Update commit-msg
2024-10-08 09:27:10 +02:00
Bas950
d5acdefc45 chore: lint 2024-10-08 09:21:10 +02:00
Bas950
39ffee4126 chore: un-ingore config files 2024-10-08 09:16:22 +02:00
Bas950
ed30c593ad chore: release v1.0.2 2024-09-29 13:22:29 +02:00
Bas950
9ffa6addb3 feat(discord-bot): add beta command 2024-09-29 13:22:08 +02:00
Bas950
95fd02b513 chore: release v1.0.1 2024-09-26 12:27:56 +02:00
Bas950
3198ac3704 fix(discord-bot): fixes credits 2024-09-26 12:27:11 +02:00
Florian Metz
0d4b53f382 chore: release v1.0.0 2024-09-26 10:56:07 +02:00
Florian Metz
3600ef87a5 chore: update version 2024-09-26 10:55:59 +02:00
Bas van Zanten
55ffabb352 feat: discord-bot (#1069)
* feat: discord-bot

* feat: final things

* chore: add to matrix

* feat: add sentry

* chore: move some things
2024-09-26 10:11:07 +02:00
Bas950
5a90f95e58 chore: release v0.0.37 2024-09-24 17:07:06 +02:00
Bas950
64547dc0ef feat: ip data 2024-09-24 17:06:48 +02:00
Bas950
9cf3f93889 chore: release v0.0.36 2024-09-24 13:47:54 +02:00
Bas950
0e30a0d250 chore: scan keys instead 2024-09-24 13:47:39 +02:00
Bas950
4dc941bb91 chore: release v0.0.35 2024-09-24 11:08:50 +02:00
Bas950
3a78c6529e feat: use prom-client 2024-09-24 10:50:21 +02:00
Bas950
d4673720a0 chore: release v1.0.4 2024-09-20 19:03:37 +02:00
Bas950
dc859448bd feat: schema v1.11 2024-09-20 18:57:28 +02:00
Bas950
9cbb88beda chore: release v0.0.34 2024-09-17 10:54:54 +02:00
Bas950
09bcfe703f chore: scan count config 2024-09-17 10:54:48 +02:00
Bas950
d24eda8957 chore: release v0.0.33 2024-09-17 10:45:42 +02:00
Bas950
bfffcb94ee chore: some testing 2024-09-17 10:45:38 +02:00
Bas950
4db6a78816 chore: release v0.0.32 2024-09-17 10:35:04 +02:00
Bas950
666838874f chore: forgot to save 2024-09-17 10:34:58 +02:00
Bas950
697f3660c2 chore: some improvements 2024-09-17 10:34:46 +02:00
Florian Metz
a668add973 chore: release v0.0.31 2024-09-17 10:00:06 +02:00
Florian Metz
42b70b1259 chore: optimize active presence gauge update with concurrency limit 2024-09-17 09:59:41 +02:00
Bas950
253b680d3e chore: release v0.0.30 2024-09-17 09:36:49 +02:00
Bas950
e9a40dc553 chore: small updates 2024-09-17 09:36:43 +02:00
Bas950
b25880d4cd chore: release v0.0.29 2024-09-17 09:11:12 +02:00
Bas950
fb06227aeb chore: release v0.0.29 2024-09-17 09:07:58 +02:00
Bas950
ff3d00497b chore: release v0.0.29 2024-09-17 09:07:21 +02:00
Bas950
a06780f85a chore: reduce memory 2024-09-17 09:06:10 +02:00
Bas950
5b1969c7ab chore: release v0.0.28 2024-09-16 23:22:15 +02:00
Bas950
bedd34594c chore: disable ip stuff for now 2024-09-16 23:22:11 +02:00
Bas950
47feaa5c70 chore: release v0.0.27 2024-09-16 22:56:14 +02:00
Bas950
9fb32f53ae chore: reduce batch size 2024-09-16 22:56:10 +02:00
Bas950
bfb84bb080 chore: release v0.0.26 2024-09-16 22:32:52 +02:00
Bas950
f545b174bd chore: lint 2024-09-16 22:32:42 +02:00
Bas950
4a492cf275 fix: store ip data in postgres 2024-09-16 22:30:29 +02:00
Bas950
3f65f678b1 chore: release v0.0.25 2024-09-16 20:58:24 +02:00
Bas950
a71b66540b chore: release v0.0.14 2024-09-16 20:58:07 +02:00
Bas van Zanten
e675f74983 feat: update tracing (#1067) 2024-09-16 20:18:35 +02:00
Florian Metz
e9e6639492 chore: release v0.0.13 2024-09-15 03:09:23 +02:00
Florian Metz
3258179040 chore: release v0.0.24 2024-09-15 03:09:13 +02:00
Florian Metz
086d476af2 chore: update hash 2024-09-15 03:09:04 +02:00
Florian Metz
146bf9e270 chore: release v0.0.23 2024-09-15 02:48:56 +02:00
Florian Metz
a02f25ba29 chore: test 2024-09-15 02:48:41 +02:00
Florian Metz
416b65f0d4 chore: release v0.0.12 2024-09-15 02:41:31 +02:00
Florian Metz
f8e9fc832d chore: test 2024-09-15 02:41:16 +02:00
Florian Metz
86b0f07216 chore: test 2024-09-15 02:31:38 +02:00
Florian Metz
9eb5c03877 chore: release v0.0.11 2024-09-15 02:25:50 +02:00
Florian Metz
e63e1270aa chore: release v0.0.22 2024-09-15 02:25:38 +02:00
Florian Metz
f730e71bbf chore: test 2024-09-15 02:25:10 +02:00
Bas950
8b68bf85c8 chore: release v0.0.10 2024-09-13 17:27:44 +02:00
Bas950
e4c794a9ad chore: 202 on disabled flag 2024-09-13 17:27:38 +02:00
Bas950
6e8258d76f chore: release v0.0.21 2024-09-13 15:08:16 +02:00
Bas950
56b796c621 chore: use ky 2024-09-13 15:08:08 +02:00
Bas950
0de59c48b4 chore: release v0.0.20 2024-09-13 14:37:31 +02:00
Bas950
60056e069d chore: update log 2024-09-13 14:37:24 +02:00
Bas950
b6bad90919 chore: release v0.0.9 2024-09-13 14:33:34 +02:00
Bas950
ee21bb9dec chore: release v0.0.20 2024-09-13 14:31:39 +02:00
Bas950
6efac4fef1 feat: use scienceId 2024-09-13 14:31:27 +02:00
Bas950
93424793bd chore: release v0.0.19 2024-09-13 13:46:33 +02:00
Bas950
affcb6a0cf chore: add reason 2024-09-13 13:46:27 +02:00
Bas950
bb56949dfb chore: release v0.0.18 2024-09-13 13:02:31 +02:00
Bas950
c06fe04b65 chore: fix time 2024-09-13 13:02:26 +02:00
Florian Metz
ef976341ba chore: release v0.0.17 2024-09-13 12:33:19 +02:00
Florian Metz
38893891af chore: why does it not abort 2024-09-13 12:33:10 +02:00
Florian Metz
63eeeefda7 chore: release v0.0.16 2024-09-13 12:05:42 +02:00
Florian Metz
056db21cb0 chore: add p-limit dependency for session cleanup 2024-09-13 12:05:37 +02:00
Bas950
d8dc08c6c3 chore: release v0.0.15 2024-09-13 11:55:36 +02:00
Bas950
634391b6e3 chore: always return the key 2024-09-13 11:55:32 +02:00
Florian Metz
c46cf6975a chore: release v0.0.14 2024-09-13 11:52:23 +02:00
Florian Metz
68c6b4fcdc chore: add p-limit dependency for session cleanup 2024-09-13 11:52:00 +02:00
Florian Metz
55fa07d5b5 chore: release v0.0.13 2024-09-13 11:38:49 +02:00
Florian Metz
903c238b33 chore: add timeout to headless session deletion 2024-09-13 11:38:40 +02:00
Bas950
acd9afb2b1 chore: release v0.0.12 2024-09-13 11:32:55 +02:00
Bas950
4bd42390eb chore: move some code 2024-09-13 11:32:44 +02:00
Florian Metz
c014504464 chore: release v0.0.11 2024-09-13 11:00:16 +02:00
Florian Metz
24fe349b60 chore: optimize session cleanup with batch deletion 2024-09-13 10:59:13 +02:00
Bas950
ee5428ce08 chore: release v0.0.10 2024-09-13 10:38:38 +02:00
Bas950
e4b1010160 chore: skip clearOldSesssions if another in progress 2024-09-13 10:38:21 +02:00
Bas950
34c42d59ed chore: release v0.0.9 2024-09-12 15:45:16 +02:00
Bas950
d9267361aa feat: use scan 2024-09-12 15:45:10 +02:00
Bas950
0d5382fd50 chore: release v0.0.8 2024-09-12 14:49:01 +02:00
Bas950
e9015b1204 chore: iodk 2024-09-12 14:47:31 +02:00
Bas950
cea36426ab chore: idk kek 2024-09-12 14:46:13 +02:00
Bas950
48c141094e chore: release v0.0.8 2024-09-12 14:41:56 +02:00
Bas950
e67fb97e14 chore: update lockfile 2024-09-12 14:41:51 +02:00
Bas950
0bd0d759f6 chore: release v0.0.7 2024-09-12 14:38:34 +02:00
Bas950
60b7f63409 feat(api-master): add metrics 2024-09-12 14:38:10 +02:00
Bas950
78b482be4f chore: release v0.0.8 2024-09-11 21:33:32 +02:00
Bas950
9db9e931b6 chore: release v0.0.6 2024-09-11 21:33:21 +02:00
Bas950
665263e9b5 chore: revert redis stuff 2024-09-11 21:33:14 +02:00
Bas950
60257dbe53 chore: release v0.0.5 2024-09-11 21:03:22 +02:00
Bas950
411a70f567 chore: release v0.0.7 2024-09-11 21:02:59 +02:00
Bas950
4d1b092ee5 chore: hash the key 2024-09-11 21:02:44 +02:00
Bas950
aa41f1cdae chore: release v0.0.6 2024-09-11 20:31:04 +02:00
Bas950
04b5d54697 chore: update arktype 2024-09-11 20:30:48 +02:00
Bas950
fb096bc4be chore: release v0.0.4 2024-09-11 20:13:30 +02:00
Bas950
6e9e4ae1b6 chore: release v0.0.5 2024-09-11 20:13:16 +02:00
Bas950
d8f73202b9 chore: fix build 2024-09-11 18:42:46 +02:00
Bas950
b175a08dce feat: add session-keep-alive 2024-09-11 18:37:30 +02:00
Florian Metz
2284ee94ad chore: update npm dependencies 2024-09-09 17:44:17 +02:00
Florian Metz
78a3311342 chore: release v0.0.4 2024-08-18 02:53:01 +02:00
Florian Metz
a1fabd3fd6 feat: metrics? 2024-08-18 02:52:51 +02:00
Florian Metz
93c62cc38f chore: release v0.0.3 2024-08-18 00:34:59 +02:00
Florian Metz
8553613593 feat: add feature flags 2024-08-18 00:34:15 +02:00
Bas950
bf83dc4452 chore: bump dep 2024-08-08 11:04:12 +02:00
88 changed files with 8884 additions and 3589 deletions

View File

@@ -20,6 +20,7 @@ jobs:
- website
- api-worker
- api-master
- discord-bot
steps:
- name: Checkout Repository
uses: actions/checkout@v4

View File

@@ -48,6 +48,7 @@ jobs:
- api-worker
- api-master
- website
- discord-bot
steps:
- name: Checkout Repository
uses: actions/checkout@v4

7
.gitignore vendored
View File

@@ -3,6 +3,7 @@ out
dist
tmp
lib
data
.vscode
.env
@@ -19,7 +20,9 @@ src/update.ini
*.app
*.xml.backup
*.js
!eslint.config.js
!*.config.js
coverage
*.tsbuildinfo
*.tsbuildinfo
.DS_Store
*.log

View File

@@ -1,2 +1,3 @@
*.js
*.ts
*.ts
*.json

View File

@@ -0,0 +1,10 @@
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dbCredentials: {
url: "postgresql://metrics:metrics@localhost:5432/metrics",
},
dialect: "postgresql",
schema: "./src/db.ts",
out: "./drizzle",
});

View File

@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS "online_users_ip_data" (
"uuid" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"ip" varchar(45) NOT NULL,
"country" varchar(2) NOT NULL,
"latitude" numeric(10, 8) NOT NULL,
"longitude" numeric(11, 8) NOT NULL,
"name" varchar(255),
"timestamp" timestamp DEFAULT now()
);

View File

@@ -0,0 +1,2 @@
CREATE INDEX IF NOT EXISTS "idx_online_users_uuid" ON "online_users_ip_data" USING btree ("uuid");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "idx_online_users_timestamp" ON "online_users_ip_data" USING btree ("timestamp");

View File

@@ -0,0 +1 @@
ALTER TABLE "online_users_ip_data" ALTER COLUMN "timestamp" SET DATA TYPE timestamp with time zone;

View File

@@ -0,0 +1,2 @@
ALTER TABLE "online_users_ip_data" ADD COLUMN "presences" jsonb DEFAULT '[]' NOT NULL;--> statement-breakpoint
ALTER TABLE "online_users_ip_data" DROP COLUMN IF EXISTS "name";

View File

@@ -0,0 +1 @@
ALTER TABLE "online_users_ip_data" ADD COLUMN "sessions" integer DEFAULT 0 NOT NULL;

View File

@@ -0,0 +1,70 @@
{
"id": "e29a6708-01f1-455a-b345-63dac1e124dc",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.online_users_ip_data": {
"name": "online_users_ip_data",
"schema": "",
"columns": {
"uuid": {
"name": "uuid",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"ip": {
"name": "ip",
"type": "varchar(45)",
"primaryKey": false,
"notNull": true
},
"country": {
"name": "country",
"type": "varchar(2)",
"primaryKey": false,
"notNull": true
},
"latitude": {
"name": "latitude",
"type": "numeric(10, 8)",
"primaryKey": false,
"notNull": true
},
"longitude": {
"name": "longitude",
"type": "numeric(11, 8)",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"timestamp": {
"name": "timestamp",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -0,0 +1,101 @@
{
"id": "4aa32a8e-f573-43b9-976a-2d078a0df0ea",
"prevId": "e29a6708-01f1-455a-b345-63dac1e124dc",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.online_users_ip_data": {
"name": "online_users_ip_data",
"schema": "",
"columns": {
"uuid": {
"name": "uuid",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"ip": {
"name": "ip",
"type": "varchar(45)",
"primaryKey": false,
"notNull": true
},
"country": {
"name": "country",
"type": "varchar(2)",
"primaryKey": false,
"notNull": true
},
"latitude": {
"name": "latitude",
"type": "numeric(10, 8)",
"primaryKey": false,
"notNull": true
},
"longitude": {
"name": "longitude",
"type": "numeric(11, 8)",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"timestamp": {
"name": "timestamp",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {
"idx_online_users_uuid": {
"name": "idx_online_users_uuid",
"columns": [
{
"expression": "uuid",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
},
"idx_online_users_timestamp": {
"name": "idx_online_users_timestamp",
"columns": [
{
"expression": "timestamp",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -0,0 +1,101 @@
{
"id": "c1b8dbed-b232-4d66-9e74-b9af333095bc",
"prevId": "4aa32a8e-f573-43b9-976a-2d078a0df0ea",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.online_users_ip_data": {
"name": "online_users_ip_data",
"schema": "",
"columns": {
"uuid": {
"name": "uuid",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"ip": {
"name": "ip",
"type": "varchar(45)",
"primaryKey": false,
"notNull": true
},
"country": {
"name": "country",
"type": "varchar(2)",
"primaryKey": false,
"notNull": true
},
"latitude": {
"name": "latitude",
"type": "numeric(10, 8)",
"primaryKey": false,
"notNull": true
},
"longitude": {
"name": "longitude",
"type": "numeric(11, 8)",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"timestamp": {
"name": "timestamp",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {
"idx_online_users_uuid": {
"name": "idx_online_users_uuid",
"columns": [
{
"expression": "uuid",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
},
"idx_online_users_timestamp": {
"name": "idx_online_users_timestamp",
"columns": [
{
"expression": "timestamp",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -0,0 +1,102 @@
{
"id": "e409a4d0-f698-484a-b412-38966a7b3a19",
"prevId": "c1b8dbed-b232-4d66-9e74-b9af333095bc",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.online_users_ip_data": {
"name": "online_users_ip_data",
"schema": "",
"columns": {
"uuid": {
"name": "uuid",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"ip": {
"name": "ip",
"type": "varchar(45)",
"primaryKey": false,
"notNull": true
},
"country": {
"name": "country",
"type": "varchar(2)",
"primaryKey": false,
"notNull": true
},
"latitude": {
"name": "latitude",
"type": "numeric(10, 8)",
"primaryKey": false,
"notNull": true
},
"longitude": {
"name": "longitude",
"type": "numeric(11, 8)",
"primaryKey": false,
"notNull": true
},
"presences": {
"name": "presences",
"type": "jsonb",
"primaryKey": false,
"notNull": true,
"default": "'[]'"
},
"timestamp": {
"name": "timestamp",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {
"idx_online_users_uuid": {
"name": "idx_online_users_uuid",
"columns": [
{
"expression": "uuid",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
},
"idx_online_users_timestamp": {
"name": "idx_online_users_timestamp",
"columns": [
{
"expression": "timestamp",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -0,0 +1,109 @@
{
"id": "179435b5-dc15-4a42-9539-c3f336699d63",
"prevId": "e409a4d0-f698-484a-b412-38966a7b3a19",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.online_users_ip_data": {
"name": "online_users_ip_data",
"schema": "",
"columns": {
"uuid": {
"name": "uuid",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"ip": {
"name": "ip",
"type": "varchar(45)",
"primaryKey": false,
"notNull": true
},
"country": {
"name": "country",
"type": "varchar(2)",
"primaryKey": false,
"notNull": true
},
"latitude": {
"name": "latitude",
"type": "numeric(10, 8)",
"primaryKey": false,
"notNull": true
},
"longitude": {
"name": "longitude",
"type": "numeric(11, 8)",
"primaryKey": false,
"notNull": true
},
"sessions": {
"name": "sessions",
"type": "integer",
"primaryKey": false,
"notNull": true,
"default": 0
},
"presences": {
"name": "presences",
"type": "jsonb",
"primaryKey": false,
"notNull": true,
"default": "'[]'"
},
"timestamp": {
"name": "timestamp",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {
"idx_online_users_uuid": {
"name": "idx_online_users_uuid",
"columns": [
{
"expression": "uuid",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
},
"idx_online_users_timestamp": {
"name": "idx_online_users_timestamp",
"columns": [
{
"expression": "timestamp",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -0,0 +1,41 @@
{
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1726516195146,
"tag": "0000_flippant_marrow",
"breakpoints": true
},
{
"idx": 1,
"version": "7",
"when": 1726516348344,
"tag": "0001_white_lifeguard",
"breakpoints": true
},
{
"idx": 2,
"version": "7",
"when": 1726516660134,
"tag": "0002_new_darkhawk",
"breakpoints": true
},
{
"idx": 3,
"version": "7",
"when": 1726517073510,
"tag": "0003_narrow_mastermind",
"breakpoints": true
},
{
"idx": 4,
"version": "7",
"when": 1726517405363,
"tag": "0004_tiresome_puff_adder",
"breakpoints": true
}
]
}

16
apps/api-master/environment.d.ts vendored Normal file
View File

@@ -0,0 +1,16 @@
declare module "ip-location-api" {
export function lookup(ip: string): Promise<{
latitude: number;
longitude: number;
country: string;
} | null>;
export function updateDb(options: { fields?: string[]; dataDir?: string; tmpDataDir?: string; smallMemory?: boolean; autoUpdate?: string }): Promise<void>;
export function reload(options: { fields?: string[]; dataDir?: string; tmpDataDir?: string; smallMemory?: boolean; autoUpdate?: string }): Promise<void>;
}
declare namespace NodeJS {
export interface ProcessEnv {
METRICS_DATABASE_URL?: string;
}
}

View File

@@ -1,7 +1,7 @@
{
"name": "@premid/api-master",
"type": "module",
"version": "0.0.3",
"version": "0.0.37",
"private": true,
"description": "PreMiD's api master",
"license": "MPL-2.0",
@@ -11,17 +11,27 @@
],
"scripts": {
"start": "node --enable-source-maps .",
"dev": "node --watch --env-file .env --enable-source-maps ."
"dev": "node --watch --env-file .env --enable-source-maps .",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:update": "pnpm db:generate && pnpm db:migrate",
"db:studio": "drizzle-kit studio"
},
"dependencies": {
"@discordjs/rest": "^2.3.0",
"@envelop/sentry": "^9.0.0",
"@sentry/node": "^8.17.0",
"cron": "^3.1.7",
"debug": "^4.3.6",
"ioredis": "^5.3.2"
"drizzle-orm": "^0.33.0",
"ioredis": "^5.3.2",
"ip-location-api": "^2.0.1",
"ky": "^1.7.2",
"p-limit": "^6.1.0",
"postgres": "^3.4.4",
"prom-client": "^15.1.3"
},
"devDependencies": {
"@types/debug": "^4.1.12"
"@types/debug": "^4.1.12",
"drizzle-kit": "^0.24.2"
}
}

27
apps/api-master/src/db.ts Normal file
View File

@@ -0,0 +1,27 @@
import process from "node:process";
import { decimal, index, integer, jsonb, pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
// Define the schema
export const onlineUsersIpData = pgTable("online_users_ip_data", {
uuid: uuid("uuid").primaryKey().defaultRandom(),
ip: varchar("ip", { length: 45 }).notNull(),
country: varchar("country", { length: 2 }).notNull(),
latitude: decimal("latitude", { precision: 10, scale: 8 }).notNull(),
longitude: decimal("longitude", { precision: 11, scale: 8 }).notNull(),
sessions: integer("sessions").notNull().default(0),
presences: jsonb("presences").notNull().default("[]").$type<string[]>(),
timestamp: timestamp("timestamp", { withTimezone: true }).defaultNow(),
}, table => ({
idxOnlineUsersUuid: index("idx_online_users_uuid").on(table.uuid),
idxOnlineUsersTimestamp: index("idx_online_users_timestamp").on(table.timestamp),
}));
if (!process.env.METRICS_DATABASE_URL) {
throw new Error("METRICS_DATABASE_URL is not set");
}
export const sql = postgres(process.env.METRICS_DATABASE_URL);
export const db = drizzle(sql);

View File

@@ -0,0 +1,10 @@
import { lt, sql } from "drizzle-orm";
import { db, onlineUsersIpData } from "../db.js";
import { mainLog } from "../index.js";
export async function cleanupOldUserData(retentionDays: number) {
mainLog("Cleaning up old user ip data");
const interval = `'${retentionDays} days'`;
await db.delete(onlineUsersIpData)
.where(lt(onlineUsersIpData.timestamp, sql`now() - interval ${sql.raw(interval)}`));
}

View File

@@ -1,47 +1,100 @@
import { REST } from "@discordjs/rest";
import pLimit from "p-limit";
import ky, { HTTPError, TimeoutError } from "ky";
import { mainLog, redis } from "../index.js";
export async function clearOldSesssions() {
const sessions = await redis.hgetall("pmd-api.sessions");
const now = Date.now();
if (Object.keys(sessions).length === 0) {
mainLog("No sessions to clear");
let inProgress = false;
export async function clearOldSessions() {
if (inProgress) {
mainLog("Session cleanup already in progress");
return;
}
mainLog(`Checking ${Object.keys(sessions).length} sessions`);
inProgress = true;
const now = Date.now();
const pattern = "pmd-api.sessions.*";
let cursor = "0";
let totalSessions = 0;
let cleared = 0;
for (const [key, value] of Object.entries(sessions)) {
const session = JSON.parse(value) as {
token: string;
session: string;
lastUpdated: number;
};
const batchSize = 100;
let keysToDelete: string[] = [];
// ? If the session is younger than 30seconds, skip it
if (now - session.lastUpdated < 30000)
continue;
mainLog("Starting session cleanup");
// ? Delete the session
try {
const discord = new REST({ version: "10", authPrefix: "Bearer" });
discord.setToken(session.token);
await discord.post("/users/@me/headless-sessions/delete", {
body: {
token: session.session,
},
});
}
catch (error) {
mainLog(`Failed to delete session: %O`, error);
const limit = pLimit(100); // Create a limit of 100 concurrent operations
do {
const [newCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", 1000); //* Use SCAN with COUNT for memory efficiency
cursor = newCursor;
totalSessions += keys.length;
const deletePromises: Promise<string>[] = [];
for (const key of keys) {
const session = await redis.hgetall(key) as unknown as {
token: string;
session: string;
lastUpdated: number;
};
if (now - session.lastUpdated < 30000)
continue;
deletePromises.push(limit(() => deleteSession(session, key)));
}
cleared++;
const results = await Promise.allSettled(deletePromises);
results.forEach((result) => {
if (result.status === "fulfilled" && result.value) {
keysToDelete.push(result.value);
cleared++;
}
});
await redis.hdel("pmd-api.sessions", key);
if (keysToDelete.length >= batchSize) {
await redis.del(...keysToDelete);
keysToDelete = [];
}
} while (cursor !== "0");
if (keysToDelete.length > 0) {
await redis.del(...keysToDelete);
}
mainLog(`Cleared ${cleared} sessions`);
if (totalSessions === 0) {
mainLog("No sessions to clear");
}
else {
mainLog(`Checked ${totalSessions} sessions, cleared ${cleared}`);
}
inProgress = false;
}
async function deleteSession(session: { token: string; session: string }, key: string): Promise<string> {
try {
await ky.post("https://discord.com/api/v10/users/@me/headless-sessions/delete", {
json: {
token: session.session,
},
headers: {
Authorization: `Bearer ${session.token}`,
},
retry: 3,
timeout: 5000,
});
}
catch (error) {
if (error instanceof TimeoutError) {
mainLog(`Session deletion aborted due to timeout for key ${key}`);
}
else if (error instanceof HTTPError) {
mainLog(`Failed to delete session for key ${key}: [${error.name}] ${error.message} ${JSON.stringify(await error.response.json())}`);
}
else {
mainLog(`Failed to delete session for key ${key}: Unknown error`);
}
}
return key;
}

View File

@@ -0,0 +1,40 @@
import type { InferInsertModel } from "drizzle-orm";
import { db, onlineUsersIpData } from "../db.js";
import { lookupIp } from "./lookupIp.js";
const batchSize = 1000;
export async function insertIpData(
data: Map<string, {
presences: string[];
sessions: number;
}>,
) {
const timestamp = new Date();
const list = [...data.keys()];
//* Split into batches of batchSize
for (let i = 0; i < list.length; i += batchSize) {
const batch = list.slice(i, i + batchSize);
const mapped = await Promise.all(batch.map(async (ip) => {
const parsed = await lookupIp(ip);
if (parsed) {
const { presences, sessions } = data.get(ip)!;
return {
ip,
country: parsed.country,
latitude: parsed.latitude.toString(),
longitude: parsed.longitude.toString(),
presences,
sessions,
timestamp,
} satisfies InferInsertModel<typeof onlineUsersIpData>;
}
}));
const toInsert = mapped.filter(Boolean) as InferInsertModel<typeof onlineUsersIpData>[];
if (toInsert.length > 0) {
await db.insert(onlineUsersIpData).values(toInsert);
}
}
}

View File

@@ -0,0 +1,46 @@
import { join } from "node:path";
import process from "node:process";
import { lookup, reload } from "ip-location-api";
import { mainLog } from "../index.js";
const fields = ["latitude", "longitude", "country"];
const dataDir = join(process.cwd(), "data");
const tmpDataDir = join(process.cwd(), "tmp");
const smallMemory = true;
let initialized = false;
export async function lookupIp(ip: string): Promise<{ latitude: number; longitude: number; country: string } | undefined> {
if (!initialized) {
reloadIpLocationApi();
return undefined;
}
try {
return await lookup(ip) ?? undefined;
}
catch {
return undefined;
}
}
let reloading: Promise<void> | undefined;
let log: debug.Debugger | undefined;
export async function reloadIpLocationApi() {
log ??= mainLog.extend("IP-Location-API");
if (reloading)
return reloading;
reloading = new Promise((resolve, reject) => {
log?.("Reloading IP location API");
reload({ fields, dataDir, tmpDataDir, smallMemory, autoUpdate: "0 9 * * *" }).then(() => {
log?.("IP location API reloaded");
initialized = true;
reloading = undefined;
resolve();
}).catch(reject);
});
return reloading;
}

View File

@@ -0,0 +1,25 @@
import http from "node:http";
import { mainLog } from "../index.js";
import { register } from "../tracing.js";
export function setupServer() {
const server = http.createServer(async (req, res) => {
//* If it's a head request, just return 200
if (req.method === "HEAD")
return res.writeHead(200).end();
//* If it's a favicon request, just return 404
if (req.url === "/favicon.ico")
return res.writeHead(404).end();
//* Basic routing logic
res.writeHead(200, { "Content-Type": "text/plain" });
res.end(await register.metrics());
});
server.listen(9464, () => {
mainLog("Server running");
});
return server;
}

View File

@@ -0,0 +1,77 @@
import process from "node:process";
import pLimit from "p-limit";
import type { Gauge } from "prom-client";
import { mainLog, redis } from "../index.js";
import { insertIpData } from "./insertIpData.js";
export const updateActivePresenceGaugeLimit = pLimit(1);
let log: debug.Debugger | undefined;
const scanCount = Number.parseInt(process.env.SCAN_COUNT || "1000", 10);
export async function updateActivePresenceGauge(gauge: Gauge) {
await updateActivePresenceGaugeLimit(async () => {
log ??= mainLog.extend("Heartbeat-Updates");
log?.("Starting active presence gauge update");
const pattern = "pmd-api.heartbeatUpdates.*";
let cursor: string = "0";
const serviceCounts = new Map<string, number>();
const ips = new Map<string, {
presences: Set<string>;
sessions: number;
}>();
do {
const [newCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", scanCount);
cursor = newCursor;
//* Use pipelining for batch Redis operations
const pipeline = redis.pipeline();
keys.forEach(key => pipeline.hmget(key, "service", "version", "ip_address"));
const hashes = await pipeline.exec();
if (!hashes) {
log?.("No hashes found");
return;
}
hashes.forEach(([err, hash]) => {
if (err || !Array.isArray(hash))
return;
const [service, version, ip] = hash;
const serviceVersion = service && version ? `${service}:${version}` : "none";
serviceCounts.set(serviceVersion, (serviceCounts.get(serviceVersion) || 0) + 1);
if (ip) {
const ipData = ips.get(ip) || { presences: new Set(), sessions: 0 };
if (serviceVersion !== "none")
ipData.presences.add(serviceVersion);
ipData.sessions++;
ips.set(ip, ipData);
}
});
} while (cursor !== "0");
log?.("Updating active presence gauge");
//* Batch update the gauge
gauge.reset();
for (const [serviceVersion, count] of serviceCounts) {
const [presence_name, version] = serviceVersion.split(":");
gauge.set({ presence_name, version }, count);
}
//* Convert IP data for insertion
const ipDataForInsertion = new Map(
Array.from(ips, ([ip, data]) => [ip, {
presences: Array.from(data.presences),
sessions: data.sessions,
}]),
);
await insertIpData(ipDataForInsertion);
log?.("Active presence gauge update completed");
});
}

View File

@@ -1,21 +1,44 @@
import process from "node:process";
import { CronJob } from "cron";
import debug from "debug";
import { clearOldSessions } from "./functions/clearOldSessions.js";
import createRedis from "./functions/createRedis.js";
import { clearOldSesssions } from "./functions/clearOldSessions.js";
import "./tracing.js";
import { reloadIpLocationApi } from "./functions/lookupIp.js";
import { cleanupOldUserData } from "./functions/cleanupOldUserData.js";
import { setupServer } from "./functions/setupServer.js";
export const redis = createRedis();
export const server = setupServer();
export const mainLog = debug("api-master");
debug("Starting cron job to clear old sessions");
debug("Starting cron jobs");
void reloadIpLocationApi();
void new CronJob(
// Every 5 seconds
"*/5 * * * * *",
async () => {
clearOldSesssions();
() => {
if (process.env.DISABLE_CLEAR_OLD_SESSIONS !== "true") {
clearOldSessions();
}
},
undefined,
true,
);
void new CronJob(
// Every day at 1am
"0 1 * * *",
() => {
cleanupOldUserData(14); // Keep 14 days of data
},
undefined,
true,
undefined,
undefined,
true,
);

View File

@@ -0,0 +1,41 @@
import process from "node:process";
import { Counter, Gauge, Registry, collectDefaultMetrics } from "prom-client";
import { updateActivePresenceGauge, updateActivePresenceGaugeLimit } from "./functions/updateActivePresenceGauge.js";
import { redis } from "./index.js";
const scanCount = Number.parseInt(process.env.SCAN_COUNT || "1000", 10);
export const register = new Registry();
collectDefaultMetrics({ register });
export const activeSessionsCounter = new Counter({
name: "active_sessions",
help: "Number of active sessions",
async collect() {
this.reset();
let length = 0;
let cursor = "0";
do {
const reply = await redis.scan(cursor, "MATCH", "pmd-api.sessions.*", "COUNT", scanCount);
cursor = reply[0];
length += reply[1].length;
} while (cursor !== "0");
this.inc(length);
},
});
export const activePresencesCounter = new Gauge({
name: "active_presences",
help: "Number of active presences",
labelNames: ["presence_name", "version"],
async collect() {
if (process.env.DISABLE_ACTIVE_PRESENCE_GAUGE !== "true") {
this.reset();
updateActivePresenceGaugeLimit.clearQueue();
await updateActivePresenceGauge(this);
}
},
});
register.registerMetric(activeSessionsCounter);
register.registerMetric(activePresencesCounter);

View File

@@ -3,6 +3,7 @@
"compilerOptions": {
"composite": true,
"rootDir": "src",
"types": ["./environment.d.ts"],
"outDir": "dist"
},
"include": ["src/**/*"]

View File

@@ -2,5 +2,6 @@ declare namespace NodeJS {
export interface ProcessEnv {
NODE_ENV?: "development" | "production" | "test";
DATABASE_URL?: string;
SESSION_KEEP_ALIVE_INTERVAL?: string;
}
}

View File

@@ -1,7 +1,7 @@
{
"name": "@premid/api-worker",
"type": "module",
"version": "0.0.2",
"version": "0.0.18",
"private": true,
"description": "PreMiD's api",
"license": "MPL-2.0",
@@ -23,16 +23,20 @@
"@escape.tech/graphql-armor-max-directives": "^2.2.0",
"@escape.tech/graphql-armor-max-tokens": "^2.4.0",
"@fastify/websocket": "^10.0.1",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/exporter-prometheus": "^0.52.1",
"@opentelemetry/node": "^0.24.0",
"@premid/db": "workspace:*",
"@sentry/node": "^8.17.0",
"arktype": "2.0.0-beta.2",
"arktype": "2.0.0-rc.6",
"defu": "^6.1.4",
"discord-api-types": "^0.37.92",
"fastify": "^4.28.1",
"graphql": "^16.9.0",
"graphql-parse-resolve-info": "^4.13.0",
"graphql-yoga": "^5.6.0",
"ioredis": "^5.3.2",
"mongoose": "^8.5.1"
"mongoose": "^8.6.3"
},
"devDependencies": {
"@graphql-codegen/cli": "5.0.2",

View File

@@ -1,35 +1,37 @@
import { scope, type } from "arktype";
import type { FastifyRequest } from "fastify";
import WebSocket from "ws";
import type { RawData } from "ws";
import { REST } from "@discordjs/rest";
import { scope, type } from "arktype";
import { Routes } from "discord-api-types/v10";
import WebSocket from "ws";
import type { FastifyRequest } from "fastify";
import type { RawData } from "ws";
import { redis } from "../functions/createServer.js";
import { counter } from "../tracing.js";
const schema = scope({
token: {
"+": "delete",
"type": "'token'",
"token": "format.trim",
"expires": "unixTimestamp",
"token": "string.trim",
"expires": "number.epoch",
},
session: {
"+": "delete",
"type": "'session'",
"token": "format.trim",
"token": "string.trim",
},
validMessages: "token | session",
}).export();
export class Socket {
currentToken: typeof schema.token.infer | undefined;
currentSesssion: typeof schema.session.infer | undefined;
currentSession: typeof schema.session.infer | undefined;
discord = new REST({ version: "10", authPrefix: "Bearer" });
constructor(
public readonly socket: WebSocket.WebSocket,
public readonly request: FastifyRequest,
) {
counter.add(1);
socket.on("message", this.onMessage.bind(this));
socket.on("close", () => this.onClose());
}
@@ -53,7 +55,7 @@ export class Socket {
}
case "session": {
await redis.hdel("pmd-api.sessions", out.token);
this.currentSesssion = out;
this.currentSession = out;
break;
}
}
@@ -65,14 +67,16 @@ export class Socket {
}
async onClose() {
if (!this.currentToken || !this.currentSesssion)
counter.add(-1);
if (!this.currentToken || !this.currentSession)
return;
await redis.hset(
"pmd-api.sessions",
this.currentSesssion.token,
this.currentSession.token,
JSON.stringify({
session: this.currentSesssion.token,
session: this.currentSession.token,
token: this.currentToken.token,
lastUpdated: Date.now(),
}),

View File

@@ -0,0 +1,10 @@
import process from "node:process";
import { defu } from "defu";
const disabledFlags = process.env.DISABLED_FEATURE_FLAGS?.split(",") ?? [];
const flags = Object.fromEntries(disabledFlags.map(flag => [flag, false]));
export const featureFlags = defu(flags, {
WebSocketManager: true,
SessionKeepAlive: true,
});

View File

@@ -1,18 +1,19 @@
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { useSentry } from "@envelop/sentry";
import { maxAliasesPlugin } from "@escape.tech/graphql-armor-max-aliases";
import { maxDepthPlugin } from "@escape.tech/graphql-armor-max-depth";
import { maxDirectivesPlugin } from "@escape.tech/graphql-armor-max-directives";
import { maxTokensPlugin } from "@escape.tech/graphql-armor-max-tokens";
import type { FastifyReply, FastifyRequest } from "fastify";
import fastify from "fastify";
import { createSchema, createYoga } from "graphql-yoga";
import fastifyWebsocket from "@fastify/websocket";
import { resolvers } from "../graphql/resolvers/v5/index.js";
import fastify from "fastify";
import { createSchema, createYoga } from "graphql-yoga";
import type { FastifyReply, FastifyRequest } from "fastify";
import { Socket } from "../classes/Socket.js";
import { resolvers } from "../graphql/resolvers/v5/index.js";
import { sessionKeepAlive } from "../routes/sessionKeepAlive.js";
import { featureFlags } from "../constants.js";
import { presences } from "../routes/presences.js";
import createRedis from "./createRedis.js";
export interface FastifyContext {
@@ -46,7 +47,7 @@ export default async function createServer() {
maxDepthPlugin(),
maxDirectivesPlugin(),
maxTokensPlugin(),
useSentry(),
/* useSentry(), */
],
schema: createSchema<FastifyContext>({
resolvers,
@@ -84,6 +85,13 @@ export default async function createServer() {
});
});
app.get("/v5/feature-flags", async (request, reply) => {
void reply.send(featureFlags);
});
app.post("/v5/session-keep-alive", sessionKeepAlive);
app.get("/v5/presence/:service/:file", presences);
return app;
}

View File

@@ -1,13 +1,14 @@
import { type } from "arktype";
import type { MutationResolvers } from "../../../../generated/graphql-v5.js";
import { GraphQLError } from "graphql";
import { redis } from "../../../../functions/createServer.js";
import type { MutationResolvers } from "../../../../generated/graphql-v5.js";
const addScienceSchema = type({
identifier: "uuid & format.lowercase",
presences: "format.trim[]",
identifier: "string.uuid & string.lower",
presences: "string.trim[]",
platform: {
arch: "format.trim", // ? "format.trim" is just a string that is trimmed
os: "format.trim",
arch: "string.trim",
os: "string.trim",
},
});
@@ -15,7 +16,7 @@ const mutation: MutationResolvers["addScience"] = async (_parent, input) => {
const out = addScienceSchema(input);
if (out instanceof type.errors)
throw new Error(out.summary);
throw new GraphQLError(out.summary);
await redis.hset(
"pmd-api.scienceUpdates",

View File

@@ -1,37 +1,49 @@
import { type } from "arktype";
import { GraphQLError } from "graphql";
import type { MutationResolvers } from "../../../../generated/graphql-v5.js";
import { redis } from "../../../../functions/createServer.js";
const heartbeatSchema = type({
identifier: "uuid & format.lowercase",
presences: {
service: "format.trim",
version: "semver",
language: "format.trim",
since: "unixTimestamp",
"identifier": "string.uuid & string.lower",
"presence?": {
service: "string.trim",
version: "string.semver",
language: "string.trim",
since: "number.epoch",
},
extension: {
"version": "semver",
"language": "format.trim",
"extension": {
"version": "string.semver",
"language": "string.trim",
"connected?": {
app: "integer",
app: "number.integer",
discord: "boolean",
},
},
});
const mutation: MutationResolvers["heartbeat"] = async (_parent, input) => {
const mutation: MutationResolvers["heartbeat"] = async (_parent, input, context) => {
const out = heartbeatSchema(input);
if (out instanceof type.errors)
throw new Error(out.summary);
throw new GraphQLError(out.summary);
// ! Disabled for now
/* await redis.setex(
`pmd-api.heartbeatUpdates.${data.identifier}`,
// 5 minutes
300,
JSON.stringify(data)
); */
//* Get the user's IP address from Cloudflare headers or fallback to the request IP
const userIp = context.request.headers.get("cf-connecting-ip") || context.request.ip;
// * Use Redis Hash with 'service' in the key to store heartbeat data
const redisKey = `pmd-api.heartbeatUpdates.${out.identifier}`;
await redis.hset(redisKey, {
service: out.presence?.service,
version: out.presence?.version,
language: out.presence?.language,
since: out.presence?.since.toString(),
extension_version: out.extension.version,
extension_language: out.extension.language,
extension_connected_app: out.extension.connected?.app?.toString(),
extension_connected_discord: out.extension.connected?.discord?.toString(),
ip_address: userIp,
});
await redis.expire(redisKey, 300);
return {
__typename: "HeartbeatResult",

View File

@@ -1,6 +1,6 @@
import { Presence } from "@premid/db";
import type { PresenceSchema } from "@premid/db/Presence.js";
import { parseResolveInfo } from "graphql-parse-resolve-info";
import type { PresenceSchema } from "@premid/db/Presence.js";
import type { FilterQuery } from "mongoose";
import type { QueryResolvers } from "../../../../generated/graphql-v5.js";

View File

@@ -1,6 +1,6 @@
import type { Resolvers } from "../../../generated/graphql-v5.js";
import { Query } from "./Query/index.js";
import { Mutation } from "./Mutation/index.js";
import { Query } from "./Query/index.js";
export const resolvers: Resolvers = {
Query,

View File

@@ -1,9 +1,5 @@
type Mutation {
addScience(
identifier: String!
presences: [String!]!
platform: PlatformInput!
): AddScienceResult
addScience(identifier: String!, presences: [String!]!, platform: PlatformInput!): AddScienceResult
}
input PlatformInput {

View File

@@ -19,6 +19,7 @@ type Presence {
}
type PresenceMetadata {
apiVersion: Int!
author: PresenceMetadataUser!
contributors: [PresenceMetadataUser!]
altnames: [String!]
@@ -49,14 +50,9 @@ type PresenceMetadataSettings {
id: String!
title: String
icon: String
if: PresenceMetadataSettingsIf # serialize
if: Scalar # serialize
placeholder: String
value: Scalar # serialize
values: Scalar # serialize
multiLanguage: Scalar # serialize
}
type PresenceMetadataSettingsIf {
propertyNames: String
patternProperties: Scalar
}

View File

@@ -2,7 +2,7 @@
import process from "node:process";
import * as Sentry from "@sentry/node";
import { connect } from "mongoose";
import "./tracing.js";
import createServer from "./functions/createServer.js";
// TODO SETUP SENTRY
@@ -16,7 +16,7 @@ Sentry.init({
if (!process.env.DATABASE_URL)
throw new Error("DATABASE_URL is not set");
await connect(process.env.DATABASE_URL, { appName: "PreMiD API" });
await connect(process.env.DATABASE_URL, { appName: "PreMiD API", dbName: "PreMiD" });
const server = await createServer();
const url = await server.listen({

View File

@@ -0,0 +1,34 @@
import { Presence } from "@premid/db";
import { type } from "arktype";
import type { FastifyReply, FastifyRequest } from "fastify";
const schema = type({
service: "string.trim",
file: "'metadata.json'|'presence.js'|'iframe.js'",
});
export async function presences(request: FastifyRequest, reply: FastifyReply) {
const out = schema(request.params);
if (out instanceof type.errors)
return reply.status(400).send({ code: "INVALID_PARAMS", message: out.message });
const service = decodeURIComponent(out.service);
const { file } = out;
const presence = await Presence.findOne({ "metadata.service": service });
if (!presence)
return reply.status(404).send({ code: "PRESENCE_NOT_FOUND", message: "The presence was not found" });
switch (file) {
case "metadata.json":
return reply.status(200).type("application/json").send(presence.metadata);
case "presence.js":
return reply.status(200).type("application/javascript").send(presence.presenceJs);
case "iframe.js":
if (!presence.iframeJs)
return reply.status(404).send({ code: "IFRAME_NOT_FOUND", message: "The presence does not have an iframe" });
return reply.status(200).type("application/javascript").send(presence.iframeJs);
}
}

View File

@@ -0,0 +1,62 @@
import process from "node:process";
import { REST } from "@discordjs/rest";
import { type } from "arktype";
import { Routes } from "discord-api-types/v10";
import type { FastifyReply, FastifyRequest } from "fastify";
import { redis } from "../functions/createServer.js";
import { featureFlags } from "../constants.js";
const schema = type({
token: "string.trim",
session: "string.trim",
version: "string.semver & string.trim",
scienceId: "string.trim",
});
export async function sessionKeepAlive(request: FastifyRequest, reply: FastifyReply) {
if (!featureFlags.SessionKeepAlive)
return reply.status(202).send();
//* Get the headers
const out = schema({
token: request.headers["x-token"],
session: request.headers["x-session"],
version: request.headers["x-version"] ?? "2.6.8",
scienceId: request.headers["x-science-id"] ?? request.headers["x-token"],
});
if (out instanceof type.errors)
return reply.status(400).send({ code: "MISSING_HEADERS", message: out.message });
if (!await isTokenValid(out.token))
return reply.status(400).send({ code: "INVALID_TOKEN", message: "The token is invalid" });
const redisKey = `pmd-api.sessions.${out.scienceId}`;
await redis.hset(redisKey, {
session: out.session,
token: out.token,
lastUpdated: Date.now(),
});
await redis.expire(redisKey, 300); // 5 minutes
const interval = Number.parseInt(process.env.SESSION_KEEP_ALIVE_INTERVAL ?? "5000"); // 5 seconds
return reply.status(200).send({
code: "OK",
message: "Session updated",
nextUpdate: interval,
});
}
async function isTokenValid(token: string) {
const discord = new REST({ version: "10", authPrefix: "Bearer" });
discord.setToken(token);
try {
await discord.get(Routes.user());
return true;
}
catch {
return false;
}
}

View File

@@ -0,0 +1,18 @@
import { ValueType } from "@opentelemetry/api";
import { PrometheusExporter } from "@opentelemetry/exporter-prometheus";
import { MeterProvider } from "@opentelemetry/sdk-metrics";
const prometheusExporter = new PrometheusExporter();
const provider = new MeterProvider({
readers: [prometheusExporter],
});
const meter = provider.getMeter("nice");
export const counter = meter.createUpDownCounter("active_activites", {
description: "Number of active activities",
valueType: ValueType.INT,
});
prometheusExporter.startServer();

View File

@@ -0,0 +1,25 @@
{
"name": "@premid/discord-bot",
"type": "module",
"version": "1.0.5",
"private": true,
"description": "PreMiD's discord bot",
"license": "MPL-2.0",
"main": "dist/index.js",
"files": [
"dist"
],
"scripts": {
"start": "node --enable-source-maps .",
"dev": "node --watch --env-file .env --enable-source-maps ."
},
"dependencies": {
"@premid/db": "workspace:*",
"@sentry/node": "^8.17.0",
"defu": "^6.1.4",
"discord.js": "^14.16.2",
"glob": "^11.0.0",
"mongoose": "^8.2.0",
"winston": "^3.14.2"
}
}

View File

@@ -0,0 +1,61 @@
import type { ChatInputCommandInteraction } from "discord.js";
import { SlashCommandBuilder } from "discord.js";
import { BetaUsers } from "@premid/db";
import { createStandardEmbed } from "../util/createStandardEmbed.js";
import type { Command } from "../util/loadCommands.js";
import { client, processEnv } from "../constants.js";
export default {
data: new SlashCommandBuilder()
.setName("beta")
.setDescription("Join or leave the beta program")
.addSubcommand(subcommand =>
subcommand
.setName("leave")
.setDescription("Leave the beta program"),
).addSubcommand(subcommand =>
subcommand
.setName("join")
.setDescription("Join the beta program"),
),
execute: async (interaction: ChatInputCommandInteraction) => {
if (!interaction.inGuild())
return;
if (interaction.options.getSubcommand() === "leave") {
const user = await BetaUsers.findOne({ userId: interaction.user.id });
if (!user) {
return interaction.reply({ content: "You are not a beta tester", ephemeral: true });
}
await BetaUsers.deleteOne({ userId: interaction.user.id });
const member = client.guilds.cache.get(interaction.guildId)?.members.cache.get(interaction.user.id);
await member?.roles.remove(processEnv.BETA_ROLE, "Left the beta program");
return interaction.reply({ content: "You are no longer a beta tester", ephemeral: true });
}
else if (interaction.options.getSubcommand() === "join") {
await BetaUsers.updateOne({ userId: interaction.user.id }, { $set: { userId: interaction.user.id } }, { upsert: true });
const member = client.guilds.cache.get(interaction.guildId)?.members.cache.get(interaction.user.id);
await member?.roles.add(processEnv.BETA_ROLE, "Joined the beta program");
return interaction.reply({ content: "You are now a beta tester", ephemeral: true });
}
else {
return interaction.reply({ content: "Invalid subcommand", ephemeral: true });
}
},
help: {
name: "beta",
value: "beta",
command: "/beta <join/leave>",
commandDescription: "Join or leave the beta program",
embed: createStandardEmbed({
title: "Command: /beta <join/leave>",
description: "Join or leave the beta program",
fields: [
{ name: "Usage", value: "`/beta <join/leave>`", inline: true },
{ name: "Example", value: "`/beta join`", inline: true },
],
}),
},
} satisfies Command;

View File

@@ -0,0 +1,83 @@
import {
ActionRowBuilder,
type AutocompleteInteraction,
ButtonBuilder,
ButtonStyle,
type ChatInputCommandInteraction,
SlashCommandBuilder,
} from "discord.js";
import { createStandardEmbed } from "../util/createStandardEmbed.js";
import { type Command, commands } from "../util/loadCommands.js";
export default {
data: new SlashCommandBuilder()
.setName("help")
.setDescription("Shows help and usage information for PreMiD commands")
.addStringOption(option =>
option
.setName("command")
.setDescription("The specific command to get help for")
.setAutocomplete(true),
),
autocomplete: async (interaction: AutocompleteInteraction) => {
const focusedValue = interaction.options.getFocused();
const choices = [...commands.values()]
.filter(cmd => cmd.help)
.map(cmd => ({ name: cmd.help!.name, value: cmd.help!.value }));
const filtered = choices.filter(choice => choice.name.toLowerCase().includes(focusedValue.toLowerCase()));
return interaction.respond(filtered.slice(0, 25));
},
execute: async (interaction: ChatInputCommandInteraction) => {
const command = interaction.options.getString("command");
if (command) {
const help = [...commands.values()].find(({ help }) => help?.value === command)?.help;
if (!help)
return interaction.reply({ content: "Command not found", ephemeral: true });
return interaction.reply({
embeds: [help.embed],
ephemeral: true,
});
}
const generalCommands = [];
for (const cmd of commands.values()) {
if (cmd.help) {
if (cmd.help.command)
generalCommands.push(`\`${cmd.help.command}\` - ${cmd.help.commandDescription}`);
}
}
const embed = createStandardEmbed({
title: "🛠️ PreMiD Help",
description: "PreMiD is a simple, configurable utility that allows you to show what you're doing on the web in your Discord now playing status.",
fields: [
{
name: "🔧 Commands",
value: generalCommands.join("\n") || "No commands available",
inline: false,
},
{
name: "📚 Additional Information",
value: "Use `/help <command>` for detailed information about a specific command.",
inline: false,
},
],
});
return interaction.reply({
embeds: [embed],
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder().setLabel("View Website").setURL("https://premid.app/").setStyle(ButtonStyle.Link),
new ButtonBuilder().setLabel("View Store").setURL("https://premid.app/store").setStyle(ButtonStyle.Link),
new ButtonBuilder().setLabel("Downloads").setURL("https://premid.app/downloads").setStyle(ButtonStyle.Link),
),
],
ephemeral: true,
});
},
} satisfies Command;

View File

@@ -0,0 +1,289 @@
import type { APIButtonComponent, AutocompleteInteraction, ChatInputCommandInteraction, ColorResolvable } from "discord.js";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, SlashCommandBuilder } from "discord.js";
import { createStandardEmbed } from "../util/createStandardEmbed.js";
import type { Command } from "../util/loadCommands.js";
const shortInfos: {
[key: string]: {
title: string;
description: string;
emoji?: string;
image?: string;
color?: ColorResolvable;
links?: Partial<APIButtonComponent>[];
};
} = {
troubleshooting: {
title: "Troubleshooting",
emoji: "❓",
description:
"If you have problems with PreMiD, you should read our troubleshooting guide and if that doesn't help, create a new post in <#1019726199494279248>.",
links: [
{
label: "Troubleshooting Guide",
url: "https://docs.premid.app/troubleshooting",
},
],
},
modifiedClients: {
title: "Modified Clients",
color: "#FF5050",
description:
"Using a modified client is an violation of Discord's ToS and therefore you run the risk of losing your account. If you want to keep using Discord, you have to follow them and make sure you're not breaking any of the rules Discord. Even using modified clients for theming or other customizations are against Discord's ToS. If you don't believe us, read it yourself.",
links: [
{
label: "Discord's ToS",
url: "https://discordapp.com/terms",
},
{
label: "Discord's Tweet",
url: "https://twitter.com/discordapp/status/908000828690182145",
},
],
},
creatingAPresence: {
title: "Creating a Presence",
emoji: "🏗",
description:
"If you wish to add support for a service that does not have a Presence yet, you can either open an issue on GitHub to request the presence to be created or you create it yourself. If you wish to create a Presence for PreMiD you need to have basic knowledge of TypeScript. For more information and docs on how to create a Presence follow our documentation.",
links: [
{
label: "Documentation",
url: "https://docs.premid.app/dev/presence",
},
{
label: "Service Request",
url: "https://github.com/PreMiD/Presences/issues/new?assignees=&labels=Service+Request&template=service_request.yml",
},
],
},
docs: {
title: "Read the Docs",
description:
"If you have any questions regarding PreMiD, please read our documentation before creating a ticket. Presence development related queries should be redirected to <#607524579874832446>",
links: [
{
label: "Documentation",
url: "https://docs.premid.app",
},
],
},
website: {
title: "Visit Our Website",
emoji: "🌐",
description: "Press the button below to visit our website full of greatness.",
links: [
{
label: "Website",
url: "https://premid.app",
},
],
},
presenceStore: {
title: "Presence Store",
emoji: "🏪",
description: "Press the button below to visit our Presence Store full of the your favourite services!",
links: [{ label: "Presence Store", url: "https://premid.app/store" }],
},
downloadPreMiD: {
title: "Download PreMiD",
emoji: "📦",
description:
"You can download PreMiD's extension for your browser via the button below.\n**Note:** You no longer need to download any application!",
links: [{ label: "Downloads", url: "https://premid.app/downloads" }],
},
donate: {
title: "Donate",
emoji: "💵",
description:
"Want to support PreMiD's development? Great! You can do so by boosting our Discord server, which you will get a special role, or you can support us on Patreon!",
links: [
{ label: "Patreon", url: "https://patreon.com/Timeraa" },
{ label: "GitHub Sponsors", url: "https://github.com/sponsors/Timeraa" },
],
},
creatingATicket: {
title: "Creating a Support Ticket",
emoji: "🙋",
description:
"Recently, we have migrated to use Discord's new forum channels for our support system. You can now create a ticket by creating a new post in <#1019726199494279248>",
},
suggestingAPresence: {
title: "Suggesting a Presence",
emoji: "🗳",
description:
"If you'd like to suggest a presence, you can do this on our GitHub repository by creating a new issue with the Service Request template! If want to create a Presence yourself, you can find more information on our documentation",
links: [
{
label: "PreMiD Documentation",
url: "https://docs.premid.app/dev/presence",
},
{
label: "GitHub Repository",
url: "https://github.com/PreMiD/Presences",
},
{
label: "Service Request",
url: "https://github.com/PreMiD/Presences/issues/new?assignees=&labels=Service+Request&template=service_request.yml",
},
],
},
tos: {
title: "PreMiD and Discord",
emoji: "🧬",
description: "PreMiD is compliant to Discord's ToS and therefore you can use it without any risk of losing your Discord account.",
links: [
{
label: "Proof",
url: "https://twitter.com/discord/status/1233704070390669312",
},
],
},
unidentifiedDeveloper: {
title: "Allow apps from unidentified developers (macOS)",
description:
"Steps for **macOS Big Sur (11.0+)**:\n1. Right click on our installer.\n2. Click `Open` in the dropdown menu.\n3. Click `Open` in popup.\n\nSteps for **older macOS versions**:\n1. Open System Preferences.\n2. Go to the Security & Privacy tab.\n3. Click on the lock and enter your password or scan your fingerprint so you can make changes.\n4. Change the setting for 'Allow apps downloaded from' to 'App Store and identified developers' from just 'App Store'.",
},
reportingaPresenceBug: {
title: "Reporting a Presence bug",
emoji: "🐛",
description:
"If you've found an issue with a presence, it is important that you report your issue on the Presence repository so the bug is resolved within a timely fashion. You can report the bug using the Bug Report template, **ensuring you fill in the template properly**.",
links: [
{
label: "Presence Repository",
url: "https://github.com/PreMiD/Presences",
},
{
label: "Bug Report",
url: "https://github.com/PreMiD/Presences/issues/new?assignees=&labels=%F0%9F%90%9B+Bug&template=bug_report.yml&title=Service+Name+%7C+Service+URL",
},
],
},
adblockDetection: {
title: "Adblock Detection",
emoji: "🚫",
description:
"If our website has falsely detected the presence of an ad-blocker, you can simply press \"I don't want to support\" six times and you will be redirected to the download. Alternatively, you can find direct download links below.",
links: [
{
label: "Download Links",
url: "https://discord.com/channels/493130730549805057/527675240231206934/715852870062309386",
},
],
},
requestANewFeature: {
title: "Requesting a Presence feature",
emoji: "🗳",
description:
"Does a presence you use not support a crucial page or not support all the possible domains for the website? If you believe a presence should include more features, you should open an issue on the Presence Repository using the Feature Request template.",
links: [
{
label: "Template",
url: "https://github.com/PreMiD/Presences/issues/new?assignees=&labels=Feature+Request&template=feature_request.yml",
},
],
},
beta: {
title: "PreMiD Beta",
emoji: "✨",
description:
"Do you want cool new features? Want to use PreMiD with the browser version of Discord? Download the beta!",
links: [
{
label: "Beta Release Page",
url: "https://premid.app/beta",
},
],
},
frequentFixes: {
title: "Frequent fixes for Presence bugs",
emoji: "🗳",
description:
"There are some frequent fixes for presences, use the buttons to navigate to these.\n If this doesn't work, please submit your issue to <#1019726199494279248>",
links: [
{
label: "YouTube/Netflix",
url: "https://discord.com/channels/493130730549805057/527675240231206934/831995042469642251",
},
{
label: "YouTube",
url: "https://discord.com/channels/493130730549805057/527675240231206934/827037909504753704",
},
{
label: "General fix",
url: "https://discord.com/channels/493130730549805057/527675240231206934/723231955893747763",
},
],
},
};
export default {
data: new SlashCommandBuilder()
.setName("info")
.setDescription("Posts an information message")
.addStringOption(option =>
option
.setName("query")
.setDescription("The infomation message to search for")
.setAutocomplete(true),
)
.addUserOption(option =>
option
.setName("user")
.setDescription("User to mention")
.setRequired(false),
),
autocomplete: async (interaction: AutocompleteInteraction) => {
const focusedValue = interaction.options.getFocused();
const choices = Object.entries(shortInfos).map(([key, data]) => ({ name: data.title, value: key }));
const filtered = choices.filter(choice => choice.name.toLowerCase().includes(focusedValue.toLowerCase()));
return interaction.respond(filtered.slice(0, 25));
},
execute: async (interaction: ChatInputCommandInteraction) => {
const query = interaction.options.getString("query");
const user = interaction.options.getUser("user");
if (!query)
return interaction.reply({ content: "Please provide a query to search for", ephemeral: true });
const info = shortInfos[query];
if (!info)
return interaction.reply({ content: "No information found for that query", ephemeral: true });
const embed = createStandardEmbed({
title: `${info.emoji || "🔖"} ${info.title}`,
description: info.description,
});
let actionRow: ActionRowBuilder<ButtonBuilder> | undefined;
if (info.links) {
actionRow = new ActionRowBuilder<ButtonBuilder>();
for (const link of info.links) {
actionRow.addComponents(new ButtonBuilder({
style: ButtonStyle.Link,
...link,
}));
}
}
return interaction.reply({ embeds: [embed], content: user ? user.toString() : undefined, components: actionRow ? [actionRow] : undefined });
},
help: {
name: "info",
value: "info",
command: "/info <query> [user]",
commandDescription: "Posts an information message",
embed: createStandardEmbed({
title: "Command: /info",
description: "Posts an information message",
fields: [
{ name: "Usage", value: "`/info <query> [user]`", inline: true },
{ name: "Example", value: "`/info troubleshooting`\n`/info beta @User`", inline: true },
],
}),
},
} satisfies Command;

View File

@@ -0,0 +1,98 @@
import type { AutocompleteInteraction, ChatInputCommandInteraction } from "discord.js";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, SlashCommandBuilder } from "discord.js";
import { Presence } from "@premid/db";
import { createStandardEmbed } from "../util/createStandardEmbed.js";
import type { Command } from "../util/loadCommands.js";
import { getPresenceList } from "../util/presenceList.js";
import { client } from "../constants.js";
export default {
data: new SlashCommandBuilder()
.setName("presence")
.setDescription("Search for a presence")
.addStringOption(option =>
option
.setName("query")
.setDescription("The presence to search for")
.setAutocomplete(true)
.setRequired(true),
),
autocomplete: async (interaction: AutocompleteInteraction) => {
const focusedValue = interaction.options.getFocused();
const presenceList = getPresenceList();
const filtered = presenceList.filter(({ service }) => service.toLowerCase().includes(focusedValue.toLowerCase()));
return interaction.respond(filtered.slice(0, 25).map(({ service }) => ({ name: service, value: service })));
},
execute: async (interaction: ChatInputCommandInteraction) => {
const query = interaction.options.getString("query");
if (!query)
return interaction.reply({ content: "Please provide a query to search for", ephemeral: true });
const presence = await Presence.findOne({ name: query }, {
_id: false,
metadata: {
service: true,
author: { id: true },
contributors: { id: true },
url: true,
description: {
en: true,
},
logo: true,
color: true,
},
});
if (!presence)
return interaction.reply({ content: "Presence not found", ephemeral: true });
const embed = createStandardEmbed({
title: presence.metadata.service,
description: presence.metadata.description.en,
color: presence.metadata.color,
fields: presence.metadata.contributors?.length
? [{
name: "Contributors",
value: presence.metadata.contributors.map(contributor => `<@${contributor.id}>`).join(", "),
}]
: undefined,
});
embed.setURL(`https://${Array.isArray(presence.metadata.url) ? presence.metadata.url[0] : presence.metadata.url}`);
embed.setThumbnail(presence.metadata.logo);
const author = await client.users.fetch(presence.metadata.author.id).catch(() => null);
if (author) {
embed.setAuthor({
name: author.username,
iconURL: author.displayAvatarURL(),
});
}
return interaction.reply({ embeds: [embed], components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel("Open in Store")
.setURL(`https://premid.app/store/presences/${encodeURI(presence.metadata.service)}`)
.setStyle(ButtonStyle.Link),
),
] });
},
help: {
name: "presence",
value: "presence",
command: "/presence <query>",
commandDescription: "Search for a presence",
embed: createStandardEmbed({
title: "Command: /presence",
description: "Search for a presence",
fields: [
{ name: "Usage", value: "`/presence <query>`", inline: true },
{ name: "Example", value: "`/presence YouTube`", inline: true },
],
}),
},
} satisfies Command;

View File

@@ -0,0 +1,69 @@
import process from "node:process";
import { defu } from "defu";
import { Client, GatewayIntentBits, REST } from "discord.js";
export const processEnv = defu({
TOKEN: process.env.TOKEN,
DATABASE_URL: process.env.DATABASE_URL,
SENTRY_DSN: process.env.SENTRY_DSN,
}, {
TOKEN: "",
DATABASE_URL: "mongodb://localhost:27017/premid",
SENTRY_DSN: "",
GUILD_ID: "493130730549805057",
BETA_ROLE: "591284574823120909",
ALPHA_ROLE: "694481247564595211",
});
export const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildPresences],
presence: {
status: "online",
},
});
export const rest = new REST().setToken(processEnv.TOKEN);
export const roles = {
PROJECT_LEADER: "493135149274365975",
STAFF_COORDINATOR: "691382096878370837",
ADMINISTRATOR: "685969048399249459",
PROJECT_MANAGEMENT: "673682085608816652",
REVIEWER: "630445337143935009",
DEVELOPER: "1027665813525778436",
DESIGNER: "691386502566903850",
MODERATOR: "514546359865442304",
SUPPORT_AGENT: "566417964820070421",
MARKETING_DIRECTOR: "673681900476432387",
LOCALIZATION_MANAGER: "811262682408943616",
REPRESENTATIVE: "691384256672563332",
CONTRIBUTOR: "1032759805732978708",
PATRON: "515874214750715904",
DONATOR: "502165799172309013",
BOOSTER: "585532751663333383",
PROOFREADER: "522755339448483840",
TRANSLATOR: "502148045991968788",
PRESENCE_DEV: "606222296016879722",
} as const;
export const roleColors = {
PROJECT_LEADER: "#E43725",
STAFF_COORDINATOR: "#E43725",
ADMINISTRATOR: "#E43725",
PROJECT_MANAGEMENT: "#E43725",
REVIEWER: "#3BA576",
DEVELOPER: "#3BA576",
DESIGNER: "#3BA576",
MODERATOR: "#D67118",
SUPPORT_AGENT: "#D67118",
MARKETING_DIRECTOR: "#3BA576",
LOCALIZATION_MANAGER: "#3BA576",
REPRESENTATIVE: "#3BA576",
CONTRIBUTOR: "#EB459E",
PATRON: "#E5472F",
DONATOR: "#FFAA33",
BOOSTER: "#F265FF",
PROOFREADER: "#00B0E6",
TRANSLATOR: "#2286D0",
PRESENCE_DEV: "#96A5E9",
} as const;

View File

@@ -0,0 +1,31 @@
import { Events } from "discord.js";
import { DiscordUsers, Presence } from "@premid/db";
import { client, roles as rolesEnv } from "../constants.js";
client.on(Events.GuildMemberAdd, async (member) => {
const [presence] = await Promise.all([
Presence.findOne({
$or: [{ "metadata.author.id": member.id }, { "metadata.contributors.id": member.id }],
}, { name: true }),
DiscordUsers.updateOne(
{ userId: member.id },
{
$set: {
avatar: member.user.avatar,
created: member.user.createdTimestamp,
discriminator: member.user.discriminator,
userId: member.id,
username: member.user.username,
},
},
{ upsert: true },
),
]);
//* User should have Presence Developer Role
if (presence) {
if (!member.roles.cache.has(rolesEnv.PRESENCE_DEV)) {
await member.roles.add(rolesEnv.PRESENCE_DEV, "User should have Presence Developer Role");
}
}
});

View File

@@ -0,0 +1,12 @@
import { Events } from "discord.js";
import { AlphaUsers, BetaUsers, Credits, DiscordUsers } from "@premid/db";
import { client } from "../constants.js";
client.on(Events.GuildMemberRemove, async (member) => {
await Promise.all([
BetaUsers.deleteOne({ userId: member.id }),
AlphaUsers.deleteOne({ userId: member.id }),
DiscordUsers.deleteOne({ userId: member.id }),
Credits.deleteOne({ userId: member.id }),
]);
});

View File

@@ -0,0 +1,86 @@
import { Events } from "discord.js";
import { AlphaUsers, BetaUsers, Credits, DiscordUsers } from "@premid/db";
import { client, processEnv, roleColors, roles as rolesEnv } from "../constants.js";
client.on(Events.GuildMemberUpdate, async (oldMember, newMember) => {
const highestRole = newMember.roles.cache
.filter(role => (Object.values(rolesEnv) as string[]).includes(role.id))
.sort((a, b) => b.position - a.position)
.at(0);
await Promise.all([
DiscordUsers.updateOne(
{ userId: newMember.id },
{
$set: {
avatar: newMember.user.avatar,
created: newMember.user.createdTimestamp,
discriminator: newMember.user.discriminator,
userId: newMember.id,
username: newMember.user.username,
},
},
{ upsert: true },
),
highestRole
? Credits.updateOne(
{ userId: newMember.id },
{
$set: {
userId: newMember.id,
name: newMember.user.username,
tag: newMember.user.discriminator,
avatar: newMember.user.displayAvatarURL({
extension: "png",
forceStatic: false,
}),
premium_since: newMember.premiumSince !== null ? newMember.premiumSinceTimestamp! : undefined,
role: highestRole.name,
roleId: highestRole.id,
roles: newMember.roles.cache.filter(r => r.name !== "@everyone").map(r => r.name),
roleIds: newMember.roles.cache.filter(r => r.name !== "@everyone").map(r => r.id),
roleColor: roleColors[
Object.entries(rolesEnv).find(([, id]) => id === highestRole.id)![0] as keyof typeof roleColors
],
rolePosition: highestRole.position,
status: newMember.presence?.status ?? "offline",
flags: newMember.user.flags?.toArray() ?? [],
},
},
{ upsert: true },
)
: Promise.resolve(),
]);
const roles = newMember.roles.cache.map(role => role.id);
//* User should have Alpha Role
if (roles.includes(rolesEnv.BOOSTER) || roles.includes(rolesEnv.PATRON) || newMember.roles.cache.has(processEnv.ALPHA_ROLE)) {
if (!newMember.roles.cache.has(processEnv.ALPHA_ROLE)) {
await newMember.roles.add(processEnv.ALPHA_ROLE, "User should have Alpha Role");
}
if (newMember.roles.cache.has(processEnv.BETA_ROLE)) {
await newMember.roles.remove(processEnv.BETA_ROLE, "User should have Alpha Role");
}
await Promise.all([
AlphaUsers.updateOne({ userId: newMember.id }, { $set: { userId: newMember.id } }, { upsert: true }),
BetaUsers.deleteOne({ userId: newMember.id }),
]);
return;
}
//* User should have Beta Role
const betaUser = await BetaUsers.findOne({ userId: newMember.id });
if (roles.includes(rolesEnv.DONATOR) || betaUser || newMember.roles.cache.has(processEnv.BETA_ROLE) || oldMember.roles.cache.has(processEnv.ALPHA_ROLE)) {
if (newMember.roles.cache.has(processEnv.ALPHA_ROLE)) {
await newMember.roles.remove(processEnv.ALPHA_ROLE, "User should have Beta Role");
}
if (!newMember.roles.cache.has(processEnv.BETA_ROLE)) {
await newMember.roles.add(processEnv.BETA_ROLE, "User should have Beta Role");
}
await Promise.all([
BetaUsers.updateOne({ userId: newMember.id }, { $set: { userId: newMember.id } }, { upsert: true }),
AlphaUsers.deleteOne({ userId: newMember.id }),
]);
}
});

View File

@@ -0,0 +1,42 @@
import { Events, InteractionType } from "discord.js";
import { client } from "../constants.js";
import { commands } from "../util/loadCommands.js";
import { logger } from "../util/logger.js";
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.inGuild()) {
logger.debug("Interaction received outside of a guild, ignoring.");
return;
}
if (interaction.type === InteractionType.ApplicationCommand || interaction.type === InteractionType.ApplicationCommandAutocomplete) {
const { commandName } = interaction;
logger.info(`Command "${commandName}" ${interaction.type === InteractionType.ApplicationCommandAutocomplete ? "autocomplete" : "executed"} in guild ${interaction.guildId} by user ${interaction.user.id}`);
if (!commands.has(commandName.toLowerCase()))
return;
const command = commands.get(commandName.toLowerCase())!;
if (interaction.type === InteractionType.ApplicationCommandAutocomplete) {
if (command.autocomplete) {
try {
await command.autocomplete(interaction);
logger.debug(`Autocomplete for command "${commandName}" handled successfully`);
}
catch (error) {
logger.error(`Error handling autocomplete for command "${commandName}":`, error);
}
}
return;
}
try {
await command.execute(interaction);
logger.debug(`Command "${commandName}" executed successfully`);
}
catch (error) {
logger.error(`Error executing command "${commandName}":`, error);
}
}
});

View File

@@ -0,0 +1,180 @@
import type { GuildMember } from "discord.js";
import { Events } from "discord.js";
import { AlphaUsers, BetaUsers, Credits, Presence } from "@premid/db";
import { client, processEnv, roleColors, roles as rolesEnv } from "../constants.js";
import { logger } from "../util/logger.js";
client.once(Events.ClientReady, async () => {
logger.debug("Giving roles to members");
const guild = await client.guilds.fetch(processEnv.GUILD_ID);
const members = await guild.members.fetch();
let count = 0;
for (const [, member] of members) {
count++;
const roles = member.roles.cache.map(role => role.id);
//* User should have Alpha Role
if (roles.includes(rolesEnv.BOOSTER) || roles.includes(rolesEnv.PATRON) || member.roles.cache.has(processEnv.ALPHA_ROLE)) {
if (!member.roles.cache.has(processEnv.ALPHA_ROLE)) {
await member.roles.add(processEnv.ALPHA_ROLE, "User should have Alpha Role");
}
if (member.roles.cache.has(processEnv.BETA_ROLE)) {
await member.roles.remove(processEnv.BETA_ROLE, "User should have Alpha Role");
}
await Promise.all([
AlphaUsers.updateOne({ userId: member.id }, { $set: { userId: member.id } }, { upsert: true }),
BetaUsers.deleteOne({ userId: member.id }),
]);
}
else {
const betaUser = await BetaUsers.findOne({ userId: member.id });
//* User should have Beta Role
if (roles.includes(rolesEnv.DONATOR) || betaUser || member.roles.cache.has(processEnv.BETA_ROLE)) {
if (member.roles.cache.has(processEnv.ALPHA_ROLE)) {
await member.roles.remove(processEnv.ALPHA_ROLE, "User should have Beta Role");
}
if (!member.roles.cache.has(processEnv.BETA_ROLE)) {
await member.roles.add(processEnv.BETA_ROLE, "User should have Beta Role");
}
await Promise.all([
BetaUsers.updateOne({ userId: member.id }, { $set: { userId: member.id } }, { upsert: true }),
AlphaUsers.deleteOne({ userId: member.id }),
]);
}
}
if (count % 1000 === 0) {
logger.debug(`Processed ${count}/${members.size} members`);
}
}
logger.debug(`Gave roles to ${count}/${members.size} members`);
//* Presence Developers
logger.debug("Checking for presence developers");
const presenceDevelopers = await Presence.find({}, {
"metadata.author.id": true,
"metadata.contributors.id": true,
"_id": false,
});
for (const presenceDeveloper of [
...new Set(
presenceDevelopers
.map(presence => [presence.metadata.author.id, ...(presence.metadata.contributors?.map(c => c.id) || [])])
.flat(),
),
]) {
const member = guild.members.cache.get(presenceDeveloper);
if (!member)
continue;
if (!member.roles.cache.has(rolesEnv.PRESENCE_DEV)) {
await member.roles.add(rolesEnv.PRESENCE_DEV, "User should have Presence Developer Role");
}
}
logger.debug("Checked for presence developers");
//* Update Credits
const usersToCredit = new Set<GuildMember>();
for (const roleId of Object.values(rolesEnv)) {
const role = await guild.roles.fetch(roleId);
if (!role)
continue;
for (const member of role.members.values()) {
usersToCredit.add(member);
}
}
const usersToRemove = await Credits.find({ userId: { $nin: [...usersToCredit].map(member => member.user.id) } });
await Credits.bulkWrite([
...usersToRemove.map(user => ({ deleteOne: { filter: { userId: user.userId } } })),
...[...usersToCredit].map((member) => {
const highestRole = member.roles.cache
.filter(role => (Object.values(rolesEnv) as string[]).includes(role.id))
.sort((a, b) => b.position - a.position)
.at(0)!;
const color = roleColors[
Object.entries(rolesEnv).find(([, id]) => id === highestRole.id)![0] as keyof typeof roleColors
];
return {
updateOne: {
filter: { userId: member.id },
update: {
$set: {
userId: member.id,
name: member.user.username,
tag: member.user.discriminator,
avatar: member.user.displayAvatarURL({
extension: "png",
forceStatic: false,
}),
premium_since: member.premiumSince !== null ? member.premiumSinceTimestamp! : undefined,
role: highestRole.name,
roleId: highestRole.id,
roles: member.roles.cache.filter(r => r.name !== "@everyone").map(r => r.name),
roleIds: member.roles.cache.filter(r => r.name !== "@everyone").map(r => r.id),
roleColor: color,
rolePosition: highestRole.position,
status: member.presence?.status ?? "offline",
flags: member.user.flags?.toArray() ?? [],
},
},
upsert: true,
},
};
}),
]);
logger.debug("Updated Credits");
//* Beta can be requested from the website so we need to periodically check for that, presence developers are external too so we need to periodically check for that
setInterval(async () => {
//* Beta Users
logger.debug("Checking for beta users");
const betaUsers = await BetaUsers.find({});
const guild = await client.guilds.fetch(processEnv.GUILD_ID);
for (const betaUser of betaUsers) {
const member = guild.members.cache.get(betaUser.userId);
if (!member)
continue;
if (!member.roles.cache.has(processEnv.BETA_ROLE)) {
await member.roles.add(processEnv.BETA_ROLE, "User should have Beta Role");
}
}
logger.debug("Checked for beta users");
//* Presence Developers
logger.debug("Checking for presence developers");
const presenceDevelopers = await Presence.find({}, {
"metadata.author.id": true,
"metadata.contributors.id": true,
"_id": false,
});
for (const presenceDeveloper of [
...new Set(
presenceDevelopers
.map(presence => [presence.metadata.author.id, ...(presence.metadata.contributors?.map(c => c.id) || [])])
.flat(),
),
]) {
const member = guild.members.cache.get(presenceDeveloper);
if (!member)
continue;
if (!member.roles.cache.has(rolesEnv.PRESENCE_DEV)) {
await member.roles.add(rolesEnv.PRESENCE_DEV, "User should have Presence Developer Role");
}
}
logger.debug("Checked for presence developers");
}, 1000 * 60 * 5);
});

View File

@@ -0,0 +1,89 @@
import process from "node:process";
import { connect } from "mongoose";
import { Routes } from "discord.js";
import * as Sentry from "@sentry/node";
import { client, processEnv, rest } from "./constants.js";
import { getActivity } from "./util/getActivity.js";
import loadCommands, { commands } from "./util/loadCommands.js";
import loadEvents from "./util/loadEvents.js";
import { logger } from "./util/logger.js";
import { updatePresenceList } from "./util/presenceList.js";
Sentry.init({
integrations: [
Sentry.mongooseIntegration(),
],
dsn: processEnv.SENTRY_DSN,
});
logger.info("Starting bot initialization...");
try {
await loadCommands();
logger.info("Commands loaded successfully");
}
catch (error) {
logger.error("Error loading commands:", error);
process.exit(1);
}
try {
await loadEvents();
logger.info("Events loaded successfully");
}
catch (error) {
logger.error("Error loading events:", error);
process.exit(1);
}
try {
await connect(processEnv.DATABASE_URL, { appName: "PreMiD Discord Bot" });
logger.info("Successfully connected to database");
await updatePresenceList();
logger.info("Successfully updated presence list");
}
catch (error) {
logger.error("Error connecting to database:", error);
process.exit(1);
}
try {
await client.login(processEnv.TOKEN);
logger.info("Bot logged in successfully");
client.user?.setActivity(getActivity({}));
}
catch (error) {
logger.error("Failed to log in:", error);
process.exit(1);
}
client.once("ready", async (client) => {
logger.info(`Bot is ready! Logged in as ${client.user?.tag}`);
try {
//* Register guild-specific commands
await rest.put(Routes.applicationGuildCommands(client.application.id, processEnv.GUILD_ID), {
body: Array.from(commands.values()).map(({ data }) => data),
});
//* Clear global commands
await rest.put(Routes.applicationCommands(client.application.id), { body: [] });
logger.info("Successfully registered commands");
}
catch (error) {
logger.error("Failed to register commands:", error);
process.exit(1);
}
});
setInterval(async () => {
const newActivity = getActivity({
previous: client.user?.presence.activities[0]?.name,
});
client.user?.setActivity(newActivity);
logger.debug(`Updated bot activity to: ${newActivity.name}`);
}, 60000);
setInterval(() => {
updatePresenceList();
}, 1000 * 60 * 5);

View File

@@ -0,0 +1,25 @@
import { type APIEmbedField, type ColorResolvable, EmbedBuilder } from "discord.js";
interface StandardEmbedOptions {
title: string;
description: string;
fields?: APIEmbedField[];
footer?: string;
color?: number | string;
}
export function createStandardEmbed({
title,
description,
fields = [],
footer = "PreMiD",
color = "Blurple",
}: StandardEmbedOptions): EmbedBuilder {
return new EmbedBuilder()
.setColor(color as ColorResolvable)
.setTitle(title)
.setDescription(description)
.addFields(fields)
.setFooter({ text: footer })
.setTimestamp();
}

View File

@@ -0,0 +1,22 @@
import type { ActivitiesOptions } from "discord.js";
import { ActivityType } from "discord.js";
import { getPresenceList } from "./presenceList.js";
export function getActivity({ previous }: {
previous?: string;
}): ActivitiesOptions {
const presenceList = getPresenceList();
const statuses = presenceList.filter(status => status.service !== previous);
const selectedStatus = statuses[Math.floor(Math.random() * statuses.length)]!;
return {
type: selectedStatus.category === "music"
? ActivityType.Listening
: selectedStatus.category === "anime"
? ActivityType.Watching
: selectedStatus.category === "videos"
? ActivityType.Watching
: ActivityType.Playing,
name: selectedStatus.service,
};
}

View File

@@ -0,0 +1,39 @@
import { resolve } from "node:path";
import type { APIApplicationCommandOptionChoice, EmbedBuilder, SharedSlashCommandOptions, SlashCommandSubcommandsOnlyBuilder } from "discord.js";
import { glob } from "glob";
export interface CommandHelp extends APIApplicationCommandOptionChoice<string> {
embed: EmbedBuilder;
command?: string;
commandDescription?: string;
}
export interface Command {
data: SharedSlashCommandOptions<any> | SlashCommandSubcommandsOnlyBuilder;
execute: (interaction: any) => Promise<any>;
autocomplete?: (interaction: any) => Promise<any>;
help?: CommandHelp;
}
export const commands = new Map<string, Command>();
export default async function loadCommands() {
for (const file of await glob("*.js", { cwd: resolve(import.meta.dirname, "../commands") })) {
const imported = await import(`../commands/${file}`);
const name = typeof imported.default.data === "function" ? imported.default.data().name : imported.default.data.name;
commands.set(name.toLowerCase(), imported.default);
if (imported.default.init) {
try {
await imported.default.init();
}
catch (error) {
//* Failed to initialize command
console.error(`Failed to initialize command ${name}:`, error);
}
}
}
return commands;
}

View File

@@ -0,0 +1,8 @@
import { resolve } from "node:path";
import { glob } from "glob";
export default async function loadEvents() {
for (const file of await glob("*.js", { cwd: resolve(import.meta.dirname, "../events") })) {
import(`../events/${file}`);
}
}

View File

@@ -0,0 +1,41 @@
import process from "node:process";
import { createLogger, format, transports } from "winston";
export const logger = createLogger({
level: process.env.NODE_ENV === "production" ? (process.env.LOG_LEVEL || "info") : (process.env.LOG_LEVEL || "debug"),
format: format.combine(
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
format.errors({ stack: true }),
format.splat(),
format.json(),
),
transports: [
new transports.Console({
format: format.combine(
format.colorize(),
format.printf(({ timestamp, level, message, ...metadata }) => {
let msg = `${timestamp} [${level}] : ${message}`;
if (Object.keys(metadata).length > 0) {
msg += `\n${JSON.stringify(metadata, null, 2)}`;
}
return msg;
}),
),
}),
new transports.File({
filename: "error.log",
level: "error",
format: format.combine(
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
format.json(),
),
}),
new transports.File({
filename: "combined.log",
format: format.combine(
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
format.json(),
),
}),
],
});

View File

@@ -0,0 +1,17 @@
import { Presence } from "@premid/db";
import type { PresenceMetadataCategory } from "@premid/db/Presence.js";
import { logger } from "./logger.js";
let presenceList: { service: string; category: PresenceMetadataCategory }[] = [];
export async function updatePresenceList() {
presenceList = (await Presence.find({}, { metadata: { category: true, service: true } })).map(presence => ({
service: presence.metadata.service,
category: presence.metadata.category,
}));
logger.debug(`Updated presence list with ${presenceList.length} presences`);
}
export function getPresenceList() {
return presenceList;
}

View File

@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
}
}

View File

@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.app.json",
"compilerOptions": {
"types": ["@types/node"],
"noEmit": true
},
"include": ["src"]
}

View File

@@ -1,9 +1,9 @@
import crypto from "node:crypto";
import process from "node:process";
import type { RouteHandlerMethod } from "fastify";
import mime from "mime-types";
import { nanoid } from "nanoid";
import type { RouteHandlerMethod } from "fastify";
import keyv from "../keyv.js";

View File

@@ -1,8 +1,8 @@
import { Buffer } from "node:buffer";
import { readFile } from "node:fs/promises";
import type { RequestOptions } from "node:http";
import type { AddressInfo } from "node:net";
import { Buffer } from "node:buffer";
import { afterAll, beforeAll, describe, it } from "vitest";
import { createServer } from "../functions/createServer.js";

View File

@@ -1,9 +1,9 @@
import crypto from "node:crypto";
import process from "node:process";
import type { RouteHandlerMethod } from "fastify";
import { fileTypeFromBuffer } from "file-type";
import { nanoid } from "nanoid";
import type { RouteHandlerMethod } from "fastify";
import keyv from "../keyv.js";

View File

@@ -1,8 +1,8 @@
import crypto from "node:crypto";
import process from "node:process";
import type { RouteHandlerMethod } from "fastify";
import { nanoid } from "nanoid";
import type { RouteHandlerMethod } from "fastify";
import keyv from "../keyv.js";

View File

@@ -1,6 +1,6 @@
import { readFile } from "node:fs/promises";
import { Buffer } from "node:buffer";
import { readFile } from "node:fs/promises";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { createServer } from "../functions/createServer.js";

View File

@@ -1,6 +1,6 @@
import crypto from "node:crypto";
import { Buffer } from "node:buffer";
import crypto from "node:crypto";
import type { RouteHandlerMethod } from "fastify";
import isInCIDRRange from "../functions/isInCidRange.js";

View File

@@ -1,7 +1,7 @@
{
"name": "@premid/schema-server",
"type": "module",
"version": "1.0.3",
"version": "1.0.4",
"private": true,
"description": "A small service to serve the JSON schemas for PreMiD",
"license": "MPL-2.0",

View File

@@ -0,0 +1,260 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://schemas.premid.app/metadata/1.11",
"title": "Metadata",
"type": "object",
"description": "Metadata that describes a presence.",
"definitions": {
"user": {
"type": "object",
"description": "User information.",
"properties": {
"name": {
"type": "string",
"description": "The name of the user."
},
"id": {
"type": "string",
"description": "The Discord snowflake of the user.",
"pattern": "^\\d+$"
}
},
"additionalProperties": false,
"required": [
"name",
"id"
]
}
},
"properties": {
"$schema": {
"$comment": "This is required otherwise the schema will fail itself when it is applied to a document via $schema. This is optional so that validators that use this schema don't fail if the metadata doesn't have the $schema property.",
"type": "string",
"description": "The metadata schema URL."
},
"author": {
"$ref": "#/definitions/user",
"description": "The author of this presence."
},
"contributors": {
"type": "array",
"description": "Any extra contributors to this presence.",
"items": {
"$ref": "#/definitions/user"
}
},
"service": {
"type": "string",
"description": "The service this presence is for."
},
"altnames": {
"type": "array",
"description": "Alternative names for the service.",
"items": {
"type": "string",
"description": "An alternative name."
},
"minItems": 1
},
"description": {
"type": "object",
"description": "A description of the presence in multiple languages.",
"propertyNames": {
"type": "string",
"description": "The language key. The key must be languagecode(_REGIONCODE).",
"pattern": "^[a-z]{2}(?:_(?:[A-Z]{2}|[0-9]{1,3}))?$"
},
"patternProperties": {
"^[a-z]{2}(?:_(?:[A-Z]{2}|[0-9]{1,3}))?$": {
"type": "string",
"description": "The description of the presence in the key's language."
}
},
"additionalProperties": false,
"required": [
"en"
]
},
"url": {
"type": [
"string",
"array"
],
"description": "The service's website URL, or an array of URLs. Protocols should not be added.",
"pattern": "^(([a-z0-9-]+\\.)*[0-9a-z_-]+(\\.[a-z]+)+|(\\d{1,3}\\.){3}\\d{1,3}|localhost)$",
"items": {
"type": "string",
"description": "One of the service's website URLs.",
"pattern": "^(([a-z0-9-]+\\.)*[0-9a-z_-]+(\\.[a-z]+)+|(\\d{1,3}\\.){3}\\d{1,3}|localhost)$"
},
"minItems": 2
},
"version": {
"type": "string",
"description": "The SemVer version of the presence. Must just be major.minor.patch.",
"pattern": "^\\d+\\.\\d+\\.\\d+$"
},
"apiVersion": {
"type": "integer",
"description": "The Presence System version this Presence supports.",
"minimum": 1,
"maximum": 2
},
"logo": {
"type": "string",
"description": "The logo of the service this presence is for.",
"pattern": "^https?://.+\\.(png|jpe?g|gif|webp)$"
},
"thumbnail": {
"type": "string",
"description": "A thumbnail of the service this presence is for.",
"pattern": "^https?://.+\\.(png|jpe?g|gif|webp)$"
},
"color": {
"type": "string",
"description": "The theme color of the service this presence is for. Must be either a 6 digit or a 3 digit hex code.",
"pattern": "^#([A-Fa-f0-9]{3}){1,2}$"
},
"tags": {
"type": [
"array"
],
"description": "The tags for the presence.",
"items": {
"type": "string",
"description": "A tag.",
"pattern": "^[^A-Z\\s!\"#$%&'()*+,./:;<=>?@\\[\\\\\\]^_`{|}~]+$"
},
"minItems": 1
},
"category": {
"type": "string",
"description": "The category the presence falls under.",
"enum": [
"anime",
"games",
"music",
"socials",
"videos",
"other"
]
},
"iframe": {
"type": "boolean",
"description": "Whether or not the presence should run in IFrames."
},
"readLogs": {
"type": "boolean",
"description": "Whether or not the extension should be reading logs."
},
"regExp": {
"type": "string",
"description": "A regular expression used to match URLs for the presence to inject into."
},
"iFrameRegExp": {
"type": "string",
"description": "A regular expression used to match IFrames for the presence to inject into."
},
"button": {
"type": "boolean",
"description": "Controls whether the presence is automatically added when the extension is installed. For partner presences only."
},
"warning": {
"type": "boolean",
"description": "Shows a warning saying that it requires additional steps for the presence to function correctly."
},
"settings": {
"type": "array",
"description": "An array of settings the user can change in the presence.",
"items": {
"type": "object",
"description": "A setting.",
"properties": {
"id": {
"type": "string",
"description": "The ID of the setting."
},
"title": {
"type": "string",
"description": "The title of the setting. Required only if `multiLanguage` is disabled."
},
"icon": {
"type": "string",
"description": "The icon of the setting. Required only if `multiLanguage` is disabled.",
"pattern": "^fa([bsdrlt]|([-](brands|solid|duotone|regular|light|thin))) fa-[0-9a-z-]+$"
},
"if": {
"type": "object",
"description": "Restrict showing this setting if another setting is the defined value.",
"propertyNames": {
"type": "string",
"description": "The ID of the setting."
},
"patternProperties": {
"": {
"type": [
"string",
"number",
"boolean"
],
"description": "The value of the setting."
}
},
"additionalProperties": false
},
"placeholder": {
"type": "string",
"description": "The placeholder for settings that require input. Shown when the input is empty."
},
"value": {
"type": [
"string",
"number",
"boolean"
],
"description": "The default value of the setting. Not compatible with `values`."
},
"values": {
"type": "array",
"description": "The default values of the setting. Not compatible with `value`.",
"items": {
"type": [
"string",
"number",
"boolean"
],
"description": "The value of the setting."
}
},
"multiLanguage": {
"type": [
"string",
"boolean",
"array"
],
"description": "When false, multi-localization is disabled. When true, strings from the `general.json` file are available for use. When a string, it is the name of a file (excluding .json) of a used language from the localization GitHub repo. When an array of strings, it is all of the file names (excluding .json) of used languages from the localization GitHub repo.",
"items": {
"type": "string",
"description": "The name of a file from the localization GitHub repository."
}
}
},
"additionalProperties": false
}
}
},
"additionalProperties": false,
"required": [
"author",
"service",
"description",
"url",
"version",
"apiVersion",
"logo",
"thumbnail",
"color",
"tags",
"category"
]
}

View File

@@ -2,9 +2,9 @@ import { extname, resolve } from "node:path";
import process from "node:process";
import helmet from "@fastify/helmet";
import type { RequestGenericInterface } from "fastify";
import fastify from "fastify";
import { globby } from "globby";
import type { RequestGenericInterface } from "fastify";
export const app = fastify();

View File

@@ -99,10 +99,10 @@ export default defineNuxtConfig({
"@nuxtjs/seo",
"floating-vue/nuxt",
"@nuxtjs/device",
/* "@nuxt/scripts", */
"nuxt-typed-router",
"nuxt-security",
"@pinia/nuxt",
"@nuxt/scripts",
],
runtimeConfig: {
// Use NUXT_ prefixed env vars for nuxt config

View File

@@ -11,41 +11,41 @@
"postinstall": "nuxt prepare"
},
"dependencies": {
"@discord-user-card/vue": "^0.0.8",
"@discordjs/rest": "^2.3.0",
"@discord-user-card/vue": "^0.0.9",
"@discordjs/rest": "^2.4.0",
"@fortawesome/fontawesome-svg-core": "^6.6.0",
"@fortawesome/free-brands-svg-icons": "^6.6.0",
"@fortawesome/free-solid-svg-icons": "^6.6.0",
"@fortawesome/vue-fontawesome": "^3.0.8",
"@nuxt/fonts": "^0.7.1",
"@nuxt/image": "^1.7.0",
"@nuxt/kit": "^3.12.3",
"@nuxt/scripts": "^0.6.3",
"@nuxtjs/device": "^3.1.1",
"@nuxt/fonts": "^0.7.2",
"@nuxt/image": "^1.8.0",
"@nuxt/kit": "^3.13.1",
"@nuxt/scripts": "^0.8.5",
"@nuxtjs/device": "^3.2.2",
"@nuxtjs/google-fonts": "^3.2.0",
"@nuxtjs/i18n": "^8.3.1",
"@nuxtjs/seo": "2.0.0-rc.15",
"@pinia/nuxt": "^0.5.1",
"@rollup/rollup-linux-arm64-gnu": "^4.18.1",
"@nuxtjs/i18n": "^8.5.2",
"@nuxtjs/seo": "2.0.0-rc.21",
"@pinia/nuxt": "^0.5.4",
"@rollup/rollup-linux-arm64-gnu": "^4.21.2",
"@types/lodash": "^4.17.7",
"@types/tinycolor2": "^1.4.6",
"@unocss/nuxt": "^0.61.5",
"@unocss/reset": "^0.61.5",
"@unocss/transformer-directives": "^0.61.5",
"@vueuse/core": "^10.11.0",
"@vueuse/nuxt": "^10.11.0",
"@unocss/nuxt": "^0.62.3",
"@unocss/reset": "^0.62.3",
"@unocss/transformer-directives": "^0.62.3",
"@vueuse/core": "^11.0.3",
"@vueuse/nuxt": "^11.0.3",
"bowser": "^2.11.0",
"discord-api-types": "^0.37.92",
"discord-api-types": "^0.37.100",
"floating-vue": "^5.2.2",
"lodash": "^4.17.21",
"nuxt": "^3.12.3",
"nuxt": "^3.13.1",
"nuxt-graphql-client": "^0.2.35",
"nuxt-security": "2.0.0-rc.9",
"nuxt-typed-router": "^3.6.5",
"sass": "^1.77.8",
"sass": "^1.78.0",
"tinycolor2": "^1.6.0",
"vue": "^3.4.32",
"vue-router": "^4.4.0",
"vue-tsc": "^2.0.26"
"vue": "^3.5.3",
"vue-router": "^4.4.3",
"vue-tsc": "^2.1.6"
}
}

View File

@@ -1,7 +1,7 @@
<script setup lang="ts">
import type { LocationQuery } from "vue-router";
import { useExtensionStore } from "~/stores/useExtension";
import breakpoints from "~/breakpoints";
import { useExtensionStore } from "~/stores/useExtension";
const extension = useExtensionStore();
@@ -19,17 +19,23 @@ const selectedCategory = ref("");
const sortBy = ref<string>("");
const pageSize = ref(9);
const totalPages = computed(() => Math.ceil(data.value.presences.length / pageSize.value));
const totalPages = computed(() =>
Math.ceil(data.value.presences.length / pageSize.value),
);
const presences = computed(() => {
const startIndex = (currentPage.value - 1) * pageSize.value;
const endIndex = startIndex + pageSize.value;
const sortedPresences = (
selectedCategory.value
? data.value.presences.filter(p => p.metadata.category === selectedCategory.value)
? data.value.presences.filter(
p => p.metadata.category === selectedCategory.value,
)
: data.value.presences
)
.filter(p => p.metadata.service.toLowerCase().includes(searchTerm.value.toLowerCase()))
.filter(p =>
p.metadata.service.toLowerCase().includes(searchTerm.value.toLowerCase()),
)
.sort((a, b) => {
if (sortBy.value === t("component.searchBar.sort.mostUsed"))
return b.users - a.users;
@@ -37,7 +43,7 @@ const presences = computed(() => {
return a.metadata.service.localeCompare(b.metadata.service);
return 0;
})
.sort(a => extension.presences.includes(a.metadata.service) ? -1 : 1);
.sort(a => (extension.presences.includes(a.metadata.service) ? -1 : 1));
return {
data: sortedPresences.slice(startIndex, endIndex),
@@ -48,7 +54,9 @@ const presences = computed(() => {
async function handleQuery(query: LocationQuery) {
const pageQuery = query.page?.toString() || "1";
const parsedPage = Number.parseInt(Number.isNaN(Number(pageQuery)) ? "1" : pageQuery);
const parsedPage = Number.parseInt(
Number.isNaN(Number(pageQuery)) ? "1" : pageQuery,
);
currentPage.value = Math.max(1, Math.min(parsedPage, totalPages.value));
searchTerm.value = query.search?.toString() || "";
selectedCategory.value = query.category?.toString() || "";
@@ -71,10 +79,16 @@ onUnmounted(() => {
window.removeEventListener("resize", resizePageSize);
});
function getLinkProperties({ page = currentPage.value, search = searchTerm.value, category = selectedCategory.value }) {
function getLinkProperties({
page = currentPage.value,
search = searchTerm.value,
category = selectedCategory.value,
}) {
const query = { category, page, search };
return {
query: Object.fromEntries(Object.entries(query).filter(([, value]) => value !== "")),
query: Object.fromEntries(
Object.entries(query).filter(([, value]) => value !== ""),
),
};
}
@@ -101,18 +115,35 @@ onMounted(() => {
<div>
<StoreSearchBar v-model:sort-order="sortBy" />
<!-- Presences Grid or Empty State -->
<div v-if="presences.data.length === 0" class="flex justify-center items-center rounded-lg h-50">
<div class="flex flex-col items-center justify-center p-5 text-lg text-primary-highlight">
<div
v-if="presences.data.length === 0"
class="flex justify-center items-center rounded-lg h-50"
>
<div
class="flex flex-col items-center justify-center p-5 text-lg text-primary-highlight"
>
<FAIcon :icon="['fa', 'frown']" class="mb-2 text-3xl" />
<p>{{ $t("page.store.noPresence") }}</p>
</div>
</div>
<div v-if="presences.data.length > 0" class="items-center mt-5 flex-col flex sm:mt-10 min-h-688px">
<div class="gap-4 grid grid-cols-[fit-content(0%)] sm-md:grid-cols-[repeat(2,fit-content(0%))] lg:grid-cols-[repeat(3,fit-content(0%))] overflow-unset">
<StoreCard v-for="presence in presences.data" :key="presence.metadata.service" :presence="presence" />
<div
v-if="presences.data.length > 0"
class="items-center mt-5 flex-col flex sm:mt-10 min-h-688px"
>
<div
class="gap-4 grid grid-cols-[fit-content(0%)] sm-md:grid-cols-[repeat(2,fit-content(0%))] lg:grid-cols-[repeat(3,fit-content(0%))] overflow-unset"
>
<StoreCard
v-for="presence in presences.data"
:key="presence.metadata.service"
:presence="presence"
/>
</div>
<!-- Pagination -->
<div v-if="presences.data.length > 0" class="flex mt-5 mb-10 flex-wrap justify-center sticky z-40">
<div
v-if="presences.data.length > 0"
class="flex mt-5 mb-10 flex-wrap justify-center sticky z-40"
>
<NuxtLink
:to="getLinkProperties({ page: 1 })"
:replace="true"
@@ -148,7 +179,7 @@ onMounted(() => {
</template>
<style scoped>
#filters {
#filters {
&::-webkit-scrollbar {
width: 0.4rem;
height: 100%;

View File

@@ -1,4 +1,5 @@
import { library } from "@fortawesome/fontawesome-svg-core";
import { faBrave, faChrome, faDiscord, faEdge, faFirefox, faGithub, faOpera, faPatreon, faSafari, faXTwitter } from "@fortawesome/free-brands-svg-icons";
import {
faBars,
faBolt,
@@ -33,7 +34,6 @@ import {
faUsers,
faVideo,
} from "@fortawesome/free-solid-svg-icons";
import { faBrave, faChrome, faDiscord, faEdge, faFirefox, faGithub, faOpera, faPatreon, faSafari, faXTwitter } from "@fortawesome/free-brands-svg-icons";
export default defineNuxtPlugin(() => {
library.add(

View File

@@ -1,8 +1,8 @@
import { ActivityType, PresenceUpdateStatus, flagsToBadges } from "@discord-user-card/vue";
import { REST } from "@discordjs/rest";
import type { RESTGetAPIGuildMemberResult, RESTGetAPIGuildRolesResult } from "discord-api-types/v10";
import { Routes } from "discord-api-types/v10";
import type { DiscordUserCardActivity, DiscordUserCardUser } from "@discord-user-card/vue";
import { ActivityType, PresenceUpdateStatus, flagsToBadges } from "@discord-user-card/vue";
import type { RESTGetAPIGuildMemberResult, RESTGetAPIGuildRolesResult } from "discord-api-types/v10";
const { discord_bot_token } = useRuntimeConfig();
const rest = new REST({ version: "10" }).setToken(discord_bot_token);
@@ -61,7 +61,8 @@ const activities: Map<string, DiscordUserCardActivity[]> = new Map()
},
],
} satisfies DiscordUserCardActivity,
]).set("193714715631812608", [
])
.set("193714715631812608", [
{
type: ActivityType.Playing,
name: "Kotobade Asobou",
@@ -73,7 +74,8 @@ const activities: Map<string, DiscordUserCardActivity[]> = new Map()
],
startTimestamp: Date.now(),
} satisfies DiscordUserCardActivity,
]).set("205984221859151873", [
])
.set("205984221859151873", [
{
type: ActivityType.Playing,
name: "BeatLeader",
@@ -90,7 +92,8 @@ const activities: Map<string, DiscordUserCardActivity[]> = new Map()
},
],
} satisfies DiscordUserCardActivity,
]).set("152155870917033985", [
])
.set("152155870917033985", [
{
type: ActivityType.Watching,
name: "YouTube",

1
commitlint.config.js Normal file
View File

@@ -0,0 +1 @@
export default { extends: ["@commitlint/config-conventional"] };

View File

@@ -13,6 +13,6 @@
"lib"
],
"dependencies": {
"mongoose": "^8.2.0"
"mongoose": "^8.6.3"
}
}

View File

@@ -0,0 +1,11 @@
import mongoose, { Schema } from "mongoose";
export interface AlphaUsersSchema {
userId: string;
}
const alphaUsersSchema = new Schema<AlphaUsersSchema>({
userId: { required: true, type: String },
});
export default mongoose.model("AlphaUsers", alphaUsersSchema, "alphaUsers");

View File

@@ -0,0 +1,11 @@
import mongoose, { Schema } from "mongoose";
export interface BetaUsersSchema {
userId: string;
}
const betaUsersSchema = new Schema<BetaUsersSchema>({
userId: { required: true, type: String },
});
export default mongoose.model("BetaUsers", betaUsersSchema, "betaUsers");

View File

@@ -0,0 +1,35 @@
import mongoose, { Schema } from "mongoose";
export interface CreditsSchema {
userId: string;
avatar: string;
name: string;
premium_since: number;
role: string;
roleColor: string;
roleId: string;
roleIds: string[];
rolePosition: number;
roles: string[];
status: string;
tag: string;
flags: string[];
}
const creditsSchema = new Schema<CreditsSchema>({
userId: { required: true, type: String },
avatar: { required: true, type: String },
name: { required: true, type: String },
premium_since: { required: true, type: Number },
role: { required: true, type: String },
roleColor: { required: true, type: String },
roleId: { required: true, type: String },
roleIds: { required: true, type: [String] },
rolePosition: { required: true, type: Number },
roles: { required: true, type: [String] },
status: { required: true, type: String },
tag: { required: true, type: String },
flags: { required: true, type: [String] },
});
export default mongoose.model("Credits", creditsSchema, "credits");

View File

@@ -0,0 +1,15 @@
import mongoose, { Schema } from "mongoose";
export interface DiscordUsersSchema {
userId: string;
username: string;
avatar: string;
discriminator: string;
created: number;
}
const discordUsersSchema = new Schema<DiscordUsersSchema>({
userId: { required: true, type: String },
});
export default mongoose.model("DiscordUsers", discordUsersSchema, "discordUsers");

View File

@@ -12,6 +12,7 @@ export interface PresenceSchema {
export interface PresenceMetadata {
$schema: string;
apiVersion: number;
altnames?: string[];
author: PresenceMetadataContributor;
category: PresenceMetadataCategory;
@@ -61,15 +62,15 @@ const PresenceMetadataSettingSchema = new Schema<PresenceMetadataSetting>({
placeholder: { type: String },
title: { type: String },
value: Schema.Types.Mixed,
values: { type: [String] },
values: { type: [Schema.Types.Mixed], default: undefined },
});
const PresenceMetadataSchema = new Schema<PresenceMetadata>({
$schema: { required: true, type: String },
altnames: { type: [String] },
altnames: { type: [String], default: undefined },
author: { required: true, type: PresenceMetadataContributorSchema },
category: { required: true, type: String },
color: { required: true, type: String },
contributors: { type: [PresenceMetadataContributorSchema] },
contributors: { type: [PresenceMetadataContributorSchema], default: undefined },
description: { required: true, type: Schema.Types.Mixed },
iFrameRegExp: { type: String },
iframe: { type: Boolean },
@@ -77,11 +78,12 @@ const PresenceMetadataSchema = new Schema<PresenceMetadata>({
readLogs: { type: Boolean },
regExp: { type: String },
service: { required: true, type: String },
settings: { type: [PresenceMetadataSettingSchema] },
settings: { type: [PresenceMetadataSettingSchema], default: undefined },
tags: { required: true, type: [String] },
thumbnail: { required: true, type: String },
url: { required: true, type: [String] },
url: { required: true, type: Schema.Types.Mixed },
version: { required: true, type: String },
apiVersion: { required: true, type: Number },
});
const presenceSchema = new Schema<PresenceSchema>({
folderName: { required: true, type: String },
@@ -93,4 +95,4 @@ const presenceSchema = new Schema<PresenceSchema>({
url: { required: true, type: String },
});
export default mongoose.model("Presence", presenceSchema);
export default mongoose.model("Presence", presenceSchema, "presences");

View File

@@ -1 +1,5 @@
export { default as AlphaUsers } from "./AlphaUsers.js";
export { default as BetaUsers } from "./BetaUsers.js";
export { default as Credits } from "./Credits.js";
export { default as DiscordUsers } from "./DiscordUsers.js";
export { default as Presence } from "./Presence.js";

9450
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -14,6 +14,9 @@
},
{
"path": "./apps/api-worker/tsconfig.app.json"
},
{
"path": "./apps/discord-bot/tsconfig.app.json"
}
],
"files": []

View File

@@ -29,5 +29,5 @@
"isolatedModules": true,
"skipLibCheck": true
},
"exclude": ["**/*/node_modules", "**/*/dist"]
"exclude": ["**/*/node_modules", "**/*/dist", "**/drizzle.config.ts"]
}