1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-23 02:42:28 +01:00

guild position update

This commit is contained in:
Flam3rboy 2021-09-03 05:03:11 +02:00
parent ba4e363279
commit e2ea701920
3 changed files with 49 additions and 8 deletions

View File

@ -33,10 +33,10 @@ router.patch("/", check(MemberChangeSchema), async (req: Request, res: Response)
const permission = await getPermission(req.user_id, guild_id);
if (body.roles) {
permission.hasThrow("MANAGE_ROLES");
const roles = await Role.find({ id: In(body.roles) });
if (body.roles.length !== roles.length) throw new HTTPError("Roles not found", 404);
permission.hasThrow("MANAGE_ROLES");
}
const member = await Member.findOneOrFail({ id: member_id, guild_id });

View File

@ -13,8 +13,9 @@ import {
import { HTTPError } from "lambert-server";
import { check } from "../../../util/instanceOf";
import { RoleModifySchema } from "../../../schema/Roles";
import { RoleModifySchema, RolePositionUpdateSchema } from "../../../schema/Roles";
import { DiscordApiErrors } from "../../../util/Constants";
import { In } from "typeorm";
const router: Router = Router();
@ -40,20 +41,21 @@ router.post("/", check(RoleModifySchema), async (req: Request, res: Response) =>
if (role_count > maxRoles) throw DiscordApiErrors.MAXIMUM_ROLES.withParams(maxRoles);
const role = {
const role = new Role({
// values before ...body are default and can be overriden
position: 0,
hoist: false,
color: 0, // default value
color: 0,
mentionable: false,
...body,
id: Snowflake.generate(),
guild_id: guild_id,
managed: false,
permissions: String(perms.bitfield & (body.permissions || 0n)),
tags: undefined
};
});
await Promise.all([
new Role(role).save(),
role.save(),
emitEvent({
event: "GUILD_ROLE_CREATE",
guild_id,
@ -119,4 +121,31 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res
res.json(role);
});
router.patch("/", check(RolePositionUpdateSchema), async (req: Request, res: Response) => {
const { guild_id } = req.params;
const body = req.body as RolePositionUpdateSchema;
const perms = await getPermission(req.user_id, guild_id);
perms.hasThrow("MANAGE_ROLES");
await Promise.all(body.map(async (x) => Role.update({ guild_id, id: x.id }, { position: x.position })));
const roles = await Role.find({ guild_id, id: In(body.map((x) => x.id)) });
await Promise.all(
roles.map((x) =>
emitEvent({
event: "GUILD_ROLE_UPDATE",
guild_id,
data: {
guild_id,
role: x
}
} as GuildRoleUpdateEvent)
)
);
res.json(roles);
});
export default router;

View File

@ -15,3 +15,15 @@ export interface RoleModifySchema {
mentionable?: boolean; // whether the role should be mentionable
position?: number;
}
export const RolePositionUpdateSchema = [
{
id: String,
position: Number
}
];
export type RolePositionUpdateSchema = {
id: string;
position: number;
}[];