mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
Added ability to edit a role
This commit is contained in:
@@ -17,6 +17,7 @@ class ActionsManager {
|
|||||||
this.register('GuildMemberRemove');
|
this.register('GuildMemberRemove');
|
||||||
this.register('GuildRoleCreate');
|
this.register('GuildRoleCreate');
|
||||||
this.register('GuildRoleDelete');
|
this.register('GuildRoleDelete');
|
||||||
|
this.register('GuildRoleUpdate');
|
||||||
this.register('UserUpdate');
|
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);
|
.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;
|
module.exports = RESTMethods;
|
||||||
|
|||||||
@@ -18,17 +18,7 @@ class GuildRoleUpdateHandler extends AbstractHandler {
|
|||||||
let data = packet.d;
|
let data = packet.d;
|
||||||
let client = this.packetManager.client;
|
let client = this.packetManager.client;
|
||||||
|
|
||||||
let guild = client.store.get('guilds', data.guild_id);
|
let response = client.actions.GuildRoleUpdate.handle(data);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,6 +11,18 @@ class Role {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
equals(role) {
|
||||||
|
return (
|
||||||
|
this.id === role.id &&
|
||||||
|
this.name === role.name &&
|
||||||
|
this.color === role.color &&
|
||||||
|
this.hoist === role.hoist &&
|
||||||
|
this.position === role.position &&
|
||||||
|
this.permissions === role.permissions &&
|
||||||
|
this.managed === role.managed
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
setup(data) {
|
setup(data) {
|
||||||
this.id = data.id;
|
this.id = data.id;
|
||||||
this.name = data.name;
|
this.name = data.name;
|
||||||
@@ -25,6 +37,30 @@ class Role {
|
|||||||
return this.client.rest.methods.DeleteGuildRole(this);
|
return this.client.rest.methods.DeleteGuildRole(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
edit(data) {
|
||||||
|
return this.client.rest.methods.UpdateGuildRole(this, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
setName(name) {
|
||||||
|
return this.client.rest.methods.UpdateGuildRole(this, {name,});
|
||||||
|
}
|
||||||
|
|
||||||
|
setColor(color) {
|
||||||
|
return this.client.rest.methods.UpdateGuildRole(this, {color,});
|
||||||
|
}
|
||||||
|
|
||||||
|
setHoist(hoist) {
|
||||||
|
return this.client.rest.methods.UpdateGuildRole(this, {hoist,});
|
||||||
|
}
|
||||||
|
|
||||||
|
setPosition(position) {
|
||||||
|
return this.client.rest.methods.UpdateGuildRole(this, {position,});
|
||||||
|
}
|
||||||
|
|
||||||
|
setPermissions(permissions) {
|
||||||
|
return this.client.rest.methods.UpdateGuildRole(this, {permissions,});
|
||||||
|
}
|
||||||
|
|
||||||
serialize() {
|
serialize() {
|
||||||
let serializedPermissions = {};
|
let serializedPermissions = {};
|
||||||
for (let permissionName in Constants.PermissionFlags) {
|
for (let permissionName in Constants.PermissionFlags) {
|
||||||
|
|||||||
@@ -41,7 +41,12 @@ client.on('guildMemberRemove', (guild, user) => {
|
|||||||
|
|
||||||
client.on('guildRoleCreate', (guild, role) => {
|
client.on('guildRoleCreate', (guild, role) => {
|
||||||
console.log('new role', role.name, 'in', guild.name);
|
console.log('new role', role.name, 'in', guild.name);
|
||||||
role.delete().then(role => console.log("deleted " + role.name)).catch(console.log);
|
role.edit({
|
||||||
|
permissions: ['DEAFEN_MEMBERS'],
|
||||||
|
name: 'deafen'
|
||||||
|
}).then(role2 => {
|
||||||
|
console.log('role replace from ' + role.name + ' to ' + role2.name);
|
||||||
|
}).catch(console.log)
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('guildRoleDelete', (guild, role) => {
|
client.on('guildRoleDelete', (guild, role) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user