diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-09-01 11:27:52 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-09-01 11:27:52 +0200 |
commit | 398e8c29cab05e3d06a73bc9fae3a4ad428c1095 (patch) | |
tree | f199eb5184545cb8b0c44ca711691ad2bfe4d3da /api/src/routes/guilds/#guild_id | |
parent | fix #295 (diff) | |
download | server-398e8c29cab05e3d06a73bc9fae3a4ad428c1095.tar.xz |
:sparkles: use DiscordApiErrors and check limits
Diffstat (limited to 'api/src/routes/guilds/#guild_id')
-rw-r--r-- | api/src/routes/guilds/#guild_id/roles.ts | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts index f6ac8caa..c3dd92dc 100644 --- a/api/src/routes/guilds/#guild_id/roles.ts +++ b/api/src/routes/guilds/#guild_id/roles.ts @@ -7,12 +7,14 @@ import { GuildRoleCreateEvent, GuildRoleUpdateEvent, GuildRoleDeleteEvent, - emitEvent + emitEvent, + Config } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { check } from "../../../util/instanceOf"; import { RoleModifySchema } from "../../../schema/Roles"; +import { DiscordApiErrors } from "../../../util/Constants"; const router: Router = Router(); @@ -33,24 +35,34 @@ router.post("/", check(RoleModifySchema), async (req: Request, res: Response) => const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); - const role = await new Role({ + const role_count = await Role.count({ guild_id }); + const { maxRoles } = Config.get().limits.guild; + + if (role_count > maxRoles) throw DiscordApiErrors.MAXIMUM_ROLES.withParams(maxRoles); + + const role = { + position: 0, + hoist: false, + color: 0, // default value ...body, id: Snowflake.generate(), guild_id: guild_id, managed: false, - position: 0, - tags: null, - permissions: String(perms.bitfield & (body.permissions || 0n)) - }).save(); - - await emitEvent({ - event: "GUILD_ROLE_CREATE", - guild_id, - data: { + permissions: String(perms.bitfield & (body.permissions || 0n)), + tags: undefined + }; + + await Promise.all([ + Role.insert(role), + emitEvent({ + event: "GUILD_ROLE_CREATE", guild_id, - role: role - } - } as GuildRoleCreateEvent); + data: { + guild_id, + role: role + } + } as GuildRoleCreateEvent) + ]); res.json(role); }); @@ -84,14 +96,13 @@ router.delete("/:role_id", async (req: Request, res: Response) => { // TODO: check role hierarchy router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - const { role_id } = req.params; + const { role_id, guild_id } = req.params; const body = req.body as RoleModifySchema; const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); - const role = new Role({ ...body, id: role_id, guild_id, permissions: perms.bitfield & (body.permissions || 0n) }); + const role = new Role({ ...body, id: role_id, guild_id, permissions: String(perms.bitfield & (body.permissions || 0n)) }); await Promise.all([ role.save(), |