Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel.from() #565

Merged
merged 19 commits into from Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 7 additions & 69 deletions lib/Client.js
@@ -1,6 +1,6 @@
"use strict";

const CategoryChannel = require("./structures/CategoryChannel");
const Channel = require("./structures/Channel");
const Collection = require("./util/Collection");
const Constants = require("./Constants");
const Endpoints = require("./rest/Endpoints");
Expand All @@ -17,11 +17,8 @@ const Relationship = require("./structures/Relationship");
const RequestHandler = require("./rest/RequestHandler");
const Role = require("./structures/Role");
const ShardManager = require("./gateway/ShardManager");
const TextChannel = require("./structures/TextChannel");
const NewsChannel = require("./structures/NewsChannel");
const UnavailableGuild = require("./structures/UnavailableGuild");
const User = require("./structures/User");
const VoiceChannel = require("./structures/VoiceChannel");
const VoiceConnectionManager = require("./voice/VoiceConnectionManager");

let EventEmitter;
Expand Down Expand Up @@ -391,7 +388,6 @@ class Client extends EventEmitter {
* @returns {Promise<CategoryChannel | TextChannel | VoiceChannel>}
*/
createChannel(guildID, name, type, reason, options = {}) {
const guild = this.guilds.get(guildID);
if(typeof options === "string") { // This used to be parentID, back-compat
this.emit("warn", "[DEPRECATED] createChannel() was called with a string `options` argument");
options = {
Expand All @@ -417,19 +413,7 @@ class Client extends EventEmitter {
rate_limit_per_user: options.rateLimitPerUser,
parent_id: options.parentID,
permission_overwrites: options.permissionOverwrites
}).then((channel) => {
if(channel.type === 2) {
return new VoiceChannel(channel, guild);
} else if(channel.type === 4) {
return new CategoryChannel(channel, guild);
} else if(channel.type === 5) {
return new NewsChannel(channel, guild);
} else if(channel.type === 0 || channel.last_message_id !== undefined) {
return new TextChannel(channel, guild);
} else {
return channel;
}
});
}).then((channel) => Channel.from(channel, this.guilds.get(channel.guild_id) || this));
}

/**
Expand Down Expand Up @@ -460,27 +444,7 @@ class Client extends EventEmitter {
rate_limit_per_user: options.rateLimitPerUser,
parent_id: options.parentID,
reason: reason
}).then((data) => {
if(data.guild_id) {
let guild = data.guild_id || this.channelGuildMap[channelID];
if(guild) {
guild = this.guilds.get(guild);
}
if(data.type === 2) {
return new VoiceChannel(data, guild);
} else if(data.type === 4) {
return new CategoryChannel(data, guild);
} else if(data.type === 5) {
return new NewsChannel(data, guild);
} else if(data.type === 0 || (data.guild_id && data.last_message_id !== undefined)) {
return new TextChannel(data, guild);
} else {
return data;
}
} else {
return new GroupChannel(data, this);
}
});
}).then((channel) => Channel.from(channel, this.guilds.get(channel.guild_id) || this));
}

/**
Expand Down Expand Up @@ -1855,23 +1819,8 @@ class Client extends EventEmitter {
if(!this.options.restMode) {
return Promise.reject(new Error("Eris REST mode is not enabled"));
}
return this.requestHandler.request("GET", Endpoints.CHANNEL(channelID), true).then((channel) => {
if(channel.type === 1) {
return new PrivateChannel(channel, this);
} else if(channel.type === 2) {
return new VoiceChannel(channel, null);
} else if(channel.type === 3) {
return new GroupChannel(channel, this);
} else if(channel.type === 4) {
return new CategoryChannel(channel, null);
} else if(channel.type === 5) {
return new NewsChannel(channel, null, this.options.messageLimit);
} else if(channel.type === 0 || (channel.guild_id && channel.last_message_id !== undefined)) {
return new TextChannel(channel, null, this.options.messageLimit);
} else {
return channel;
}
});
return this.requestHandler.request("GET", Endpoints.CHANNEL(channelID), true)
.then((channel) => Channel.from(channel, this.guilds.get(channel.guild_id) || this));
}

/**
Expand Down Expand Up @@ -1913,19 +1862,8 @@ class Client extends EventEmitter {
if(!this.options.restMode) {
return Promise.reject(new Error("Eris REST mode is not enabled"));
}
return this.requestHandler.request("GET", Endpoints.GUILD_CHANNELS(guildID), true).then((channels) => channels.map((channel) => {
if(channel.type === 2) {
return new VoiceChannel(channel, null);
} else if(channel.type === 4) {
return new CategoryChannel(channel, null);
} else if(channel.type === 5) {
return new NewsChannel(channel, null, this.options.messageLimit);
} else if(channel.type === 0 || channel.last_message_id !== undefined) {
return new TextChannel(channel, null, this.options.messageLimit);
} else {
return channel;
}
}));
return this.requestHandler.request("GET", Endpoints.GUILD_CHANNELS(guildID), true)
.then((channels) => channels.map((channel) => Channel.from(channel, this)));
}

/**
Expand Down
34 changes: 33 additions & 1 deletion lib/structures/Channel.js
Expand Up @@ -3,7 +3,7 @@
const Base = require("./Base");

/**
* Represents a channel. You also probably want to look at CategoryChannel, GroupChannel, PrivateChannel, TextChannel, NewsChannel, and VoiceChannel.
* Represents a channel. You also probably want to look at CategoryChannel, GroupChannel, NewsChannel, PrivateChannel, TextChannel, and VoiceChannel.
* @prop {String} id The ID of the channel
* @prop {String} mention A string that mentions the channel
* @prop {Number} type The type of the channel
Expand All @@ -19,6 +19,30 @@ class Channel extends Base {
return `<#${this.id}>`;
}

static from(data, extra) {
apacheli marked this conversation as resolved.
Show resolved Hide resolved
switch(data.type) {
case 0: {
return new TextChannel(data, extra);
}
case 1: {
return new PrivateChannel(data, extra);
}
case 2: {
return new VoiceChannel(data, extra);
}
case 3: {
return new GroupChannel(data, extra);
}
case 4: {
return new CategoryChannel(data, extra);
}
case 5: {
return new NewsChannel(data, extra);
}
}
return new Channel(data);
}

toJSON() {
const base = super.toJSON(true);
for(const prop of ["type"]) {
Expand All @@ -31,3 +55,11 @@ class Channel extends Base {
}

module.exports = Channel;

// Circular import
const CategoryChannel = require("./CategoryChannel");
const GroupChannel = require("./GroupChannel");
const NewsChannel = require("./NewsChannel");
const PrivateChannel = require("./PrivateChannel");
const TextChannel = require("./TextChannel");
const VoiceChannel = require("./VoiceChannel");
22 changes: 5 additions & 17 deletions lib/structures/Guild.js
@@ -1,16 +1,13 @@
"use strict";

const Base = require("./Base");
const CategoryChannel = require("./CategoryChannel");
const Channel = require("./Channel");
const {CDN_URL} = require("../rest/Endpoints");
const Constants = require("../Constants");
const Collection = require("../util/Collection");
const GuildChannel = require("./GuildChannel");
const Member = require("./Member");
const Role = require("./Role");
const TextChannel = require("./TextChannel");
const NewsChannel = require("./NewsChannel");
const VoiceChannel = require("./VoiceChannel");
const VoiceState = require("./VoiceState");

/**
Expand Down Expand Up @@ -70,19 +67,10 @@ class Guild extends Base {

if(data.channels) {
for(let channel of data.channels) {
if(channel.type === 2) {
channel = this.channels.add(new VoiceChannel(channel, this), this);
} else if(channel.type === 4) {
channel = this.channels.add(new CategoryChannel(channel, this), this);
} else if(channel.type === 5) {
channel = this.channels.add(new NewsChannel(channel, this), this);
} else if(channel.type === 0 || channel.last_message_id !== undefined) {
channel = this.channels.add(new TextChannel(channel, this), this);
} else {
channel = this.channels.add(channel, this);
}
if(channel.parentID && this.channels.get(channel.parentID)) {
this.channels.get(channel.parentID).channels.add(channel);
channel = this.channels.add(Channel.from(channel, this));
const parent = this.channels.get(channel.parentID);
if(parent !== undefined) {
parent.channels.add(channel);
}
client.channelGuildMap[channel.id] = this.id;
}
Expand Down