Skip to content

Commit

Permalink
Guard against unknown text-like channel types
Browse files Browse the repository at this point in the history
  • Loading branch information
abalabahaha committed Mar 8, 2019
1 parent e0a2fc0 commit df40987
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
36 changes: 22 additions & 14 deletions lib/Client.js
Expand Up @@ -388,12 +388,14 @@ class Client extends EventEmitter {
rate_limit_per_user: options.rateLimitPerUser,
parent_id: options.parentID
}).then((channel) => {
if(channel.type === 0) {
return new TextChannel(channel, guild);
} else if(channel.type === 2) {
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 TextChannel(channel, guild);
} else if(channel.type === 0 || channel.last_message_id !== undefined) {
return new TextChannel(channel, guild);
} else {
return channel;
}
Expand Down Expand Up @@ -429,17 +431,19 @@ class Client extends EventEmitter {
parent_id: options.parentID,
reason: reason
}).then((data) => {
if(!data.type || data.type === 2 || data.type === 4) {
let guild = this.channelGuildMap[channelID];
if(data.guild_id) {
let guild = data.guild_id || this.channelGuildMap[channelID];
if(guild) {
guild = this.guilds.get(guild);
}
if(data.type === 0) {
return new TextChannel(data, guild);
} else if(data.type === 2) {
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 TextChannel(data, guild);
} else if(data.type === 0 || (data.guild_id && data.last_message_id !== undefined)) {
return new TextChannel(data, guild);
} else {
return data;
}
Expand Down Expand Up @@ -1816,16 +1820,18 @@ class Client extends EventEmitter {
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 === 0) {
return new TextChannel(channel, null, this.options.messageLimit);
} else if(channel.type === 1) {
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 TextChannel(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;
}
Expand Down Expand Up @@ -1872,12 +1878,14 @@ class Client extends EventEmitter {
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 === 0) {
return new TextChannel(channel, null, this.options.messageLimit);
} else if(channel.type === 2) {
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 TextChannel(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;
}
Expand Down
14 changes: 8 additions & 6 deletions lib/gateway/Shard.js
Expand Up @@ -866,19 +866,21 @@ class Shard extends EventEmitter {
this.client.privateChannelMap[packet.d.recipients[0].id] = packet.d.id;
this.emit("channelCreate", this.client.privateChannels.add(packet.d, this.client));
}
} else if(packet.d.type === 0 || packet.d.type === 2 || packet.d.type === 4) {
} else if(packet.d.guild_id) {
const guild = this.client.guilds.get(packet.d.guild_id);
if(!guild) {
this.emit("debug", `Missing guild ${packet.d.guild_id} in CHANNEL_CREATE`);
break;
}
let channel;
if(packet.d.type === 0) {
channel = guild.channels.add(new TextChannel(packet.d, guild), guild);
} else if(packet.d.type === 2) {
if(packet.d.type === 2) {
channel = guild.channels.add(new VoiceChannel(packet.d, guild), guild);
} else if(packet.d.type === 4) {
channel = guild.channels.add(new CategoryChannel(packet.d, guild), guild);
} else if(packet.d.type === 5) {
channel = guild.channels.add(new TextChannel(packet.d, guild), guild);
} else if(packet.d.type === 0 || (packet.d.guild_id && packet.d.last_message_id !== undefined)) {
channel = guild.channels.add(new TextChannel(packet.d, guild), guild);
} else {
channel = guild.channels.add(packet.d, guild);
}
Expand Down Expand Up @@ -917,7 +919,7 @@ class Shard extends EventEmitter {
icon: channel.icon
};
}
if(channel.type === 0 || channel.type === 2 || channel.type === 4) {
if(channel instanceof GuildChannel) {
oldChannel = {
name: channel.name,
topic: channel.topic,
Expand Down Expand Up @@ -976,7 +978,7 @@ class Shard extends EventEmitter {
this.emit("channelDelete", channel);
}
}
} else if(packet.d.type === 0 || packet.d.type === 2 || packet.d.type === 4) {
} else if(packet.d.guild_id) {
delete this.client.channelGuildMap[packet.d.id];
const guild = this.client.guilds.get(packet.d.guild_id);
if(!guild) {
Expand Down
8 changes: 5 additions & 3 deletions lib/structures/Guild.js
Expand Up @@ -59,12 +59,14 @@ class Guild extends Base {

if(data.channels) {
for(let channel of data.channels) {
if(channel.type === 0) {
channel = this.channels.add(new TextChannel(channel, this), this);
} else if(channel.type === 2) {
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 TextChannel(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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/TextChannel.js
Expand Up @@ -29,7 +29,7 @@ class TextChannel extends GuildChannel {
}
this.messages = new Collection(Message, messageLimit);
this.lastMessageID = data.last_message_id || null;
this.rateLimitPerUser = data.rate_limit_per_user;
this.rateLimitPerUser = data.rate_limit_per_user == undefined ? null : data.rate_limit_per_user;
this.lastPinTimestamp = data.last_pin_timestamp ? Date.parse(data.last_pin_timestamp) : null;
this.update(data);
}
Expand Down

0 comments on commit df40987

Please sign in to comment.