throw missing permission

This commit is contained in:
Flam3rboy 2021-04-23 00:10:10 +02:00
parent 29bdb72979
commit 6f5fba5c38
3 changed files with 15 additions and 10 deletions

4
package-lock.json generated
View File

@ -1,11 +1,11 @@
{ {
"name": "@fosscord/fosscord-server-util", "name": "@fosscord/server-util",
"version": "1.0.2", "version": "1.0.2",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@fosscord/fosscord-server-util", "name": "@fosscord/server-util",
"version": "1.0.2", "version": "1.0.2",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@fosscord/server-util", "name": "@fosscord/server-util",
"version": "1.0.2", "version": "1.0.3",
"description": "Utility functions for the all server repositories", "description": "Utility functions for the all server repositories",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
@ -16,12 +16,10 @@
"discord", "discord",
"fosscord", "fosscord",
"fosscord-server-util", "fosscord-server-util",
"foss cord",
"discord open source", "discord open source",
"discord source",
"discord-open-source" "discord-open-source"
], ],
"author": "", "author": "Fosscord",
"license": "ISC", "license": "ISC",
"bugs": { "bugs": {
"url": "https://github.com/fosscord/fosscord-server-util/issues" "url": "https://github.com/fosscord/fosscord-server-util/issues"

View File

@ -7,6 +7,14 @@ import { Role, RoleModel } from "../models/Role";
import { BitField } from "./BitField"; import { BitField } from "./BitField";
import { GuildDocument, GuildModel } from "../models/Guild"; 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; export type PermissionResolvable = bigint | number | Permissions | PermissionResolvable[] | PermissionString;
type 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. * 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) { hasThrow(permission: PermissionResolvable, checkAdmin = true) {
if ((checkAdmin && super.has(Permissions.FLAGS.ADMINISTRATOR)) || super.has(permission)) { if (this.has(permission)) return true;
return true; // @ts-ignore
} throw new HTTPError(`You are missing the following permissions ${permission}`, 403);
throw new Error(`User doesn't fulfill the following permission criteria: ${permission}`);
} }
static channelPermission(overwrites: ChannelPermissionOverwrite[], init?: bigint) { static channelPermission(overwrites: ChannelPermissionOverwrite[], init?: bigint) {