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 all 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));
}

/**
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));
}

/**
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));
}

/**
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
38 changes: 37 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,33 @@ class Channel extends Base {
return `<#${this.id}>`;
}

static from(data, client) {
switch(data.type) {
case 0: {
return new TextChannel(data, client);
}
case 1: {
return new PrivateChannel(data, client);
}
case 2: {
return new VoiceChannel(data, client);
}
case 3: {
return new GroupChannel(data, client);
}
case 4: {
return new CategoryChannel(data, client);
}
case 5: {
return new NewsChannel(data, client);
}
case 6: {
return new StoreChannel(data, client);
}
}
return new Channel(data);
}

toJSON() {
const base = super.toJSON(true);
for(const prop of ["type"]) {
Expand All @@ -31,3 +58,12 @@ 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 StoreChannel = require("./StoreChannel");
const TextChannel = require("./TextChannel");
const VoiceChannel = require("./VoiceChannel");
19 changes: 3 additions & 16 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 @@ -69,18 +66,8 @@ 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);
}
for(const channel of data.channels) {
this.channels.add(Channel.from(channel, client));
client.channelGuildMap[channel.id] = this.id;
}
}
Expand Down
14 changes: 5 additions & 9 deletions lib/structures/GuildChannel.js
Expand Up @@ -7,7 +7,7 @@ const {Permissions} = require("../Constants");
const PermissionOverwrite = require("./PermissionOverwrite");

/**
* Represents a guild channel. You also probably want to look at CategoryChannel, TextChannel, NewsChannel, and VoiceChannel.
* Represents a guild channel. You also probably want to look at CategoryChannel, NewsChannel, StoreChannel, TextChannel, and VoiceChannel.
* @extends Channel
* @prop {String} id The ID of the channel
* @prop {String} mention A string that mentions the channel
Expand All @@ -20,15 +20,11 @@ const PermissionOverwrite = require("./PermissionOverwrite");
* @prop {Collection<PermissionOverwrite>} permissionOverwrites Collection of PermissionOverwrites in this channel
*/
class GuildChannel extends Channel {
constructor(data, guild) {
constructor(data, client) {
super(data);
if(!guild && data.guild_id) {
this.guild = {
id: data.guild_id
};
} else {
this.guild = guild;
}
this.guild = client.guilds.get(data.guild_id) || {
id: data.guild_id
};

this.update(data);
}
Expand Down
4 changes: 0 additions & 4 deletions lib/structures/NewsChannel.js
Expand Up @@ -26,10 +26,6 @@ class NewsChannel extends TextChannel {
this.rateLimitPerUser = 0;
this.update(data);
}

update(data) {
super.update(data);
}
}

module.exports = NewsChannel;
21 changes: 21 additions & 0 deletions lib/structures/StoreChannel.js
@@ -0,0 +1,21 @@
"use strict";

const GuildChannel = require("./GuildChannel");

/**
* Represents a store channel
* @extends GuildChannel
* @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
* @prop {Guild} guild The guild that owns the channel
* @prop {String?} parentID The ID of the category this channel belongs to
* @prop {String} name The name of the channel
* @prop {Number} position The position of the channel
* @prop {Boolean} nsfw Whether the channel is an NSFW channel or not
* @prop {Collection<PermissionOverwrite>} permissionOverwrites Collection of PermissionOverwrites in this channel
*/
class StoreChannel extends GuildChannel {
}

module.exports = StoreChannel;
11 changes: 4 additions & 7 deletions lib/structures/TextChannel.js
Expand Up @@ -23,14 +23,11 @@ const Message = require("./Message");
* @prop {Number} rateLimitPerUser The ratelimit of the channel, in seconds. 0 means no ratelimit is enabled
*/
class TextChannel extends GuildChannel {
constructor(data, guild, messageLimit) {
super(data, guild);
if(messageLimit == null && guild) {
messageLimit = guild.shard.client.options.messageLimit;
}
this.messages = new Collection(Message, messageLimit);
constructor(data, client, messageLimit) {
super(data, client);
this.messages = new Collection(Message, messageLimit == null ? client.options.messageLimit : messageLimit);
this.lastMessageID = data.last_message_id || null;
this.rateLimitPerUser = data.rate_limit_per_user == undefined ? null : data.rate_limit_per_user;
this.rateLimitPerUser = data.rate_limit_per_user == null ? null : data.rate_limit_per_user;
this.lastPinTimestamp = data.last_pin_timestamp ? Date.parse(data.last_pin_timestamp) : null;
this.update(data);
}
Expand Down