diff --git a/src/routes/guilds/#guild_id/roles.ts b/src/routes/guilds/#guild_id/roles.ts
index c663fea1..e9360847 100644
--- a/src/routes/guilds/#guild_id/roles.ts
+++ b/src/routes/guilds/#guild_id/roles.ts
@@ -1,27 +1,38 @@
import { Request, Response, Router } from "express";
-import { RoleModel, GuildModel, getPermission, toObject, UserModel, Snowflake, MemberModel } from "@fosscord/server-util";
+import {
+ RoleModel,
+ GuildModel,
+ getPermission,
+ toObject,
+ UserModel,
+ Snowflake,
+ MemberModel,
+ GuildRoleCreateEvent,
+ GuildRoleUpdateEvent,
+ GuildRoleDeleteEvent
+} from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { emitEvent } from "../../../util/Event";
import { check } from "../../../util/instanceOf";
-import { RoleCreateSchema, RoleModifySchema } from "../../../schema/Roles";
+import { RoleModifySchema } from "../../../schema/Roles";
import { getPublicUser } from "../../../util/User";
+import { isMember } from "../../../util/Member";
const router: Router = Router();
router.get("/", async (req: Request, res: Response) => {
const guild_id = req.params.guild_id;
- const guild = await GuildModel.exists({ id: guild_id });
- if (!guild) throw new HTTPError("Guild not found", 404);
+ await isMember(req.user_id, guild_id);
+
+ const roles = await RoleModel.find({ guild_id: guild_id }).exec();
- var roles = await RoleModel.find({ guild_id: guild_id }).exec();
- if (!roles) res.send("No roles");
return res.json(toObject(roles));
});
-router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) => {
+router.post("/", check(RoleModifySchema), async (req: Request, res: Response) => {
const guild_id = req.params.guild_id;
- const body = req.body as RoleCreateSchema;
+ const body = req.body as RoleModifySchema;
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
if (!guild) throw new HTTPError("Guild not found", 404);
@@ -30,22 +41,29 @@ router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) =>
if (!user) throw new HTTPError("User not found", 404);
const perms = await getPermission(req.user_id, guild_id);
+ perms.hasThrow("MANAGE_ROLES");
+ if (!body.name) throw new HTTPError("You need to specify a name");
- if (!perms.has("MANAGE_ROLES")) throw new HTTPError("You missing the MANAGE_ROLES permission", 401);
-
- const role_id = Snowflake.generate();
-
- var role = {
+ const role = await new RoleModel({
...body,
- id: role_id,
+ id: Snowflake.generate(),
guild_id: guild_id,
managed: false,
position: 0,
- tags: null
- };
+ tags: null,
+ permissions: body.permissions || 0n
+ }).save();
+
+ await emitEvent({
+ event: "GUILD_ROLE_CREATE",
+ guild_id,
+ data: {
+ guild_id,
+ role: toObject(role)
+ }
+ } as GuildRoleCreateEvent);
- const roleNew = await new RoleModel(role).save();
- res.json(toObject(roleNew)).send();
+ res.json(toObject(role));
});
router.delete("/:role_id", async (req: Request, res: Response) => {
@@ -68,7 +86,16 @@ router.delete("/:role_id", async (req: Request, res: Response) => {
guild_id: guild_id
}).exec();
- res.send("Deleted");
+ await emitEvent({
+ event: "GUILD_ROLE_DELETE",
+ guild_id,
+ data: {
+ guild_id,
+ role_id
+ }
+ } as GuildRoleDeleteEvent);
+
+ res.sendStatus(204);
});
// TODO: check role hierarchy
@@ -76,6 +103,7 @@ router.delete("/:role_id", async (req: Request, res: Response) => {
router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Response) => {
const guild_id = req.params.guild_id;
const { role_id } = req.params;
+ const body = req.body as RoleModifySchema;
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
if (!guild) throw new HTTPError("Guild not found", 404);
@@ -92,9 +120,19 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res
id: role_id,
guild_id: guild_id
},
- ...req.body
+ // @ts-ignore
+ body
).exec();
+ await emitEvent({
+ event: "GUILD_ROLE_UPDATE",
+ guild_id,
+ data: {
+ guild_id,
+ role
+ }
+ } as GuildRoleUpdateEvent);
+
res.json(toObject(role));
});
|