Merge pull request #1052 from spacebarchat/feat/auto-create-bot-users
Feat: Auto add bot users to new apps
This commit is contained in:
commit
ba0d1bb6ff
@ -7199,9 +7199,14 @@
|
|||||||
},
|
},
|
||||||
"instanceId": {
|
"instanceId": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
},
|
||||||
|
"autoCreateBotUsers": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": [
|
"required": [
|
||||||
|
"autoCreateBotUsers",
|
||||||
"correspondenceEmail",
|
"correspondenceEmail",
|
||||||
"correspondenceUserID",
|
"correspondenceUserID",
|
||||||
"frontPage",
|
"frontPage",
|
||||||
|
@ -422590,10 +422590,15 @@
|
|||||||
},
|
},
|
||||||
"instanceId": {
|
"instanceId": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
},
|
||||||
|
"autoCreateBotUsers": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
"required": [
|
"required": [
|
||||||
|
"autoCreateBotUsers",
|
||||||
"correspondenceEmail",
|
"correspondenceEmail",
|
||||||
"correspondenceUserID",
|
"correspondenceUserID",
|
||||||
"frontPage",
|
"frontPage",
|
||||||
|
@ -22,6 +22,7 @@ import {
|
|||||||
BotModifySchema,
|
BotModifySchema,
|
||||||
DiscordApiErrors,
|
DiscordApiErrors,
|
||||||
User,
|
User,
|
||||||
|
createAppBotUser,
|
||||||
generateToken,
|
generateToken,
|
||||||
handleFile,
|
handleFile,
|
||||||
} from "@spacebar/util";
|
} from "@spacebar/util";
|
||||||
@ -52,23 +53,7 @@ router.post(
|
|||||||
if (app.owner.id != req.user_id)
|
if (app.owner.id != req.user_id)
|
||||||
throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
|
throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
|
||||||
|
|
||||||
const user = await User.register({
|
const user = await createAppBotUser(app, req);
|
||||||
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.send({
|
res.send({
|
||||||
token: await generateToken(user.id),
|
token: await generateToken(user.id),
|
||||||
|
@ -20,7 +20,9 @@ import { route } from "@spacebar/api";
|
|||||||
import {
|
import {
|
||||||
Application,
|
Application,
|
||||||
ApplicationCreateSchema,
|
ApplicationCreateSchema,
|
||||||
|
Config,
|
||||||
User,
|
User,
|
||||||
|
createAppBotUser,
|
||||||
trimSpecial,
|
trimSpecial,
|
||||||
} from "@spacebar/util";
|
} from "@spacebar/util";
|
||||||
import { Request, Response, Router } from "express";
|
import { Request, Response, Router } from "express";
|
||||||
@ -68,7 +70,11 @@ router.post(
|
|||||||
flags: 0,
|
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);
|
res.json(app);
|
||||||
},
|
},
|
||||||
|
@ -28,4 +28,5 @@ export class GeneralConfiguration {
|
|||||||
correspondenceUserID: string | null = null;
|
correspondenceUserID: string | null = null;
|
||||||
image: string | null = null;
|
image: string | null = null;
|
||||||
instanceId: string = Snowflake.generate();
|
instanceId: string = Snowflake.generate();
|
||||||
|
autoCreateBotUsers: boolean = false;
|
||||||
}
|
}
|
||||||
|
24
src/util/util/Application.ts
Normal file
24
src/util/util/Application.ts
Normal file
@ -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;
|
||||||
|
}
|
@ -42,3 +42,4 @@ export * from "./Token";
|
|||||||
export * from "./TraverseDirectory";
|
export * from "./TraverseDirectory";
|
||||||
export * from "./WebAuthn";
|
export * from "./WebAuthn";
|
||||||
export * from "./Gifs";
|
export * from "./Gifs";
|
||||||
|
export * from "./Application";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user