From 6f5fba5c381aaca94f6e6518a9d1a9f1d7923032 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 23 Apr 2021 00:10:10 +0200 Subject: [PATCH] :sparkles: throw missing permission --- package-lock.json | 4 ++-- package.json | 6 ++---- src/util/Permissions.ts | 15 +++++++++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8065faab..30a8333b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "@fosscord/fosscord-server-util", + "name": "@fosscord/server-util", "version": "1.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "@fosscord/fosscord-server-util", + "name": "@fosscord/server-util", "version": "1.0.2", "license": "ISC", "dependencies": { diff --git a/package.json b/package.json index 0a03a20e..2815e2ed 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.0.2", + "version": "1.0.3", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -16,12 +16,10 @@ "discord", "fosscord", "fosscord-server-util", - "foss cord", "discord open source", - "discord source", "discord-open-source" ], - "author": "", + "author": "Fosscord", "license": "ISC", "bugs": { "url": "https://github.com/fosscord/fosscord-server-util/issues" diff --git a/src/util/Permissions.ts b/src/util/Permissions.ts index c7a41594..2f43f7e1 100644 --- a/src/util/Permissions.ts +++ b/src/util/Permissions.ts @@ -7,6 +7,14 @@ import { Role, RoleModel } from "../models/Role"; import { BitField } from "./BitField"; import { GuildDocument, GuildModel } from "../models/Guild"; +var HTTPError: typeof Error; + +try { + HTTPError = require("lambert-server").HTTPError; +} catch (e) { + HTTPError = Error; +} + export type PermissionResolvable = bigint | number | Permissions | PermissionResolvable[] | PermissionString; type PermissionString = @@ -101,10 +109,9 @@ export class Permissions extends BitField { * Checks whether the bitfield has a permission, or multiple permissions, but throws an Error if user fails to match auth criteria. */ hasThrow(permission: PermissionResolvable, checkAdmin = true) { - if ((checkAdmin && super.has(Permissions.FLAGS.ADMINISTRATOR)) || super.has(permission)) { - return true; - } - throw new Error(`User doesn't fulfill the following permission criteria: ${permission}`); + if (this.has(permission)) return true; + // @ts-ignore + throw new HTTPError(`You are missing the following permissions ${permission}`, 403); } static channelPermission(overwrites: ChannelPermissionOverwrite[], init?: bigint) {