mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 21:16:09 +00:00
feat: Permissions
This commit is contained in:
parent
330a83c83a
commit
6de668d223
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;
|
@ -3,13 +3,14 @@ import type { Session } from "../session/Session.ts";
|
||||
import type { DiscordRole } from "../vendor/external.ts";
|
||||
import { Snowflake } from "../util/Snowflake.ts";
|
||||
import { iconHashToBigInt } from "../util/hash.ts";
|
||||
import { Permissions } from "./Permissions.ts";
|
||||
import { Guild } from "./Guild.ts";
|
||||
|
||||
export class Role implements Model {
|
||||
constructor(session: Session, guild: Guild, data: DiscordRole) {
|
||||
constructor(session: Session, guildId: Snowflake, data: DiscordRole) {
|
||||
this.session = session;
|
||||
this.id = data.id;
|
||||
this.guild = guild;
|
||||
this.guildId = guildId;
|
||||
this.hoist = data.hoist;
|
||||
this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined;
|
||||
this.color = data.color;
|
||||
@ -17,11 +18,12 @@ export class Role implements Model {
|
||||
this.unicodeEmoji = data.unicode_emoji;
|
||||
this.mentionable = data.mentionable;
|
||||
this.managed = data.managed;
|
||||
this.permissions = new Permissions(BigInt(data.permissions));
|
||||
}
|
||||
|
||||
session: Session;
|
||||
id: Snowflake;
|
||||
guild: Guild;
|
||||
guildId: Snowflake;
|
||||
hoist: boolean;
|
||||
iconHash?: bigint;
|
||||
color: number;
|
||||
@ -29,6 +31,7 @@ export class Role implements Model {
|
||||
unicodeEmoji?: string;
|
||||
mentionable: boolean;
|
||||
managed: boolean;
|
||||
permissions: Permissions;
|
||||
|
||||
get createdTimestamp() {
|
||||
return Snowflake.snowflakeToTimestamp(this.id);
|
||||
@ -42,13 +45,14 @@ export class Role implements Model {
|
||||
return `#${this.color.toString(16).padStart(6, '0')}`;
|
||||
}
|
||||
|
||||
async delete() {
|
||||
await this.guild.deleteRole(this.id);
|
||||
async delete(): Promise<void> {
|
||||
// cool jS trick
|
||||
await Guild.prototype.deleteRole.call({ id: this.guildId }, this.id);
|
||||
}
|
||||
|
||||
toString() {
|
||||
switch (this.id) {
|
||||
case this.guild.id:
|
||||
case this.guildId:
|
||||
return "@everyone";
|
||||
default:
|
||||
return `<@&${this.id}>`;
|
||||
|
@ -81,3 +81,7 @@ export function GUILD_ROLE(guildId: Snowflake, roleId: Snowflake) {
|
||||
export function GUILD_ROLES(guildId: Snowflake) {
|
||||
return `/guilds/${guildId}/roles`;
|
||||
}
|
||||
|
||||
export function USER_DM() {
|
||||
return `/users/@me/channels`;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user