mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-11 13:14:06 +01:00
Merge branch 'master' of https://github.com/discord-open-source/discord-api
This commit is contained in:
commit
f309ecb325
12302
package-lock.json
generated
12302
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -31,7 +31,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/fosscord/fosscord-api#readme",
|
||||
"dependencies": {
|
||||
"@fosscord/server-util": "^1.1.1",
|
||||
"@fosscord/server-util": "^1.1.2",
|
||||
"@types/jest": "^26.0.22",
|
||||
"bcrypt": "^5.0.0",
|
||||
"body-parser": "^1.19.0",
|
||||
|
@ -1,8 +1,8 @@
|
||||
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 } from "../../../util/Member";
|
||||
import { PublicMemberProjection, addMember, removeMember, addRole, removeRole, changeNickname } from "../../../util/Member";
|
||||
import { emitEvent } from "../../../util/Event";
|
||||
import { getPublicUser } from "../../../util/User";
|
||||
|
||||
@ -37,45 +37,70 @@ 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;
|
||||
await removeMember(member_id, guild_id);
|
||||
res.sendStatus(204)
|
||||
});
|
||||
|
||||
const member = await MemberModel.findOne({ id: user_id, guild_id }).exec();
|
||||
if (!member) throw new HTTPError("Member not found", 404);
|
||||
router.delete("/:member_id/roles/:role_id", async (req: Request, res: Response) => {
|
||||
const { guild_id, role_id, member_id } = req.params;
|
||||
|
||||
await removeMember(user_id, guild_id);
|
||||
const perms = await getPermission(member_id, guild_id);
|
||||
perms.hasThrow("MANAGE_ROLES");
|
||||
|
||||
// https://discord.com/developers/docs/resources/guild#remove-guild-member
|
||||
await removeRole(member_id, guild_id, role_id);
|
||||
res.sendStatus(204);
|
||||
});
|
||||
|
||||
router.put("/:member_id/roles/:role_id", async (req: Request, res: Response) => {
|
||||
const { guild_id, role_id, member_id } = req.params;
|
||||
|
||||
const perms = await getPermission(member_id, guild_id);
|
||||
perms.hasThrow("MANAGE_ROLES");
|
||||
|
||||
await addRole(member_id, guild_id, role_id);
|
||||
res.sendStatus(204);
|
||||
});
|
||||
|
||||
router.patch("/:member_id/nick", async (req: Request, res: Response) => {
|
||||
const { guild_id, member_id } = req.params;
|
||||
if(!req.body.nickname) throw new HTTPError("No nickname defined", 404);
|
||||
|
||||
const perms = await getPermission(member_id, guild_id);
|
||||
perms.hasThrow("MANAGE_NICKNAMES");
|
||||
|
||||
await changeNickname(member_id, guild_id, req.body.nickname);
|
||||
res.status(204);
|
||||
});
|
||||
|
||||
router.patch("/members/@me/nick", async (req: Request, res: Response) => {
|
||||
const { guild_id, member_id } = req.params;
|
||||
if(!req.body.nickname) throw new HTTPError("No nickname defined", 404);
|
||||
|
||||
const perms = await getPermission(member_id, guild_id);
|
||||
perms.hasThrow("CHANGE_NICKNAME");
|
||||
|
||||
await changeNickname(member_id, guild_id, req.body.nickname);
|
||||
res.status(204);
|
||||
});
|
||||
|
||||
|
||||
|
@ -4,10 +4,13 @@ import {
|
||||
GuildDeleteEvent,
|
||||
GuildMemberAddEvent,
|
||||
GuildMemberRemoveEvent,
|
||||
GuildMemberUpdateEvent,
|
||||
GuildModel,
|
||||
MemberModel,
|
||||
RoleModel,
|
||||
UserModel,
|
||||
} from "@fosscord/server-util";
|
||||
import { response } from "express";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import Config from "./Config";
|
||||
import { emitEvent } from "./Event";
|
||||
@ -120,3 +123,80 @@ export async function removeMember(user_id: string, guild_id: string) {
|
||||
} as GuildMemberRemoveEvent),
|
||||
]);
|
||||
}
|
||||
|
||||
export async function addRole(user_id: string, guild_id: string, role_id: string) {
|
||||
const user = await getPublicUser(user_id);
|
||||
|
||||
const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec();
|
||||
if (!role) throw new HTTPError("role not found", 404);
|
||||
|
||||
var memberObj = await MemberModel.findOneAndUpdate({
|
||||
id: user_id,
|
||||
guild_id: guild_id,
|
||||
}, { $push: { roles: role_id } }).exec();
|
||||
|
||||
if(!memberObj) throw new HTTPError("Member not found", 404);
|
||||
|
||||
await emitEvent({
|
||||
event: "GUILD_MEMBER_UPDATE",
|
||||
data: {
|
||||
guild_id: guild_id,
|
||||
user: user,
|
||||
roles: memberObj.roles
|
||||
|
||||
},
|
||||
guild_id: guild_id,
|
||||
} as GuildMemberUpdateEvent);
|
||||
|
||||
}
|
||||
|
||||
export async function removeRole(user_id: string, guild_id: string, role_id: string) {
|
||||
const user = await getPublicUser(user_id);
|
||||
|
||||
const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec();
|
||||
if (!role) throw new HTTPError("role not found", 404);
|
||||
|
||||
var memberObj = await MemberModel.findOneAndUpdate({
|
||||
id: user_id,
|
||||
guild_id: guild_id,
|
||||
}, { $pull: { roles: role_id } }).exec();
|
||||
|
||||
if(!memberObj) throw new HTTPError("Member not found", 404);
|
||||
|
||||
await emitEvent({
|
||||
event: "GUILD_MEMBER_UPDATE",
|
||||
data: {
|
||||
guild_id: guild_id,
|
||||
user: user,
|
||||
roles: memberObj.roles
|
||||
|
||||
},
|
||||
guild_id: guild_id,
|
||||
} as GuildMemberUpdateEvent);
|
||||
|
||||
}
|
||||
|
||||
export async function changeNickname(user_id: string, guild_id: string, nickname: string) {
|
||||
const user = await getPublicUser(user_id);
|
||||
|
||||
var memberObj = await MemberModel.findOneAndUpdate({
|
||||
id: user_id,
|
||||
guild_id: guild_id,
|
||||
}, { nick: nickname } ).exec();
|
||||
|
||||
if(!memberObj) throw new HTTPError("Member not found", 404);
|
||||
|
||||
await emitEvent({
|
||||
event: "GUILD_MEMBER_UPDATE",
|
||||
data: {
|
||||
guild_id: guild_id,
|
||||
user: user,
|
||||
nick: nickname
|
||||
|
||||
},
|
||||
guild_id: guild_id,
|
||||
} as GuildMemberUpdateEvent);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user