add .toObject() method to structures (#522)

* add .toObject() method to structures

* add compiled PMChannel change from last commit

* uncomment members in VoiceChannel toObject method
This commit is contained in:
Brian Tanner
2016-08-16 23:53:07 +07:00
committed by abal
parent e42178181d
commit fdfd41dd8c
21 changed files with 426 additions and 58 deletions

View File

@@ -37,4 +37,15 @@ export default class Emoji {
get getURL() {
return Endpoints.EMOJI(this.id);
}
toObject() {
let keys = ['id', 'name', 'roleList', 'colons', 'managed'],
obj = {};
for (let k of keys) {
obj[k] = this[k];
}
return obj;
}
}

View File

@@ -58,6 +58,22 @@ export default class Message extends Equality{
return this.author;
}
toObject() {
let keys = ['id', 'timestamp', 'everyoneMentioned', 'pinned', 'editedTimestamp', 'content', 'cleanContent', 'tts', 'attachments', 'embeds'],
obj = {};
for (let k of keys) {
obj[k] = this[k];
}
obj.channelID = this.channel ? this.channel.id : null;
obj.serverID = this.server ? this.server.id : null;
obj.author = this.author.toObject();
obj.mentions = this.mentions.map(m => m.toObject());
return obj;
}
isMentioned(user){
user = this.client.internal.resolver.resolveUser(user);
if (!user) {

View File

@@ -24,6 +24,17 @@ export default class PMChannel extends Channel {
return this.recipient.toString();
}
toObject() {
let keys = ['type', 'lastMessageID', 'recipient'],
obj = {};
for (let k of keys) {
obj[k] = this[k];
}
return obj;
}
sendMessage(){
return this.client.sendMessage.apply(this.client, reg(this, arguments));
}

View File

@@ -43,6 +43,17 @@ export default class PermissionOverwrite {
return denied;
}
toObject() {
let keys = ['id', 'type', 'allow', 'deny'],
obj = {};
for (let k of keys) {
obj[k] = this[k];
}
return obj;
}
setAllowed(allowedArray){
allowedArray.forEach( (permission) => {
if(permission instanceof String || typeof permission === "string"){

View File

@@ -47,6 +47,17 @@ export default class Role {
return new Date((+this.id / 4194304) + 1420070400000);
}
toObject() {
let keys = ['id', 'position', 'permissions', 'name', 'managed', 'hoist', 'color', 'mentionable'],
obj = {};
for (let k of keys) {
obj[k] = this[k];
}
return obj;
}
serialise(explicit){
var hp = (perm) => this.hasPermission(perm, explicit);

View File

@@ -132,6 +132,22 @@ export default class Server extends Equality {
return new Date((+this.id / 4194304) + 1420070400000);
}
toObject() {
var keys = ['id', 'name', 'region', 'ownerID', 'icon', 'afkTimeout', 'afkChannelID', 'large', 'memberCount'],
obj = {};
for (let k of keys) {
obj[k] = this[k];
}
obj.members = this.members.map(member => member.toObject());
obj.channels = this.channels.map(channel => channel.toObject());
obj.roles = this.roles.map(role => role.toObject());
obj.emojis = this.emojis.map(emoji => emoji.toObject());
return obj;
}
detailsOf(user) {
user = this.client.internal.resolver.resolveUser(user);
if (user) {

View File

@@ -21,6 +21,19 @@ export default class ServerChannel extends Channel{
}
}
toObject() {
let keys = ['id', 'name', 'type', 'position'],
obj = {};
for (let k of keys) {
obj[k] = this[k];
}
obj.permissionOverwrites = this.permissionOverwrites.map(p => p.toObject());
return obj;
}
permissionsOf(userOrRole){
userOrRole = this.client.internal.resolver.resolveUser(userOrRole);
if (userOrRole) {

View File

@@ -18,6 +18,15 @@ export default class TextChannel extends ServerChannel{
return this.messages.get("id", this.lastMessageID);
}
toObject() {
let obj = super.toObject();
obj.topic = this.topic;
obj.lastMessageID = this.lastMessageID;
return obj;
}
setTopic(){
return this.client.setChannelTopic.apply(this.client, reg(this, arguments));
}

View File

@@ -49,6 +49,23 @@ export default class User extends Equality {
return this.mention();
}
toObject() {
let keys = ['id', 'username', 'discriminator', 'avatar', 'bot', 'status', 'game', 'note', 'voiceState', 'speaking'],
obj = {};
for (let k of keys) {
obj[k] = this[k];
}
obj.typing = {
since: this.typing.since,
channelID: this.typing.channel ? this.typing.channel.id : null
};
obj.voiceChannelID = this.voiceChannel ? this.voiceChannel.id : null;
return obj;
}
equalsStrict(obj){
if(obj instanceof User)
return (

View File

@@ -13,6 +13,16 @@ export default class VoiceChannel extends ServerChannel{
this.bitrate = Math.round(this._bitrate / 1000); // store as kbps
}
toObject() {
let obj = super.toObject();
obj.userLimit = this.userLimit;
obj.bitrate = this.bitrate;
obj.members = this.members.map(member => member.toObject());
return obj;
}
join(callback = function () { }) {
return this.client.joinVoiceChannel.apply(this.client, [this, callback]);
}