From 942cce913dcf00acc94f4a2cefc0902427a6c7b7 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sat, 6 May 2023 23:45:09 -0400 Subject: [PATCH 1/3] add option to auto add bot users to new apps --- assets/openapi.json | Bin 564151 -> 564440 bytes assets/schemas.json | Bin 17945785 -> 17946010 bytes src/api/routes/applications/index.ts | 21 ++++++++++++++++++ src/util/config/types/GeneralConfiguration.ts | 1 + 4 files changed, 22 insertions(+) diff --git a/assets/openapi.json b/assets/openapi.json index 2dc7874167f884022f583ac3deecd7cca58463a9..14bfb70a72a69640c6f42c304550442b15750a3f 100644 GIT binary patch delta 131 zcmdnKUFpV7rG^&97N#xC^>Wh}s4zNB7HE>2tgy*_dciD4*6Fr$8Pz7IvWh{<}kWXFPO#1I^A|IquS(@Ho@rvIgE1K8|0a@7J+0v crpp~>=50I7+;*6y?J#TGVYY3D*>Btg0Mj2EtpET3 diff --git a/assets/schemas.json b/assets/schemas.json index dc25cacd30a1452f3cea3414caf6dfbad404bf82..68ef81877393902f1c1c6d64efd0faa13478ac09 100644 GIT binary patch delta 1073 zcmY+)SyLiE46(Ofwid2-M)mdE>qprF{mnv4>)Ln7vq01Dn%ax!+xz$rw=t^CsUb@~oiw0<*1}RB{m8=xqsv#PxRHf-Q4byOq&`6EaXpPbB zx&BGH>5-mkFG%aht}r zny%i+s0>qE>am-g*p$v@Td60wVwKHaR8i)^LwFc7@Cari3$rjAk0Kj6n1fv8As+>p zi^uRdp1_lM3iB`@PvaRB;#n-fLI}^{c@*IVyoh3ypcIQxhH@;%5-f!m%di|3Fn9?s zqY|&+Rjj~DyoT4|!y9-LZ{cmcgLknCtFZ=a@gCks71m)rKEMWSL^VFdN7#hT*n+M2 z7@wd9+fa*7@fo&b2R_Fa*oivS<4b&n27HZg@GZVWBX(go_TYQ`fW7z;`!-dS`Rg-G zPJCQsL_vObTCQhmz9%(1Ki!*99BZZwOmdr?tlkdqxE0aHIk7Y|`hTZ9Pi|hKX>0bo zO+&!vH0{lPmpK*i#hCpEeGZd!h%IGuRESwWA?>L6zIDuB2#3{7lH2%OD{E4$Thx53AzwkFM;vf8r_LAfl sTRE(Buo7gYqZPZAU@IY3Lal^Z>14%WCEQAc6{i)Ktv0zO(l)R4KY*4WOaK4? delta 948 zcmW;J^?!{407v1^UEcMkYr4C;yW21w)7@?OGHmWJjER}Prj41}8rkSxs^wGl~4IqKm}Dug;hjFRZPWILM2s7rBy~{RZitqK^0X=l~qMmRZZ1ZLp4=P zwN*!TRZsQRKn>MMjnza=)lALRLM_!wt<^?t)lTizL4N9}PUr+Fc5<<7(*}=!!R5pFcPCM8e=dP<1ii*FcFh5 z8B;J7(=Z)@n1Pv?1z|SkAP93Y5AzX>5G;TNp;(ATSd1_%!BQ+kIF@4tBCryxuo`O+ ziM5ErI;_VAY{VvP#uiwy717v+?bv~x*oEELgT08sKI}&<4j>K(aR`TT1V<5%1RTS0 zoWMz(!f7Pp49?;l&f@|u;u0?73a;WBuHy!7;udb>PHciD$>#TeuYXFEXLw|Clv9w^ zmV~>whx;`aWdj;#B9XHh^rAdBko2#jCdOHGR0eM-lnwFf4j&N A>Hq)$ diff --git a/src/api/routes/applications/index.ts b/src/api/routes/applications/index.ts index 1e536a06..27300c4a 100644 --- a/src/api/routes/applications/index.ts +++ b/src/api/routes/applications/index.ts @@ -20,6 +20,7 @@ import { route } from "@spacebar/api"; import { Application, ApplicationCreateSchema, + Config, User, trimSpecial, } from "@spacebar/util"; @@ -68,6 +69,26 @@ router.post( flags: 0, }); + // april 14, 2023: discord made bot users be automatically added to all new apps + const { autoCreateBotUsers } = Config.get().general; + if (autoCreateBotUsers) { + 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(); 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; } From bb22e42da93f9e9a6ea5c00980cff9c865ea3c48 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sun, 7 May 2023 00:00:42 -0400 Subject: [PATCH 2/3] create util for app bot user creation --- src/api/routes/applications/#id/bot/index.ts | 19 ++-------------- src/api/routes/applications/index.ts | 21 +++-------------- src/util/util/Application.ts | 24 ++++++++++++++++++++ src/util/util/index.ts | 1 + 4 files changed, 30 insertions(+), 35 deletions(-) create mode 100644 src/util/util/Application.ts 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 27300c4a..5bba3338 100644 --- a/src/api/routes/applications/index.ts +++ b/src/api/routes/applications/index.ts @@ -22,6 +22,7 @@ import { ApplicationCreateSchema, Config, User, + createAppBotUser, trimSpecial, } from "@spacebar/util"; import { Request, Response, Router } from "express"; @@ -72,24 +73,8 @@ router.post( // april 14, 2023: discord made bot users be automatically added to all new apps const { autoCreateBotUsers } = Config.get().general; if (autoCreateBotUsers) { - 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(); + await createAppBotUser(app, req); + } else await app.save(); res.json(app); }, 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..6076f70a 100644 --- a/src/util/util/index.ts +++ b/src/util/util/index.ts @@ -18,6 +18,7 @@ export * from "./ApiError"; export * from "./Array"; +export * from "./Application"; export * from "./BitField"; //export * from "./Categories"; export * from "./cdn"; From f256209ff27ed5d5301e9266fdf7c1692a5f0511 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sun, 7 May 2023 00:06:56 -0400 Subject: [PATCH 3/3] fix build failure --- src/util/util/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/util/index.ts b/src/util/util/index.ts index 6076f70a..10e09b5c 100644 --- a/src/util/util/index.ts +++ b/src/util/util/index.ts @@ -18,7 +18,6 @@ export * from "./ApiError"; export * from "./Array"; -export * from "./Application"; export * from "./BitField"; //export * from "./Categories"; export * from "./cdn"; @@ -43,3 +42,4 @@ export * from "./Token"; export * from "./TraverseDirectory"; export * from "./WebAuthn"; export * from "./Gifs"; +export * from "./Application";