mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-11 13:14:06 +01:00
Update members.ts
This commit is contained in:
parent
73b1674439
commit
614a0bf057
@ -1,5 +1,5 @@
|
||||
import { Request, Response, Router } from "express";
|
||||
import { GuildModel, MemberModel, UserModel, toObject, GuildMemberAddEvent } from "@fosscord/server-util";
|
||||
import { GuildModel, MemberModel, UserModel, toObject, GuildMemberAddEvent, getPermission } from "@fosscord/server-util";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { instanceOf, Length } from "../../../util/instanceOf";
|
||||
import { PublicMemberProjection, addMember, removeMember, addRole, removeRole } from "../../../util/Member";
|
||||
@ -37,83 +37,58 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
return res.json(toObject(members));
|
||||
});
|
||||
|
||||
router.get("/:member", async (req: Request, res: Response) => {
|
||||
const { guild_id } = req.params;
|
||||
const user_id = req.params.member;
|
||||
router.get("/:member_id", async (req: Request, res: Response) => {
|
||||
const { guild_id, member_id } = req.params;
|
||||
|
||||
const member = await MemberModel.findOne({ id: user_id, guild_id }).exec();
|
||||
const member = await MemberModel.findOne({ id: member_id, guild_id }).exec();
|
||||
if (!member) throw new HTTPError("Member not found", 404);
|
||||
|
||||
return res.json(member);
|
||||
return res.json(toObject(member));
|
||||
});
|
||||
|
||||
router.put("/:member", async (req: Request, res: Response) => {
|
||||
const { guild_id } = req.params;
|
||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
||||
router.put("/:member_id", async (req: Request, res: Response) => {
|
||||
const { guild_id, member_id } = req.params;
|
||||
|
||||
const user_id = req.params.member;
|
||||
|
||||
const user = await UserModel.findOne({ id: user_id }).exec();
|
||||
if (!user) throw new HTTPError("User not found", 404);
|
||||
|
||||
await addMember(user_id, guild_id);
|
||||
|
||||
// https://discord.com/developers/docs/resources/guild#add-guild-member
|
||||
await addMember(member_id, guild_id);
|
||||
res.sendStatus(204)
|
||||
});
|
||||
|
||||
|
||||
router.delete("/:member", async (req: Request, res: Response) => {
|
||||
const { guild_id } = req.params;
|
||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
||||
router.delete("/:member_id", async (req: Request, res: Response) => {
|
||||
const { guild_id, member_id } = req.params;
|
||||
|
||||
const user_id = req.params.member;
|
||||
|
||||
const member = await MemberModel.findOne({ id: user_id, guild_id }).exec();
|
||||
if (!member) throw new HTTPError("Member not found", 404);
|
||||
|
||||
await removeMember(user_id, guild_id);
|
||||
|
||||
// https://discord.com/developers/docs/resources/guild#remove-guild-member
|
||||
await removeMember(member_id, guild_id);
|
||||
res.sendStatus(204)
|
||||
});
|
||||
|
||||
router.delete("/:member/roles/:role_id", async (req: Request, res: Response) => {
|
||||
router.delete("/:member_id/roles/:role_id", async (req: Request, res: Response) => {
|
||||
const { guild_id, role_id, member_id } = req.params;
|
||||
|
||||
const { guild_id, role_id } = req.params;
|
||||
if(!role_id) throw new HTTPError("role_id not defined", 404);
|
||||
const perms = await getPermission(member_id, guild_id);
|
||||
perms.hasThrow("MANAGE_ROLES");
|
||||
|
||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
||||
await removeRole(member_id, guild_id, role_id);
|
||||
res.sendStatus(204);
|
||||
});
|
||||
|
||||
const user_id = req.params.member;
|
||||
router.put("/:member_id/roles/:role_id", async (req: Request, res: Response) => {
|
||||
const { guild_id, role_id, member_id } = req.params;
|
||||
|
||||
const member = await MemberModel.findOne({ id: user_id, guild_id }).exec();
|
||||
if (!member) throw new HTTPError("Member not found", 404);
|
||||
const perms = await getPermission(member_id, guild_id);
|
||||
perms.hasThrow("MANAGE_ROLES");
|
||||
|
||||
await removeRole(user_id, guild_id, role_id);
|
||||
await addRole(member_id, guild_id, role_id);
|
||||
res.sendStatus(204);
|
||||
});
|
||||
|
||||
router.patch("/:member_id/@me/nick", async (req: Request, res: Response) => {
|
||||
const { guild_id, member_id } = req.params;
|
||||
|
||||
const perms = await getPermission(member_id, guild_id);
|
||||
perms.hasThrow("CHANGE_NICKNAME");
|
||||
|
||||
//await addRole(member_id, guild_id, role_id);
|
||||
res.status(204);
|
||||
|
||||
// https://discord.com/developers/docs/resources/guild#remove-guild-member-role
|
||||
});
|
||||
|
||||
router.put("/:member/roles/:role_id", async (req: Request, res: Response) => {
|
||||
|
||||
const { guild_id, role_id } = req.params;
|
||||
if(!role_id) throw new HTTPError("role_id not defined", 404);
|
||||
|
||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
||||
|
||||
const user_id = req.params.member;
|
||||
|
||||
const member = await MemberModel.findOne({ id: user_id, guild_id }).exec();
|
||||
if (!member) throw new HTTPError("Member not found", 404);
|
||||
|
||||
await addRole(user_id, guild_id, role_id);
|
||||
res.status(204);
|
||||
|
||||
// https://discord.com/developers/docs/resources/guild#remove-guild-member-role
|
||||
});
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user