2020-11-15 07:32:46 +00:00
|
|
|
// Ported from https://github.com/discordjs/discord.js/blob/master/src/util/Permissions.js
|
2020-12-02 12:29:52 +00:00
|
|
|
import { PermissionFlags } from '../types/permissionFlags.ts'
|
2021-01-25 11:28:46 +00:00
|
|
|
import { BitField, BitFieldResolvable } from './bitfield.ts'
|
2020-11-15 07:32:46 +00:00
|
|
|
|
2021-02-21 13:14:14 +00:00
|
|
|
export type PermissionResolvable = BitFieldResolvable
|
2020-11-15 07:32:46 +00:00
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Manages Discord's Bit-based Permissions */
|
2020-11-15 07:32:46 +00:00
|
|
|
export class Permissions extends BitField {
|
2021-02-21 13:14:14 +00:00
|
|
|
static DEFAULT = 104324673n
|
|
|
|
static ALL = Object.values(PermissionFlags).reduce(
|
|
|
|
(all, p) => BigInt(all) | BigInt(p),
|
|
|
|
0n
|
|
|
|
)
|
2020-11-15 07:32:46 +00:00
|
|
|
|
2021-01-25 11:28:46 +00:00
|
|
|
constructor(bits: BitFieldResolvable) {
|
2020-11-15 07:32:46 +00:00
|
|
|
super(PermissionFlags, bits)
|
|
|
|
}
|
|
|
|
|
|
|
|
any(permission: PermissionResolvable, checkAdmin = true): boolean {
|
2020-12-02 12:29:52 +00:00
|
|
|
return (
|
2021-02-21 13:14:14 +00:00
|
|
|
(checkAdmin && super.has(this.flags().ADMINISTRATOR)) ||
|
2020-12-02 12:29:52 +00:00
|
|
|
super.any(permission as any)
|
|
|
|
)
|
2020-11-15 07:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
has(permission: PermissionResolvable, checkAdmin = true): boolean {
|
2020-12-02 12:29:52 +00:00
|
|
|
return (
|
2021-02-21 13:14:14 +00:00
|
|
|
(checkAdmin && super.has(this.flags().ADMINISTRATOR)) ||
|
2020-12-02 12:29:52 +00:00
|
|
|
super.has(permission as any)
|
|
|
|
)
|
2020-11-15 07:32:46 +00:00
|
|
|
}
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|