Implemented guild icons and banners

This commit is contained in:
BuildTools 2021-08-07 19:51:11 +03:00
parent c24e921b28
commit cbb62a8bb5

View File

@ -17,6 +17,7 @@ import { HTTPError } from "lambert-server";
import { GuildUpdateSchema } from "../../../schema/Guild"; import { GuildUpdateSchema } from "../../../schema/Guild";
import { emitEvent } from "../../../util/Event"; import { emitEvent } from "../../../util/Event";
import { check } from "../../../util/instanceOf"; import { check } from "../../../util/instanceOf";
import { uploadFile } from "../../../util/cdn";
import "missing-native-js-functions"; import "missing-native-js-functions";
const router = Router(); const router = Router();
@ -42,6 +43,32 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response)
const perms = await getPermission(req.user_id, guild_id); const perms = await getPermission(req.user_id, guild_id);
perms.hasThrow("MANAGE_GUILD"); perms.hasThrow("MANAGE_GUILD");
if (body.icon && body.icon.startsWith('data')) {
try {
const mimetype = body.icon.split(":")[1].split(";")[0];
const buffer = Buffer.from(body.icon.split(",")[1], "base64");
// @ts-ignore
const { id } = await uploadFile(`/icons/${guild_id}`, { buffer, mimetype, originalname: "icon" });
body.icon = id;
} catch (error) {
throw new HTTPError("Invalid icon");
}
}
if (body.banner && body.banner.startsWith('data')) {
try {
const mimetype = body.banner.split(":")[1].split(";")[0];
const buffer = Buffer.from(body.banner.split(",")[1], "base64");
// @ts-ignore
const { id } = await uploadFile(`/banners/${guild_id}`, { buffer, mimetype, originalname: "banner" });
body.banner = id;
} catch (error) {
throw new HTTPError("Invalid banner");
}
}
const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body) const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body)
.populate({ path: "joined_at", match: { id: req.user_id } }) .populate({ path: "joined_at", match: { id: req.user_id } })
.exec(); .exec();
@ -50,7 +77,7 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response)
emitEvent({ event: "GUILD_UPDATE", data: data, guild_id } as GuildUpdateEvent); emitEvent({ event: "GUILD_UPDATE", data: data, guild_id } as GuildUpdateEvent);
return res.send(data); return res.json(data);
}); });
export default router; export default router;