mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-03 05:26:07 +00:00
Merge branch 'yuzudev:main' into main
This commit is contained in:
commit
93fc7cfdcf
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 type { DiscordRole } from "../vendor/external.ts";
|
||||||
import { Snowflake } from "../util/Snowflake.ts";
|
import { Snowflake } from "../util/Snowflake.ts";
|
||||||
import { iconHashToBigInt } from "../util/hash.ts";
|
import { iconHashToBigInt } from "../util/hash.ts";
|
||||||
|
import { Permissions } from "./Permissions.ts";
|
||||||
import { Guild } from "./Guild.ts";
|
import { Guild } from "./Guild.ts";
|
||||||
|
|
||||||
export class Role implements Model {
|
export class Role implements Model {
|
||||||
constructor(session: Session, guild: Guild, data: DiscordRole) {
|
constructor(session: Session, guildId: Snowflake, data: DiscordRole) {
|
||||||
this.session = session;
|
this.session = session;
|
||||||
this.id = data.id;
|
this.id = data.id;
|
||||||
this.guild = guild;
|
this.guildId = guildId;
|
||||||
this.hoist = data.hoist;
|
this.hoist = data.hoist;
|
||||||
this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined;
|
this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined;
|
||||||
this.color = data.color;
|
this.color = data.color;
|
||||||
@ -17,11 +18,12 @@ 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;
|
||||||
id: Snowflake;
|
id: Snowflake;
|
||||||
guild: Guild;
|
guildId: Snowflake;
|
||||||
hoist: boolean;
|
hoist: boolean;
|
||||||
iconHash?: bigint;
|
iconHash?: bigint;
|
||||||
color: number;
|
color: number;
|
||||||
@ -29,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);
|
||||||
@ -42,13 +45,14 @@ 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.guild.deleteRole(this.id);
|
// cool jS trick
|
||||||
|
await Guild.prototype.deleteRole.call({ id: this.guildId }, this.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
toString() {
|
toString() {
|
||||||
switch (this.id) {
|
switch (this.id) {
|
||||||
case this.guild.id:
|
case this.guildId:
|
||||||
return "@everyone";
|
return "@everyone";
|
||||||
default:
|
default:
|
||||||
return `<@&${this.id}>`;
|
return `<@&${this.id}>`;
|
||||||
|
@ -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