mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 01:23:31 +01:00
Added ability to edit a role
This commit is contained in:
@@ -17,6 +17,7 @@ class ActionsManager {
|
||||
this.register('GuildMemberRemove');
|
||||
this.register('GuildRoleCreate');
|
||||
this.register('GuildRoleDelete');
|
||||
this.register('GuildRoleUpdate');
|
||||
this.register('UserUpdate');
|
||||
}
|
||||
|
||||
|
||||
44
src/client/actions/GuildRoleUpdate.js
Normal file
44
src/client/actions/GuildRoleUpdate.js
Normal file
@@ -0,0 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
const CloneObject = require('../../util/CloneObject');
|
||||
const Message = require('../../structures/Message');
|
||||
|
||||
class GuildRoleUpdateAction extends Action {
|
||||
|
||||
constructor(client) {
|
||||
super(client);
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
|
||||
let client = this.client;
|
||||
let guild = client.store.get('guilds', data.guild_id);
|
||||
|
||||
let roleData = data.role;
|
||||
|
||||
if (guild) {
|
||||
let oldRole;
|
||||
let existingRole = guild.store.get('roles', roleData.id);
|
||||
// exists and not the same
|
||||
if (existingRole && !existingRole.equals(roleData)) {
|
||||
oldRole = CloneObject(existingRole);
|
||||
existingRole.setup(data.role);
|
||||
client.emit(Constants.Events.GUILD_ROLE_UPDATE, guild, oldRole, existingRole);
|
||||
}
|
||||
|
||||
return {
|
||||
old: oldRole,
|
||||
updated: existingRole,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
old: null,
|
||||
updated: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = GuildRoleUpdateAction;
|
||||
@@ -281,6 +281,49 @@ class RESTMethods{
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
UpdateGuildRole(role, _data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
/*
|
||||
can contain:
|
||||
name, position, permissions, color, hoist
|
||||
*/
|
||||
|
||||
let data = {};
|
||||
|
||||
data.name = _data.name || role.name;
|
||||
data.position = _data.position || role.position;
|
||||
data.color = _data.color || role.color;
|
||||
|
||||
if (typeof _data.hoist !== 'undefined') {
|
||||
data.hoist = _data.hoist;
|
||||
} else {
|
||||
data.hoist = role.hoist;
|
||||
}
|
||||
|
||||
if (_data.permissions) {
|
||||
let perms = 0;
|
||||
for (let perm of _data.permissions) {
|
||||
if (perm instanceof String || typeof perm === 'string') {
|
||||
perm = Constants.PermissionFlags[perm];
|
||||
}
|
||||
perms |= perm;
|
||||
}
|
||||
data.permissions = perms;
|
||||
} else {
|
||||
data.permissions = role.permissions;
|
||||
}
|
||||
console.log(data);
|
||||
this.rest.makeRequest('patch', Constants.Endpoints.GUILD_ROLE(role.guild.id, role.id), true, data)
|
||||
.then(_role => {
|
||||
resolve(this.rest.client.actions.GuildRoleUpdate.handle({
|
||||
role: _role,
|
||||
guild_id: role.guild.id,
|
||||
}).updated);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RESTMethods;
|
||||
|
||||
@@ -18,17 +18,7 @@ class GuildRoleUpdateHandler extends AbstractHandler {
|
||||
let data = packet.d;
|
||||
let client = this.packetManager.client;
|
||||
|
||||
let guild = client.store.get('guilds', data.guild_id);
|
||||
|
||||
if (guild) {
|
||||
let existingRole = guild.store.get('roles', data.role.id);
|
||||
if (existingRole) {
|
||||
let oldRole = CloneObject(existingRole);
|
||||
existingRole.setup(data.role);
|
||||
client.emit(Constants.Events.GUILD_ROLE_UPDATE, guild, oldRole, existingRole);
|
||||
}
|
||||
}
|
||||
|
||||
let response = client.actions.GuildRoleUpdate.handle(data);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user