✨ use DiscordApiErrors and check limits

This commit is contained in:
Flam3rboy
2021-09-01 11:27:52 +02:00
parent 2e7ec1ec63
commit 7ff5e14887
8 changed files with 104 additions and 117 deletions
+2 -1
View File
@@ -1,6 +1,7 @@
import { Channel, ChannelPinsUpdateEvent, Config, emitEvent, getPermission, Message, MessageUpdateEvent } from "@fosscord/util";
import { Router, Request, Response } from "express";
import { HTTPError } from "lambert-server";
import { DiscordApiErrors } from "../../../util/Constants";
const router: Router = Router();
@@ -16,7 +17,7 @@ router.put("/:message_id", async (req: Request, res: Response) => {
const pinned_count = await Message.count({ channel: { id: channel_id }, pinned: true });
const { maxPins } = Config.get().limits.channel;
if (pinned_count >= maxPins) throw new HTTPError("Max pin count reached: " + maxPins);
if (pinned_count >= maxPins) throw DiscordApiErrors.MAXIMUM_PINS.withParams(maxPins);
await Promise.all([
Message.update({ id: message_id }, { pinned: true }),
@@ -1,11 +1,12 @@
import { Router, Response, Request } from "express";
import { check, Length } from "../../../util/instanceOf";
import { Channel, getPermission, trimSpecial } from "@fosscord/util";
import { Channel, Config, getPermission, trimSpecial, Webhook } from "@fosscord/util";
import { HTTPError } from "lambert-server";
import { isTextChannel } from "./messages/index";
import { DiscordApiErrors } from "../../../util/Constants";
const router: Router = Router();
// TODO:
// TODO: webhooks
// TODO: use Image Data Type for avatar instead of String
router.post("/", check({ name: new Length(String, 1, 80), $avatar: String }), async (req: Request, res: Response) => {
@@ -15,6 +16,10 @@ router.post("/", check({ name: new Length(String, 1, 80), $avatar: String }), as
isTextChannel(channel.type);
if (!channel.guild_id) throw new HTTPError("Not a guild channel", 400);
const webhook_count = await Webhook.count({ channel_id });
const { maxWebhooks } = Config.get().limits.channel;
if (webhook_count > maxWebhooks) throw DiscordApiErrors.MAXIMUM_WEBHOOKS.withParams(maxWebhooks);
const permission = await getPermission(req.user_id, channel.guild_id);
permission.hasThrow("MANAGE_WEBHOOKS");