mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 01:53:30 +01:00
Added GuildUpdate handling, and setting guilds details
This commit is contained in:
@@ -5,6 +5,7 @@ const Structure = name => require(`../structures/${name}`);
|
||||
const User = Structure('User');
|
||||
const Message = Structure('Message');
|
||||
const Guild = Structure('Guild');
|
||||
const Channel = Structure('Channel');
|
||||
const ServerChannel = Structure('ServerChannel');
|
||||
const TextChannel = Structure('TextChannel');
|
||||
const VoiceChannel = Structure('VoiceChannel');
|
||||
@@ -59,8 +60,31 @@ class ClientDataResolver {
|
||||
if (data instanceof Buffer) {
|
||||
return 'data:image/jpg;base64,' + data.toString('base64');
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
ResolveChannel(channel) {
|
||||
if (channel instanceof Channel) {
|
||||
return channel;
|
||||
}
|
||||
|
||||
if ($string(channel)) {
|
||||
return this.client.store.get('channels', channel);
|
||||
}
|
||||
}
|
||||
|
||||
ResolveString(data) {
|
||||
if (data instanceof String) {
|
||||
return data;
|
||||
}
|
||||
|
||||
if (data instanceof Array) {
|
||||
return data.join('\n');
|
||||
}
|
||||
|
||||
return String(data);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ClientDataResolver;
|
||||
|
||||
@@ -13,6 +13,7 @@ class ActionsManager {
|
||||
this.register('ChannelDelete');
|
||||
this.register('ChannelUpdate');
|
||||
this.register('GuildDelete');
|
||||
this.register('GuildUpdate');
|
||||
this.register('UserUpdate');
|
||||
}
|
||||
|
||||
|
||||
42
src/client/actions/GuildUpdate.js
Normal file
42
src/client/actions/GuildUpdate.js
Normal file
@@ -0,0 +1,42 @@
|
||||
'use strict';
|
||||
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
const CloneObject = require('../../util/CloneObject');
|
||||
const Message = require('../../structures/Message');
|
||||
|
||||
class GuildUpdateAction extends Action {
|
||||
|
||||
constructor(client) {
|
||||
super(client);
|
||||
this.deleted = {};
|
||||
this.timeouts = [];
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
|
||||
let client = this.client;
|
||||
let guild = client.store.get('guilds', data.id);
|
||||
|
||||
if (guild) {
|
||||
let oldGuild = CloneObject(guild);
|
||||
guild.setup(data);
|
||||
|
||||
if (!oldGuild.equals(data)) {
|
||||
client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild);
|
||||
}
|
||||
|
||||
return {
|
||||
old: oldGuild,
|
||||
updated: guild,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
old: null,
|
||||
updated: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = GuildUpdateAction;
|
||||
@@ -152,6 +152,53 @@ class RESTMethods{
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
UpdateGuild(guild, _data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
/*
|
||||
can contain:
|
||||
name, region, verificationLevel, afkChannel, afkTimeout, icon, owner, splash
|
||||
*/
|
||||
|
||||
let data = {};
|
||||
|
||||
if (_data.name) {
|
||||
data.name = _data.name;
|
||||
}
|
||||
|
||||
if (_data.region) {
|
||||
data.region = _data.region;
|
||||
}
|
||||
|
||||
if (_data.verificationLevel) {
|
||||
data.verification_level = Number(_data.verificationLevel);
|
||||
}
|
||||
|
||||
if (_data.afkChannel) {
|
||||
data.afk_channel_id = this.rest.client.resolver.ResolveChannel(_data.afkChannel).id;
|
||||
}
|
||||
|
||||
if (_data.afkTimeout) {
|
||||
data.afk_timeout = Number(_data.afkTimeout);
|
||||
}
|
||||
|
||||
if (_data.icon) {
|
||||
data.icon = this.rest.client.resolver.ResolveBase64(_data.icon);
|
||||
}
|
||||
|
||||
if (_data.owner) {
|
||||
data.owner_id = this.rest.client.resolver.ResolveUser(_data.owner).id;
|
||||
}
|
||||
|
||||
if (_data.splash) {
|
||||
data.splash = this.rest.client.resolver.ResolveBase64(_data.splash);
|
||||
}
|
||||
|
||||
this.rest.makeRequest('patch', Constants.Endpoints.GUILD(guild.id), true, data)
|
||||
.then(data => resolve(this.rest.client.actions.GuildUpdate.handle(data).updated))
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RESTMethods;
|
||||
|
||||
@@ -19,11 +19,7 @@ class GuildUpdateHandler extends AbstractHandler {
|
||||
let data = packet.d;
|
||||
let client = this.packetManager.client;
|
||||
|
||||
let guild = client.store.get('guilds', data.id);
|
||||
|
||||
if (guild) {
|
||||
client.store.UpdateGuild(guild, data);
|
||||
}
|
||||
let response = client.actions.GuildUpdate.handle(data);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user