summary refs log tree commit diff
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@proton.me>2023-05-07 00:15:04 -0400
committerGitHub <noreply@github.com>2023-05-07 00:15:04 -0400
commitba0d1bb6ff678c5a451afd7dad51b27a2e770cd8 (patch)
tree734d2721cf99205da2439db34e6a399ec4b2546d
parentadd initial_guild_id client state property (diff)
parentfix build failure (diff)
downloadserver-ba0d1bb6ff678c5a451afd7dad51b27a2e770cd8.tar.xz
Merge pull request #1052 from spacebarchat/feat/auto-create-bot-users
Feat: Auto add bot users to new apps
-rw-r--r--assets/openapi.json5
-rw-r--r--assets/schemas.json5
-rw-r--r--src/api/routes/applications/#id/bot/index.ts19
-rw-r--r--src/api/routes/applications/index.ts8
-rw-r--r--src/util/config/types/GeneralConfiguration.ts1
-rw-r--r--src/util/util/Application.ts24
-rw-r--r--src/util/util/index.ts1
7 files changed, 45 insertions, 18 deletions
diff --git a/assets/openapi.json b/assets/openapi.json
index 5a330775..4d285cb1 100644
--- a/assets/openapi.json
+++ b/assets/openapi.json
@@ -7199,9 +7199,14 @@
                     },
                     "instanceId": {
                         "type": "string"
+                    },
+                    "autoCreateBotUsers": {
+                        "type": "boolean",
+                        "default": false
                     }
                 },
                 "required": [
+                    "autoCreateBotUsers",
                     "correspondenceEmail",
                     "correspondenceUserID",
                     "frontPage",
diff --git a/assets/schemas.json b/assets/schemas.json
index de301bfc..919d4981 100644
--- a/assets/schemas.json
+++ b/assets/schemas.json
@@ -422590,10 +422590,15 @@
             },
             "instanceId": {
                 "type": "string"
+            },
+            "autoCreateBotUsers": {
+                "type": "boolean",
+                "default": false
             }
         },
         "additionalProperties": false,
         "required": [
+            "autoCreateBotUsers",
             "correspondenceEmail",
             "correspondenceUserID",
             "frontPage",
diff --git a/src/api/routes/applications/#id/bot/index.ts b/src/api/routes/applications/#id/bot/index.ts
index 0a6e6fd4..3c431e3d 100644
--- a/src/api/routes/applications/#id/bot/index.ts
+++ b/src/api/routes/applications/#id/bot/index.ts
@@ -22,6 +22,7 @@ import {
 	BotModifySchema,
 	DiscordApiErrors,
 	User,
+	createAppBotUser,
 	generateToken,
 	handleFile,
 } from "@spacebar/util";
@@ -52,23 +53,7 @@ router.post(
 		if (app.owner.id != req.user_id)
 			throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
 
-		const user = await User.register({
-			username: app.name,
-			password: undefined,
-			id: app.id,
-			req,
-		});
-
-		user.id = app.id;
-		user.premium_since = new Date();
-		user.bot = true;
-
-		await user.save();
-
-		// flags is NaN here?
-		app.assign({ bot: user, flags: app.flags || 0 });
-
-		await app.save();
+		const user = await createAppBotUser(app, req);
 
 		res.send({
 			token: await generateToken(user.id),
diff --git a/src/api/routes/applications/index.ts b/src/api/routes/applications/index.ts
index 1e536a06..5bba3338 100644
--- a/src/api/routes/applications/index.ts
+++ b/src/api/routes/applications/index.ts
@@ -20,7 +20,9 @@ import { route } from "@spacebar/api";
 import {
 	Application,
 	ApplicationCreateSchema,
+	Config,
 	User,
+	createAppBotUser,
 	trimSpecial,
 } from "@spacebar/util";
 import { Request, Response, Router } from "express";
@@ -68,7 +70,11 @@ router.post(
 			flags: 0,
 		});
 
-		await app.save();
+		// april 14, 2023: discord made bot users be automatically added to all new apps
+		const { autoCreateBotUsers } = Config.get().general;
+		if (autoCreateBotUsers) {
+			await createAppBotUser(app, req);
+		} else await app.save();
 
 		res.json(app);
 	},
diff --git a/src/util/config/types/GeneralConfiguration.ts b/src/util/config/types/GeneralConfiguration.ts
index c20fe9a7..cff8c527 100644
--- a/src/util/config/types/GeneralConfiguration.ts
+++ b/src/util/config/types/GeneralConfiguration.ts
@@ -28,4 +28,5 @@ export class GeneralConfiguration {
 	correspondenceUserID: string | null = null;
 	image: string | null = null;
 	instanceId: string = Snowflake.generate();
+	autoCreateBotUsers: boolean = false;
 }
diff --git a/src/util/util/Application.ts b/src/util/util/Application.ts
new file mode 100644
index 00000000..23019a7f
--- /dev/null
+++ b/src/util/util/Application.ts
@@ -0,0 +1,24 @@
+import { Request } from "express";
+import { Application, User } from "../entities";
+
+export async function createAppBotUser(app: Application, req: Request) {
+	const user = await User.register({
+		username: app.name,
+		password: undefined,
+		id: app.id,
+		req,
+	});
+
+	user.id = app.id;
+	user.premium_since = new Date();
+	user.bot = true;
+
+	await user.save();
+
+	// flags is NaN here?
+	app.assign({ bot: user, flags: app.flags || 0 });
+
+	await app.save();
+
+	return user;
+}
diff --git a/src/util/util/index.ts b/src/util/util/index.ts
index 3a98be15..10e09b5c 100644
--- a/src/util/util/index.ts
+++ b/src/util/util/index.ts
@@ -42,3 +42,4 @@ export * from "./Token";
 export * from "./TraverseDirectory";
 export * from "./WebAuthn";
 export * from "./Gifs";
+export * from "./Application";