Skip to content

Commit

Permalink
Fix channelUpdate event when type changes
Browse files Browse the repository at this point in the history
Fixes #178
  • Loading branch information
DonovanDMC committed Jul 1, 2024
1 parent 3fba256 commit ae20187
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
29 changes: 29 additions & 0 deletions lib/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ import type {
RawVoiceChannel
} from "./types/channels";
import type MediaChannel from "./structures/MediaChannel";
import type {
JSONAnnouncementChannel,
JSONAnnouncementThreadChannel,
JSONCategoryChannel,
JSONForumChannel,
JSONGroupChannel,
JSONMediaChannel,
JSONPrivateChannel,
JSONPrivateThreadChannel,
JSONPublicThreadChannel,
JSONStageChannel,
JSONTextChannel,
JSONVoiceChannel
} from "./types";
import pkg from "../package.json";

export const GATEWAY_VERSION = 10;
Expand Down Expand Up @@ -398,6 +412,21 @@ export interface RawChannelTypeMap {
[ChannelTypes.GUILD_FORUM]: RawForumChannel;
[ChannelTypes.GUILD_MEDIA]: RawMediaChannel;
}
export interface JSONChannelTypeMap {
[ChannelTypes.GUILD_TEXT]: JSONTextChannel;
[ChannelTypes.DM]: JSONPrivateChannel;
[ChannelTypes.GUILD_VOICE]: JSONVoiceChannel;
[ChannelTypes.GROUP_DM]: JSONGroupChannel;
[ChannelTypes.GUILD_CATEGORY]: JSONCategoryChannel;
[ChannelTypes.GUILD_ANNOUNCEMENT]: JSONAnnouncementChannel;
[ChannelTypes.ANNOUNCEMENT_THREAD]: JSONAnnouncementThreadChannel;
[ChannelTypes.PUBLIC_THREAD]: JSONPublicThreadChannel;
[ChannelTypes.PRIVATE_THREAD]: JSONPrivateThreadChannel;
[ChannelTypes.GUILD_STAGE_VOICE]: JSONStageChannel;
[ChannelTypes.GUILD_DIRECTORY]: never;
[ChannelTypes.GUILD_FORUM]: JSONForumChannel;
[ChannelTypes.GUILD_MEDIA]: JSONMediaChannel;
}
/* eslint-enable @typescript-eslint/member-ordering */

export enum OverwriteTypes {
Expand Down
17 changes: 13 additions & 4 deletions lib/gateway/Shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import type {
ThreadParentChannel,
UncachedThreadMember,
AnyVoiceChannel,
PollAnswer
PollAnswer,
AnyGuildChannel
} from "../types/channels";
import type TextChannel from "../structures/TextChannel";
import type { JSONAnnouncementThreadChannel } from "../types/json";
import VoiceChannel from "../structures/VoiceChannel";
import StageChannel from "../structures/StageChannel";
Expand Down Expand Up @@ -331,8 +331,17 @@ export default class Shard extends TypedEmitter<ShardEvents> {
}

case "CHANNEL_UPDATE": {
const oldChannel = this.client.getChannel<TextChannel>(packet.d.id)?.toJSON() ?? null;
const channel = this.client.util.updateChannel<TextChannel>(packet.d);
const oldChannel = this.client.getChannel<AnyGuildChannel>(packet.d.id)?.toJSON() ?? null;
let channel: AnyGuildChannel;
if (oldChannel && oldChannel.type !== packet.d.type) {
if (this.client.channelGuildMap[packet.d.id]) {
this.client.guilds.get(this.client.channelGuildMap[packet.d.id])!.channels.delete(packet.d.id);
}

channel = this.client.util.updateChannel(packet.d);
} else {
channel = this.client.util.updateChannel(packet.d);
}
this.client.emit("channelUpdate", channel, oldChannel);
break;
}
Expand Down
17 changes: 5 additions & 12 deletions lib/types/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ import type {
UncachedThreadMember,
AnyVoiceChannel,
PollAnswer,
EventReaction
EventReaction,
GuildChannels,
AnyGuildChannel
} from "./channels";
import type { RawRequest } from "./request-handler";
import type { AutoModerationActionExecution, DeletedPrivateChannel, VoiceChannelEffect } from "./gateway";
import type { AnyDispatchPacket } from "./gateway-raw";
import type { Uncached } from "./shared";
import type {
JSONAnnouncementChannel,
JSONAnnouncementThreadChannel,
JSONAutoModerationRule,
JSONCategoryChannel,
JSONThreadOnlyChannel,
JSONGuild,
JSONIntegration,
JSONMember,
Expand All @@ -32,11 +31,8 @@ import type {
JSONPublicThreadChannel,
JSONRole,
JSONScheduledEvent,
JSONStageChannel,
JSONStageInstance,
JSONTextChannel,
JSONUser,
JSONVoiceChannel,
JSONVoiceState,
JSONEntitlement,
JSONTestEntitlement
Expand All @@ -56,10 +52,7 @@ import type AutoModerationRule from "../structures/AutoModerationRule";
import type AnnouncementThreadChannel from "../structures/AnnouncementThreadChannel";
import type PublicThreadChannel from "../structures/PublicThreadChannel";
import type PrivateThreadChannel from "../structures/PrivateThreadChannel";
import type TextChannel from "../structures/TextChannel";
import type VoiceChannel from "../structures/VoiceChannel";
import type CategoryChannel from "../structures/CategoryChannel";
import type AnnouncementChannel from "../structures/AnnouncementChannel";
import type StageChannel from "../structures/StageChannel";
import type User from "../structures/User";
import type Member from "../structures/Member";
Expand All @@ -70,11 +63,11 @@ import type Invite from "../structures/Invite";
import type Message from "../structures/Message";
import type PrivateChannel from "../structures/PrivateChannel";
import type StageInstance from "../structures/StageInstance";
import type ForumChannel from "../structures/ForumChannel";
import type AuditLogEntry from "../structures/AuditLogEntry";
import type GroupChannel from "../structures/GroupChannel";
import type Entitlement from "../structures/Entitlement";
import type TestEntitlement from "../structures/TestEntitlement";
import type { JSONChannelTypeMap } from "../Constants";


export interface ClientEvents {
Expand All @@ -95,7 +88,7 @@ export interface ClientEvents {
/** @event Emitted when a channel's pins are updated (message pinned, message unpinned). Requires the `GUILDS` intent for guild channels, and `DIRECT_MESSAGES` for direct messages. */
channelPinsUpdate: [channel: AnyTextableChannel | Uncached, timestamp: Date | null];
/** @event Emitted when a channel is updated. Requires the `GUILDS` intent. */
channelUpdate: [channel: TextChannel, oldChannel: JSONTextChannel | null] | [channel: VoiceChannel, oldChannel: JSONVoiceChannel | null] | [channel: CategoryChannel, oldChannel: JSONCategoryChannel | null] | [channel: AnnouncementChannel, oldChannel: JSONAnnouncementChannel | null] | [channel: StageChannel, oldChannel: JSONStageChannel | null] | [channel: ForumChannel, oldChannel: JSONThreadOnlyChannel | null];
channelUpdate: [channel: AnyGuildChannel, oldChannel: JSONChannelTypeMap[GuildChannels] | null];
/** @event Emitted when a shard connects. */
connect: [id: number];
/** @event Emitted with various information for debugging. */
Expand Down

0 comments on commit ae20187

Please sign in to comment.