mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +01:00
Created Role class and permission evaluation within Roles
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const User = require('./User');
|
const User = require('./User');
|
||||||
const Member = require('./Member');
|
const GuildMember = require('./GuildMember');
|
||||||
const GuildDataStore = require('./datastore/GuildDataStore');
|
const GuildDataStore = require('./datastore/GuildDataStore');
|
||||||
const TextChannel = require('./TextChannel');
|
const TextChannel = require('./TextChannel');
|
||||||
const VoiceChannel = require('./VoiceChannel');
|
const VoiceChannel = require('./VoiceChannel');
|
||||||
const Constants = require('../Util/Constants');
|
const Constants = require('../Util/Constants');
|
||||||
|
const Role = require('./Role');
|
||||||
|
|
||||||
class Guild {
|
class Guild {
|
||||||
constructor(client, data) {
|
constructor(client, data) {
|
||||||
@@ -27,7 +28,7 @@ class Guild {
|
|||||||
|
|
||||||
_addMember(guildUser) {
|
_addMember(guildUser) {
|
||||||
guildUser.user = this.client.store.NewUser(guildUser.user);
|
guildUser.user = this.client.store.NewUser(guildUser.user);
|
||||||
let member = this.store.add('members', new Member(this, guildUser));
|
let member = this.store.add('members', new GuildMember(this, guildUser));
|
||||||
if (this.client.ws.emittedReady) {
|
if (this.client.ws.emittedReady) {
|
||||||
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member);
|
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member);
|
||||||
}
|
}
|
||||||
@@ -81,6 +82,13 @@ class Guild {
|
|||||||
this.client.store.NewChannel(channel, this);
|
this.client.store.NewChannel(channel, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data.roles) {
|
||||||
|
this.store.clear('roles');
|
||||||
|
for (let role of data.roles) {
|
||||||
|
this.store.add('roles', new Role(this, role));
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
class Member {
|
class GuildMember {
|
||||||
constructor(guild, data) {
|
constructor(guild, data) {
|
||||||
this.client = guild.client;
|
this.client = guild.client;
|
||||||
this.guild = guild;
|
this.guild = guild;
|
||||||
@@ -39,4 +39,4 @@ class Member {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Member;
|
module.exports = GuildMember;
|
||||||
52
src/structures/Role.js
Normal file
52
src/structures/Role.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const Constants = require('../Util/Constants');
|
||||||
|
|
||||||
|
class Role {
|
||||||
|
constructor(guild, data) {
|
||||||
|
this.guild = guild;
|
||||||
|
this.client = guild.client;
|
||||||
|
if (data) {
|
||||||
|
this.setup(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setup(data) {
|
||||||
|
this.id = data.id;
|
||||||
|
this.name = data.name;
|
||||||
|
this.color = data.color;
|
||||||
|
this.hoist = data.hoist;
|
||||||
|
this.position = data.position;
|
||||||
|
this.permissions = data.permissions;
|
||||||
|
this.managed = data.managed;
|
||||||
|
}
|
||||||
|
|
||||||
|
serialize() {
|
||||||
|
let serializedPermissions = {};
|
||||||
|
for (let permissionName in Constants.PermissionFlags) {
|
||||||
|
serializedPermissions[permissionName] = this.hasPermission(permissionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return serializedPermissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
hasPermission(permission, explicit) {
|
||||||
|
if (permission instanceof String || typeof permission === 'string') {
|
||||||
|
permission = Constants.PermissionFlags[permission];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!permission) {
|
||||||
|
throw Constants.Errors.NOT_A_PERMISSION;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!explicit) {
|
||||||
|
if ((this.permissions & Constants.PermissionFlags.MANAGE_ROLES) > 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((this.permissions & permission) > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Role;
|
||||||
@@ -20,6 +20,7 @@ const Errors = exports.Errors = {
|
|||||||
NO_BOT_ACCOUNT: new Error('you should ideally be using a bot account!'),
|
NO_BOT_ACCOUNT: new Error('you should ideally be using a bot account!'),
|
||||||
BAD_WS_MESSAGE: new Error('a bad message was received from the websocket - bad compression or not json'),
|
BAD_WS_MESSAGE: new Error('a bad message was received from the websocket - bad compression or not json'),
|
||||||
TOOK_TOO_LONG: new Error('something took too long to do'),
|
TOOK_TOO_LONG: new Error('something took too long to do'),
|
||||||
|
NOT_A_PERMISSION: new Error('that is not a valid permission number'),
|
||||||
};
|
};
|
||||||
|
|
||||||
const API = 'https://discordapp.com/api';
|
const API = 'https://discordapp.com/api';
|
||||||
@@ -116,3 +117,30 @@ const WSEvents = exports.WSEvents = {
|
|||||||
FRIEND_ADD: 'RELATIONSHIP_ADD',
|
FRIEND_ADD: 'RELATIONSHIP_ADD',
|
||||||
FRIEND_REMOVE: 'RELATIONSHIP_REMOVE',
|
FRIEND_REMOVE: 'RELATIONSHIP_REMOVE',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const PermissionFlags = exports.PermissionFlags = {
|
||||||
|
CREATE_INSTANT_INVITE: 1 << 0,
|
||||||
|
KICK_MEMBERS: 1 << 1,
|
||||||
|
BAN_MEMBERS: 1 << 2,
|
||||||
|
MANAGE_ROLES: 1 << 3,
|
||||||
|
MANAGE_CHANNELS: 1 << 4,
|
||||||
|
MANAGE_GUILD: 1 << 5,
|
||||||
|
|
||||||
|
READ_MESSAGES: 1 << 10,
|
||||||
|
SEND_MESSAGES: 1 << 11,
|
||||||
|
SEND_TTS_MESSAGES: 1 << 12,
|
||||||
|
MANAGE_MESSAGES: 1 << 13,
|
||||||
|
EMBED_LINKS: 1 << 14,
|
||||||
|
ATTACH_FILES: 1 << 15,
|
||||||
|
READ_MESSAGE_HISTORY: 1 << 16,
|
||||||
|
MENTION_EVERYONE: 1 << 17,
|
||||||
|
|
||||||
|
CONNECT: 1 << 20,
|
||||||
|
SPEAK: 1 << 21,
|
||||||
|
MUTE_MEMBERS: 1 << 22,
|
||||||
|
DEAFEN_MEMBERS: 1 << 23,
|
||||||
|
MOVE_MEMBERS: 1 << 24,
|
||||||
|
USE_VAD: 1 << 25,
|
||||||
|
};
|
||||||
|
|
||||||
|
const DEFAULT_PERMISSIONS = exports.DEFAULT_PERMISSIONS = 36953089;
|
||||||
|
|||||||
Reference in New Issue
Block a user