Skip to content

Commit

Permalink
Improve handling of messages in uncached channels (abalabahaha#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsian03 authored Mar 31, 2021
1 parent e79348d commit 8acd468
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
12 changes: 7 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ declare namespace Eris {
type GuildTextableChannel = TextChannel | NewsChannel;
type InviteChannel = InvitePartialChannel | Exclude<AnyGuildChannel, CategoryChannel>;
type TextableChannel = (GuildTextable & GuildTextableChannel) | (Textable & PrivateChannel);
type PossiblyUncachedTextable = Textable | { id: string };
type PossiblyUncachedTextableChannel = TextableChannel | { id: string };

// Command
type CommandGenerator = CommandGeneratorFunction | MessageContent | MessageContent[] | CommandGeneratorFunction[];
Expand Down Expand Up @@ -47,7 +49,7 @@ declare namespace Eris {
};
type ImageFormat = "jpg" | "jpeg" | "png" | "gif" | "webp";
type MessageContent = string | AdvancedMessageContent;
type PossiblyUncachedMessage = Message | { channel: TextableChannel | { id: string; guild: { id: string } }; guildID: string; id: string };
type PossiblyUncachedMessage = Message | { channel: TextableChannel | { id: string; guild?: { id: string } }; guildID?: string; id: string };

// Permission
type PermissionType = "role" | "member";
Expand Down Expand Up @@ -473,7 +475,7 @@ declare namespace Eris {
(event: "guildUpdate", listener: (guild: Guild, oldGuild: OldGuild) => void): T;
(event: "hello", listener: (trace: string[], id: number) => void): T;
(event: "inviteCreate" | "inviteDelete", listener: (guild: Guild, invite: Invite) => void): T;
(event: "messageCreate", listener: (message: Message) => void): T;
(event: "messageCreate", listener: (message: Message<PossiblyUncachedTextableChannel>) => void): T;
(event: "messageDelete" | "messageReactionRemoveAll", listener: (message: PossiblyUncachedMessage) => void): T;
(event: "messageReactionRemoveEmoji", listener: (message: PossiblyUncachedMessage, emoji: PartialEmoji) => void): T;
(event: "messageDeleteBulk", listener: (messages: PossiblyUncachedMessage[]) => void): T;
Expand All @@ -485,7 +487,7 @@ declare namespace Eris {
event: "messageReactionRemove",
listener: (message: PossiblyUncachedMessage, emoji: PartialEmoji, userID: string) => void
): T;
(event: "messageUpdate", listener: (message: Message, oldMessage: OldMessage | null) => void
(event: "messageUpdate", listener: (message: Message<PossiblyUncachedTextableChannel>, oldMessage: OldMessage | null) => void
): T;
(event: "presenceUpdate", listener: (other: Member | Relationship, oldPresence: Presence | null) => void): T;
(event: "rawREST", listener: (request: RawRESTRequest) => void): T;
Expand Down Expand Up @@ -2077,12 +2079,12 @@ declare namespace Eris {
unban(reason?: string): Promise<void>;
}

export class Message<T extends Textable = TextableChannel> extends Base {
export class Message<T extends PossiblyUncachedTextable = TextableChannel> extends Base {
activity?: MessageActivity;
application?: MessageApplication;
attachments: Attachment[];
author: User;
channel: T | { id: string };
channel: T;
channelMentions: string[];
/** @deprecated */
cleanContent: string;
Expand Down
16 changes: 8 additions & 8 deletions lib/gateway/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -894,27 +894,27 @@ class Shard extends EventEmitter {
this.emit("messageDelete", (channel && channel.messages.remove(packet.d)) || {
id: packet.d.id,
channel: channel || {
id: packet.d.channel_id
}
id: packet.d.channel_id,
guild: packet.d.guild_id ? {id: packet.d.guild_id} : undefined
},
guildID: packet.d.guild_id
});
break;
}
case "MESSAGE_DELETE_BULK": {
const channel = this.client.getChannel(packet.d.channel_id);
if(!channel) {
break;
}

/**
* Fired when a bulk delete occurs
* @event Client#messageDeleteBulk
* @prop {Array<Message> | Array<Object>} messages An array of (potentially partial) message objects. If a message is not cached, it will be an object with `id` and `channel` keys. No other property is guaranteed
*/
this.emit("messageDeleteBulk", packet.d.ids.map((id) => (channel.messages.remove({
this.emit("messageDeleteBulk", packet.d.ids.map((id) => (channel && channel.messages.remove({
id
}) || {
id,
channel
id: id,
channel: {id: packet.d.channel_id, guild: packet.d.guild_id ? {id: packet.d.guild_id} : undefined},
guildID: packet.d.guild_id
})));
break;
}
Expand Down

0 comments on commit 8acd468

Please sign in to comment.