Skip to content

Commit

Permalink
Reduce channel cache reliance in Message events (abalabahaha#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaaz authored Mar 24, 2021
1 parent 9cd3a5d commit c81b689
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2034,7 +2034,7 @@ declare namespace Eris {
application?: MessageApplication;
attachments: Attachment[];
author: User;
channel: T;
channel: T | { id: string };
channelMentions: string[];
/** @deprecated */
cleanContent: string;
Expand Down
23 changes: 14 additions & 9 deletions lib/gateway/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Call = require("../structures/Call");
const Channel = require("../structures/Channel");
const GroupChannel = require("../structures/GroupChannel");
const GuildChannel = require("../structures/GuildChannel");
const Message = require("../structures/Message");
const PrivateChannel = require("../structures/PrivateChannel");
const {GATEWAY_VERSION, GatewayOPCodes, ChannelTypes} = require("../Constants");
const ExtendedUser = require("../structures/ExtendedUser");
Expand Down Expand Up @@ -822,17 +823,21 @@ class Shard extends EventEmitter {
/**
* Fired when a message is created
* @event Client#messageCreate
* @prop {Message} message The message
* @prop {Message} message The message.
*/
this.emit("messageCreate", channel.messages.add(packet.d, this.client));
} else {
this.emit("debug", "MESSAGE_CREATE but channel not found (OK if deleted channel)", this.id);
this.emit("messageCreate", new Message(packet.d, this.client));
}
break;
}
case "MESSAGE_UPDATE": {
const channel = this.client.getChannel(packet.d.channel_id);
if(!channel) {
packet.d.channel = {
id: packet.d.channel_id
};
this.emit("messageUpdate", packet.d, null);
break;
}
const message = channel.messages.get(packet.d.id);
Expand All @@ -859,7 +864,7 @@ class Shard extends EventEmitter {
/**
* Fired when a message is updated
* @event Client#messageUpdate
* @prop {Message} message The updated message. If oldMessage is null, it is recommended to discard this event, since the message data will be very incomplete (only `id` and `channel` are guaranteed)
* @prop {Message} message The updated message. If oldMessage is null, it is recommended to discard this event, since the message data will be very incomplete (only `id` and `channel` are guaranteed). If the channel isn't cached, `channel` will be an object with an `id` key.
* @prop {Object?} oldMessage The old message data. If the message was cached, this will return the full old message. Otherwise, it will be null
* @prop {Array<Object>} oldMessage.attachments Array of attachments
* @prop {Array<String>} oldMessage.channelMentions Array of mentions channels' ids.
Expand All @@ -878,17 +883,17 @@ class Shard extends EventEmitter {
}
case "MESSAGE_DELETE": {
const channel = this.client.getChannel(packet.d.channel_id);
if(!channel) {
break;
}

/**
* Fired when a cached message is deleted
* @event Client#messageDelete
* @prop {Message | Object} message The message object. If the message is not cached, this will be an object with `id` and `channel` keys. No other property is guaranteed
* @prop {Message | Object} message The message object. If the message is not cached, this will be an object with `id` and `channel` keys. No other property is guaranteed. If the channel is not cached, channel will be an object with an `id` key.
*/
this.emit("messageDelete", channel.messages.remove(packet.d) || {
this.emit("messageDelete", (channel && channel.messages.remove(packet.d)) || {
id: packet.d.id,
channel: channel
channel: channel || {
id: packet.d.channel_id
}
});
break;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const User = require("./User");
* @prop {Object?} application The application of the activity in the message
* @prop {Array<Object>} attachments Array of attachments
* @prop {User} author The message author
* @prop {PrivateChannel | TextChannel | NewsChannel} channel The channel the message is in
* @prop {PrivateChannel | TextChannel | NewsChannel} channel The channel the message is in. Can be partial with only the id if the channel is not cached.
* @prop {Array<String>} channelMentions Array of mentions channels' ids
* @prop {String?} cleanContent Message content with mentions replaced by names. Mentions are currently escaped, but this behavior is [DEPRECATED] and will be removed soon. Use allowed mentions, the official way of avoiding unintended mentions, when creating messages.
* @prop {Command?} command The Command used in the Message, if any (CommandClient only)
Expand Down

0 comments on commit c81b689

Please sign in to comment.