I think I got ChannelUpdate working??

This commit is contained in:
hydrabolt
2016-04-30 18:22:09 +01:00
parent 2341c83638
commit 90cf787759
9 changed files with 130 additions and 12 deletions

View File

@@ -11,6 +11,7 @@ class ActionsManager {
this.register('MessageUpdate');
this.register('ChannelCreate');
this.register('ChannelDelete');
this.register('ChannelUpdate');
}
register(name) {

View File

@@ -0,0 +1,39 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const CloneObject = require('../../util/CloneObject');
const Message = require('../../structures/Message');
class ChannelUpdateAction extends Action {
constructor(client) {
super(client);
}
handle(data) {
let client = this.client;
let channel = client.store.get('channels', data.id);
if (channel) {
let oldChannel = CloneObject(channel);
channel.setup(data);
if (!oldChannel.equals(data)) {
client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
}
return {
old: oldChannel,
updated: channel,
};
}
return {
old: null,
updated: null,
};
}
};
module.exports = ChannelUpdateAction;

View File

@@ -25,7 +25,13 @@ class MessageUpdateAction extends Action {
old: oldMessage,
updated: message,
};
client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message);
}
return {
old: message,
updated: message,
};
}
return {

View File

@@ -96,6 +96,21 @@ class RESTMethods{
.catch(reject);
});
}
UpdateChannel(channel, data) {
return new Promise((resolve, reject) => {
data.name = (data.name || channel.name).trim();
data.topic = data.topic || channel.topic;
data.position = data.position || channel.position;
data.bitrate = data.bitrate || channel.bitrate;
this.rest.makeRequest('patch', Constants.Endpoints.CHANNEL(channel.id), true, data)
.then(data => {
resolve(this.rest.client.actions.ChannelUpdate.handle(data).updated);
})
.catch(reject);
});
}
}
module.exports = RESTMethods;

View File

@@ -20,12 +20,7 @@ class ChannelUpdateHandler extends AbstractHandler {
let data = packet.d;
let client = this.packetManager.client;
let channel = client.store.get('channels', data.id);
if (channel) {
client.store.UpdateChannel(channel, data);
}
client.actions.ChannelUpdate.handle(data);
}
};

View File

@@ -20,10 +20,6 @@ class MessageUpdateHandler extends AbstractHandler {
let response = client.actions.MessageUpdate.handle(data);
if (response.old) {
client.emit(Constants.Events.MESSAGE_UPDATE, response.old, response.updated);
}
}
};

View File

@@ -5,6 +5,19 @@ const PermissionOverwrites = require('./PermissionOverwrites');
const EvaluatedPermissions = require('./EvaluatedPermissions');
const Constants = require('../util/Constants');
function arraysEqual(a, b) {
if (a === b) return true;
for (let itemInd in a) {
let ind = b.indexOf(item);
if (ind) {
b.splice(ind, 1);
}
}
return b.length === 0;
}
class ServerChannel extends Channel{
constructor(guild, data) {
super(guild.client, data, guild);
@@ -18,14 +31,40 @@ class ServerChannel extends Channel{
this.name = data.name;
this.lastMessageID = data.last_message_id;
this.ow = data.permission_overwrites;
this.permissionOverwrites = [];
if (data.permission_overwrites) {
this.permissionOverwrites = [];
for (let overwrite of data.permission_overwrites) {
this.permissionOverwrites.push(new PermissionOverwrites(this, overwrite));
}
}
}
equals(other) {
let base = (
this.type === other.type &&
this.topic === other.topic &&
this.position === other.position &&
this.name === other.name &&
this.id === other.id
);
if (base) {
if (other.permission_overwrites && other.permission_overwrites.length === this.permissionOverwrites.length) {
let thisIDSet = this.permissionOverwrites.map(overwrite => overwrite.id);
let otherIDSet = other.permission_overwrites.map(overwrite => overwrite.id);
if (arraysEqual(thisIDSet, otherIDSet)) {
base = true;
} else {
base = false;
}
} else {
base = false;
}
}
return base;
}
permissionsFor(member) {
member = this.client.resolver.ResolveGuildMember(this.guild, member);
if (member) {
@@ -82,6 +121,26 @@ class ServerChannel extends Channel{
return [];
}
edit(data) {
return this.client.rest.methods.UpdateChannel(this, data);
}
setName(name) {
return this.client.rest.methods.UpdateChannel(this, { name, });
}
setPosition(position) {
return this.rest.client.rest.methods.UpdateChannel(this, { position, });
}
setTopic(topic) {
return this.rest.client.rest.methods.UpdateChannel(this, { topic, });
}
setBitrate() {
return this.rest.client.rest.methods.UpdateChannel(this, { bitrate, });
}
toString() {
return this.name;
}

View File

@@ -100,7 +100,6 @@ class ClientDataStore extends AbstractDataStore{
UpdateChannel(currentChannel, newData) {
let oldChannel = CloneObject(currentChannel);
currentChannel.setup(newData);
this.client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, currentChannel);
}
}

View File

@@ -79,9 +79,17 @@ client.on('message', message => {
if (message.content === 'delchann') {
message.channel.delete();
}
if (message.content.startsWith('setname')) {
message.channel.setName(message.content.substr(8)).then(chanLoop).catch(console.log);
}
}
});
function chanLoop(channel) {
channel.setName(channel.name + 'a').then(chanLoop).catch(console.log);
}
client.on('messageDelete', message => {
console.log('Message deleted by', message.author.username);
});