1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 21:23:24 +01:00
server/dist/util/Permissions.js

84 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-02-05 22:01:01 +01:00
"use strict";
// https://github.com/discordjs/discord.js/blob/master/src/util/Permissions.js
// Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah
Object.defineProperty(exports, "__esModule", { value: true });
exports.Permissions = void 0;
const BitField_1 = require("./BitField");
class Permissions extends BitField_1.BitField {
any(permission, checkAdmin = true) {
return (checkAdmin && super.has(Permissions.FLAGS.ADMINISTRATOR)) || super.any(permission);
}
/**
* Checks whether the bitfield has a permission, or multiple permissions.
*/
has(permission, checkAdmin = true) {
return (checkAdmin && super.has(Permissions.FLAGS.ADMINISTRATOR)) || super.has(permission);
}
static channelPermission(overwrites, init) {
// channelOverwrites.filter((x) => x.type === 1 && x.id !== user.id);
return overwrites.reduce((permission, overwrite) => {
// apply disallowed permission
// * permission: current calculated permission (e.g. 010)
// * deny contains all denied permissions (e.g. 011)
// * allow contains all explicitly allowed permisions (e.g. 100)
return (permission & ~overwrite.deny) | overwrite.allow;
// ~ operator inverts deny (e.g. 011 -> 100)
// & operator only allows 1 for both ~deny and permission (e.g. 010 & 100 -> 000)
// | operators adds both together (e.g. 000 + 100 -> 100)
}, 0n ?? init);
}
static rolePermission(roles) {
// adds all permissions of all roles together (Bit OR)
return roles.reduce((permission, role) => permission | role.permissions, 0n);
}
static finalPermission({ user, guild, channel, }) {
let roles = guild.roles.filter((x) => user.roles.includes(x.id));
let permission = Permissions.rolePermission(roles);
if (channel?.overwrites) {
let overwrites = channel.overwrites.filter((x) => {
if (x.type === 0 && user.roles.includes(x.id))
return true;
if (x.type === 1 && x.id == user.id)
return true;
return false;
});
permission = Permissions.channelPermission(overwrites, permission);
}
return permission;
}
2021-02-05 22:01:01 +01:00
}
exports.Permissions = Permissions;
Permissions.FLAGS = {
CREATE_INSTANT_INVITE: 1n << 0n,
KICK_MEMBERS: 1n << 1n,
BAN_MEMBERS: 1n << 2n,
ADMINISTRATOR: 1n << 3n,
MANAGE_CHANNELS: 1n << 4n,
MANAGE_GUILD: 1n << 5n,
ADD_REACTIONS: 1n << 6n,
VIEW_AUDIT_LOG: 1n << 7n,
PRIORITY_SPEAKER: 1n << 8n,
STREAM: 1n << 9n,
VIEW_CHANNEL: 1n << 10n,
SEND_MESSAGES: 1n << 11n,
SEND_TTS_MESSAGES: 1n << 12n,
MANAGE_MESSAGES: 1n << 13n,
EMBED_LINKS: 1n << 14n,
ATTACH_FILES: 1n << 15n,
READ_MESSAGE_HISTORY: 1n << 16n,
MENTION_EVERYONE: 1n << 17n,
USE_EXTERNAL_EMOJIS: 1n << 18n,
VIEW_GUILD_INSIGHTS: 1n << 19n,
CONNECT: 1n << 20n,
SPEAK: 1n << 21n,
MUTE_MEMBERS: 1n << 22n,
DEAFEN_MEMBERS: 1n << 23n,
MOVE_MEMBERS: 1n << 24n,
USE_VAD: 1n << 25n,
CHANGE_NICKNAME: 1n << 26n,
MANAGE_NICKNAMES: 1n << 27n,
MANAGE_ROLES: 1n << 28n,
MANAGE_WEBHOOKS: 1n << 29n,
MANAGE_EMOJIS: 1n << 30n,
};
//# sourceMappingURL=Permissions.js.map