Skip to content

Commit

Permalink
Use Channel.from() in channel event handlers
Browse files Browse the repository at this point in the history
Channel.from() now attempts to guess the appropriate class for an unknown channel type
  • Loading branch information
abalabahaha committed Nov 24, 2019
1 parent 16c6df5 commit b0c7a07
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 75 deletions.
123 changes: 48 additions & 75 deletions lib/gateway/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

const Bucket = require("../util/Bucket");
const Call = require("../structures/Call");
const CategoryChannel = require("../structures/CategoryChannel");
const Channel = require("../structures/Channel");
const GroupChannel = require("../structures/GroupChannel");
const GuildChannel = require("../structures/GuildChannel");
const PrivateChannel = require("../structures/PrivateChannel");
const Constants = require("../Constants");
const ExtendedUser = require("../structures/ExtendedUser");
const GuildChannel = require("../structures/GuildChannel");
const OPCodes = Constants.GatewayOPCodes;
const TextChannel = require("../structures/TextChannel");
const NewsChannel = require("../structures/NewsChannel");
const User = require("../structures/User");
const VoiceChannel = require("../structures/VoiceChannel");
let WebSocket = typeof window !== "undefined" ? window.WebSocket : require("ws");

let EventEmitter;
Expand Down Expand Up @@ -861,48 +860,31 @@ class Shard extends EventEmitter {
break;
}
case "CHANNEL_CREATE": {
if(packet.d.type === undefined || packet.d.type === 1) {
if(this.id === 0) {
/**
* Fired when a channel is created
* @event Client#channelCreate
* @prop {PrivateChannel | TextChannel | VoiceChannel | CategoryChannel} channel The channel
*/
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.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 === 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 NewsChannel(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);
}
this.client.channelGuildMap[packet.d.id] = packet.d.guild_id;
if(packet.d.parent_id) {
if(!guild.channels.has(packet.d.parent_id)) {
this.emit("debug", `Missing category channel ${packet.d.parent_id} in CHANNEL_CREATE`);
const channel = Channel.from(packet.d, this.client);
if(packet.d.guild_id) {
if(!channel.guild) {
channel.guild = this.client.guilds.get(packet.d.guild_id);
if(!channel.guild) {
this.emit("debug", `Received CHANNEL_CREATE for channel in missing guild ${packet.d.guild_id}`);
break;
}
}
channel.guild.channels.add(channel, this.client);
this.client.channelGuildMap[packet.d.id] = packet.d.guild_id;
this.emit("channelCreate", channel);
} else if(packet.d.type === 3) {
} else if(channel instanceof PrivateChannel) {
if(channel instanceof GroupChannel) {
this.client.groupChannels.add(channel, this.client);
} else {
this.client.privateChannels.add(channel, this.client);
this.client.privateChannelMap[packet.d.recipients[0].id] = packet.d.id;
}
if(this.id === 0) {
this.emit("channelCreate", this.client.groupChannels.add(packet.d, this.client));
this.emit("channelCreate", channel);
}
} else {
this.emit("warn", new Error("Unhandled CHANNEL_CREATE type: " + JSON.stringify(packet, null, 2)));
break;
}
break;
}
Expand All @@ -911,22 +893,14 @@ class Shard extends EventEmitter {
if(!channel) {
break;
}
let guild;
if(packet.d.guild_id) {
guild = this.client.guilds.get(packet.d.guild_id);
}
let oldChannel;
if(channel.type === 3) {
if(this.id !== 0) {
break;
}
if(channel instanceof GroupChannel) {
oldChannel = {
name: channel.name,
ownerID: channel.ownerID,
icon: channel.icon
};
}
if(channel instanceof GuildChannel) {
} else if(channel instanceof GuildChannel) {
oldChannel = {
name: channel.name,
topic: channel.topic,
Expand All @@ -938,39 +912,38 @@ class Shard extends EventEmitter {
parentID: channel.parentID,
rateLimitPerUser: channel.rateLimitPerUser
};
} else {
this.emit("warn", `Unexpected CHANNEL_UPDATE for channel ${packet.d.id} with type ${oldType}`);
}
const oldType = channel.type;
channel.update(packet.d);
if(oldType !== packet.d.type) {
this.emit("warn", `Channel ${packet.d.id} changed from type ${oldType} to ${packet.d.type}`);
if(guild) {
if(packet.d.type === 2) {
guild.channels.remove(channel);
channel = guild.channels.add(new VoiceChannel(packet.d, guild), guild);
} else if(packet.d.type === 4) {
guild.channels.remove(channel);
channel = guild.channels.add(new CategoryChannel(packet.d, guild), guild);
} else if(packet.d.type === 5) {
guild.channels.remove(channel);
channel = guild.channels.add(new NewsChannel(packet.d, guild), guild);
} else if(packet.d.type === 0 || packet.d.last_message_id !== undefined) {
guild.channels.remove(channel);
channel = guild.channels.add(new TextChannel(packet.d, guild), guild);
}
}
}

if(packet.d.parent_id) {
if(packet.d.parent_id !== channel.parentID) {
if(!guild.channels.has(channel.parentID)) {
if(oldType === packet.d.type) {
channel.update(packet.d);
} else {
this.emit("debug", `Channel ${packet.d.id} changed from type ${oldType} to ${packet.d.type}`);
const newChannel = Channel.from(packet.d);
if(packet.d.guild_id) {
const guild = this.client.guilds.get(packet.d.guild_id);
if(!guild) {
this.emit("debug", `Received CHANNEL_UPDATE for channel in missing guild ${packet.d.guild_id}`);
break;
}
}
if(!guild.channels.has(packet.d.parent_id)) {
this.emit("debug", `Missing category channel ${packet.d.parent_id} in UPDATE`);
guild.channels.remove(channel);
guild.channels.add(newChannel, this.client);
} else if(channel instanceof PrivateChannel) {
if(channel instanceof GroupChannel) {
this.client.groupChannels.remove(channel);
this.client.groupChannels.add(newChannel, this.client);
} else {
this.client.privateChannels.remove(channel);
this.client.privateChannels.add(newChannel, this.client);
}
} else {
this.emit("warn", new Error("Unhandled CHANNEL_UPDATE type: " + JSON.stringify(packet, null, 2)));
break;
}
channel = newChannel;
}

/**
* Fired when a channel is updated
* @event Client#channelUpdate
Expand Down
10 changes: 10 additions & 0 deletions lib/structures/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ class Channel extends Base {
return new StoreChannel(data, client);
}
}
if(data.guild_id) {
if(data.last_message_id !== undefined) {
client.emit("warn", new Error(`Unknown guild text channel type: ${data.type}\n${JSON.stringify(data)}`));
return new TextChannel(data, client);
}
client.emit("warn", new Error(`Unknown guild channel type: ${data.type}\n${JSON.stringify(data)}`));
return new GuildChannel(data, client);
}
client.emit("warn", new Error(`Unknown channel type: ${data.type}\n${JSON.stringify(data)}`));
return new Channel(data);
}

Expand All @@ -61,6 +70,7 @@ module.exports = Channel;

// Circular import
const CategoryChannel = require("./CategoryChannel");
const GuildChannel = require("./GuildChannel");
const GroupChannel = require("./GroupChannel");
const NewsChannel = require("./NewsChannel");
const PrivateChannel = require("./PrivateChannel");
Expand Down

0 comments on commit b0c7a07

Please sign in to comment.