mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 21:16:09 +00:00
Merge branch 'main' of github.com:socram03/biscuit into main
This commit is contained in:
commit
1f0a25bbcd
42
structures/Permissions.ts
Normal file
42
structures/Permissions.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { BitwisePermissionFlags } from "../vendor/external.ts";
|
||||||
|
|
||||||
|
export type PermissionString = keyof typeof BitwisePermissionFlags;
|
||||||
|
export type PermissionResolvable=
|
||||||
|
| bigint
|
||||||
|
| PermissionString
|
||||||
|
| PermissionString[]
|
||||||
|
| BitwisePermissionFlags;
|
||||||
|
|
||||||
|
export class Permissions {
|
||||||
|
static Flags = BitwisePermissionFlags;
|
||||||
|
bitfield: bigint;
|
||||||
|
|
||||||
|
constructor(bitfield: PermissionResolvable) {
|
||||||
|
this.bitfield = Permissions.resolve(bitfield);
|
||||||
|
}
|
||||||
|
|
||||||
|
has(bit: PermissionResolvable) {
|
||||||
|
if (this.bitfield & BigInt(Permissions.Flags.ADMINISTRATOR)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !!(this.bitfield & Permissions.resolve(bit));
|
||||||
|
}
|
||||||
|
|
||||||
|
static resolve(bit: PermissionResolvable): bigint {
|
||||||
|
switch (typeof bit) {
|
||||||
|
case "bigint":
|
||||||
|
return bit;
|
||||||
|
case "number":
|
||||||
|
return BigInt(bit);
|
||||||
|
case "string":
|
||||||
|
return BigInt(Permissions.Flags[bit]);
|
||||||
|
case "object":
|
||||||
|
return Permissions.resolve(bit.map((p) => BigInt(Permissions.Flags[p])).reduce((acc, cur) => acc | cur, 0n));
|
||||||
|
default:
|
||||||
|
throw new TypeError(`Cannot resolve permission: ${bit}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Permissions;
|
@ -1,8 +1,10 @@
|
|||||||
import type { Model } from "./Base.ts";
|
import type { Model } from "./Base.ts";
|
||||||
import type { Session } from "../session/Session.ts";
|
import type { Session } from "../session/Session.ts";
|
||||||
import type { DiscordRole } from "../vendor/external.ts";
|
import type { DiscordRole } from "../vendor/external.ts";
|
||||||
import { Snowflake, Routes } from "../mod.ts";
|
import { Snowflake } from "../mod.ts";
|
||||||
import { iconHashToBigInt } from "../util/hash.ts";
|
import { iconHashToBigInt } from "../util/hash.ts";
|
||||||
|
import { Permissions } from "./Permissions.ts";
|
||||||
|
import { Guild } from "./Guild.ts";
|
||||||
|
|
||||||
export class Role implements Model {
|
export class Role implements Model {
|
||||||
constructor(session: Session, guildId: Snowflake, data: DiscordRole) {
|
constructor(session: Session, guildId: Snowflake, data: DiscordRole) {
|
||||||
@ -16,6 +18,7 @@ export class Role implements Model {
|
|||||||
this.unicodeEmoji = data.unicode_emoji;
|
this.unicodeEmoji = data.unicode_emoji;
|
||||||
this.mentionable = data.mentionable;
|
this.mentionable = data.mentionable;
|
||||||
this.managed = data.managed;
|
this.managed = data.managed;
|
||||||
|
this.permissions = new Permissions(BigInt(data.permissions));
|
||||||
}
|
}
|
||||||
|
|
||||||
session: Session;
|
session: Session;
|
||||||
@ -28,6 +31,7 @@ export class Role implements Model {
|
|||||||
unicodeEmoji?: string;
|
unicodeEmoji?: string;
|
||||||
mentionable: boolean;
|
mentionable: boolean;
|
||||||
managed: boolean;
|
managed: boolean;
|
||||||
|
permissions: Permissions;
|
||||||
|
|
||||||
get createdTimestamp() {
|
get createdTimestamp() {
|
||||||
return Snowflake.snowflakeToTimestamp(this.id);
|
return Snowflake.snowflakeToTimestamp(this.id);
|
||||||
@ -41,8 +45,9 @@ export class Role implements Model {
|
|||||||
return `#${this.color.toString(16).padStart(6, "0")}`;
|
return `#${this.color.toString(16).padStart(6, "0")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete() {
|
async delete(): Promise<void> {
|
||||||
await this.session.rest.runMethod<undefined>(this.session.rest, "DELETE", Routes.GUILD_ROLE(this.guildId, this.id));
|
// cool jS trick
|
||||||
|
await Guild.prototype.deleteRole.call({ id: this.guildId }, this.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
toString() {
|
toString() {
|
||||||
|
@ -105,3 +105,7 @@ export function GUILD_ROLE(guildId: Snowflake, roleId: Snowflake) {
|
|||||||
export function GUILD_ROLES(guildId: Snowflake) {
|
export function GUILD_ROLES(guildId: Snowflake) {
|
||||||
return `/guilds/${guildId}/roles`;
|
return `/guilds/${guildId}/roles`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function USER_DM() {
|
||||||
|
return `/users/@me/channels`;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user