create util for app bot user creation

This commit is contained in:
Puyodead1 2023-05-07 00:00:42 -04:00
parent 942cce913d
commit bb22e42da9
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
4 changed files with 30 additions and 35 deletions

View File

@ -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),

View File

@ -22,6 +22,7 @@ import {
ApplicationCreateSchema, ApplicationCreateSchema,
Config, Config,
User, User,
createAppBotUser,
trimSpecial, trimSpecial,
} from "@spacebar/util"; } from "@spacebar/util";
import { Request, Response, Router } from "express"; 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 // april 14, 2023: discord made bot users be automatically added to all new apps
const { autoCreateBotUsers } = Config.get().general; const { autoCreateBotUsers } = Config.get().general;
if (autoCreateBotUsers) { if (autoCreateBotUsers) {
const user = await User.register({ await createAppBotUser(app, req);
username: app.name, } else await app.save();
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); res.json(app);
}, },

View 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;
}

View File

@ -18,6 +18,7 @@
export * from "./ApiError"; export * from "./ApiError";
export * from "./Array"; export * from "./Array";
export * from "./Application";
export * from "./BitField"; export * from "./BitField";
//export * from "./Categories"; //export * from "./Categories";
export * from "./cdn"; export * from "./cdn";