From 18ab0cf62a3ed86b5b6256b882e04d18eeb5e956 Mon Sep 17 00:00:00 2001 From: Almeida Date: Mon, 13 Jan 2025 11:29:57 +0000 Subject: [PATCH] refactor(actions): removed unnecessary actions (#10666) Removed actions that were only being used in their respective websocket handlers. The remaining actions either were either being used elsewhere or were using methods from the `GenericAction` class. Co-authored-by: Vlad Frangu --- .../src/client/actions/ActionsManager.js | 24 -------- .../ApplicationCommandPermissionsUpdate.js | 34 ----------- .../actions/AutoModerationActionExecution.js | 26 -------- .../actions/AutoModerationRuleCreate.js | 27 --------- .../actions/AutoModerationRuleDelete.js | 31 ---------- .../actions/AutoModerationRuleUpdate.js | 29 --------- .../src/client/actions/EntitlementCreate.js | 23 ------- .../src/client/actions/EntitlementDelete.js | 27 --------- .../src/client/actions/EntitlementUpdate.js | 25 -------- .../actions/GuildAuditLogEntryCreate.js | 29 --------- .../src/client/actions/GuildBanAdd.js | 20 ------- .../src/client/actions/GuildBanRemove.js | 25 -------- .../src/client/actions/GuildDelete.js | 44 -------------- .../client/actions/GuildIntegrationsUpdate.js | 19 ------ .../src/client/actions/GuildRoleUpdate.js | 39 ------------ .../actions/GuildScheduledEventCreate.js | 27 --------- .../actions/GuildScheduledEventUpdate.js | 30 ---------- .../src/client/actions/InviteCreate.js | 27 --------- .../src/client/actions/InviteDelete.js | 29 --------- .../src/client/actions/PresenceUpdate.js | 45 -------------- .../src/client/actions/ThreadDelete.js | 26 -------- .../src/client/actions/ThreadListSync.js | 60 ------------------- .../src/client/actions/ThreadMemberUpdate.js | 30 ---------- .../src/client/actions/VoiceStateUpdate.js | 43 ------------- .../src/client/actions/WebhooksUpdate.js | 22 ------- .../APPLICATION_COMMAND_PERMISSIONS_UPDATE.js | 18 +++++- .../AUTO_MODERATION_ACTION_EXECUTION.js | 16 ++++- .../handlers/AUTO_MODERATION_RULE_CREATE.js | 17 +++++- .../handlers/AUTO_MODERATION_RULE_DELETE.js | 20 ++++++- .../handlers/AUTO_MODERATION_RULE_UPDATE.js | 19 +++++- .../websocket/handlers/ENTITLEMENT_CREATE.js | 13 +++- .../websocket/handlers/ENTITLEMENT_DELETE.js | 17 +++++- .../websocket/handlers/ENTITLEMENT_UPDATE.js | 15 ++++- .../handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js | 18 +++++- .../websocket/handlers/GUILD_BAN_ADD.js | 14 ++++- .../websocket/handlers/GUILD_BAN_REMOVE.js | 19 +++++- .../client/websocket/handlers/GUILD_DELETE.js | 34 ++++++++++- .../handlers/GUILD_INTEGRATIONS_UPDATE.js | 14 ++++- .../websocket/handlers/GUILD_ROLE_UPDATE.js | 20 ++++++- .../handlers/GUILD_SCHEDULED_EVENT_CREATE.js | 16 ++++- .../handlers/GUILD_SCHEDULED_EVENT_UPDATE.js | 18 +++++- .../websocket/handlers/INVITE_CREATE.js | 19 +++++- .../websocket/handlers/INVITE_DELETE.js | 22 ++++++- .../websocket/handlers/PRESENCE_UPDATE.js | 42 ++++++++++++- .../websocket/handlers/THREAD_DELETE.js | 16 ++++- .../websocket/handlers/THREAD_LIST_SYNC.js | 48 ++++++++++++++- .../handlers/THREAD_MEMBER_UPDATE.js | 24 +++++++- .../websocket/handlers/VOICE_STATE_UPDATE.js | 36 ++++++++++- .../websocket/handlers/WEBHOOKS_UPDATE.js | 15 ++++- 49 files changed, 462 insertions(+), 809 deletions(-) delete mode 100644 packages/discord.js/src/client/actions/ApplicationCommandPermissionsUpdate.js delete mode 100644 packages/discord.js/src/client/actions/AutoModerationActionExecution.js delete mode 100644 packages/discord.js/src/client/actions/AutoModerationRuleCreate.js delete mode 100644 packages/discord.js/src/client/actions/AutoModerationRuleDelete.js delete mode 100644 packages/discord.js/src/client/actions/AutoModerationRuleUpdate.js delete mode 100644 packages/discord.js/src/client/actions/EntitlementCreate.js delete mode 100644 packages/discord.js/src/client/actions/EntitlementDelete.js delete mode 100644 packages/discord.js/src/client/actions/EntitlementUpdate.js delete mode 100644 packages/discord.js/src/client/actions/GuildAuditLogEntryCreate.js delete mode 100644 packages/discord.js/src/client/actions/GuildBanAdd.js delete mode 100644 packages/discord.js/src/client/actions/GuildBanRemove.js delete mode 100644 packages/discord.js/src/client/actions/GuildDelete.js delete mode 100644 packages/discord.js/src/client/actions/GuildIntegrationsUpdate.js delete mode 100644 packages/discord.js/src/client/actions/GuildRoleUpdate.js delete mode 100644 packages/discord.js/src/client/actions/GuildScheduledEventCreate.js delete mode 100644 packages/discord.js/src/client/actions/GuildScheduledEventUpdate.js delete mode 100644 packages/discord.js/src/client/actions/InviteCreate.js delete mode 100644 packages/discord.js/src/client/actions/InviteDelete.js delete mode 100644 packages/discord.js/src/client/actions/PresenceUpdate.js delete mode 100644 packages/discord.js/src/client/actions/ThreadDelete.js delete mode 100644 packages/discord.js/src/client/actions/ThreadListSync.js delete mode 100644 packages/discord.js/src/client/actions/ThreadMemberUpdate.js delete mode 100644 packages/discord.js/src/client/actions/VoiceStateUpdate.js delete mode 100644 packages/discord.js/src/client/actions/WebhooksUpdate.js diff --git a/packages/discord.js/src/client/actions/ActionsManager.js b/packages/discord.js/src/client/actions/ActionsManager.js index dd305a948..49e2a9693 100644 --- a/packages/discord.js/src/client/actions/ActionsManager.js +++ b/packages/discord.js/src/client/actions/ActionsManager.js @@ -11,36 +11,20 @@ class ActionsManager { constructor(client) { this.client = client; - this.register(require('./ApplicationCommandPermissionsUpdate')); - this.register(require('./AutoModerationActionExecution')); - this.register(require('./AutoModerationRuleCreate')); - this.register(require('./AutoModerationRuleDelete')); - this.register(require('./AutoModerationRuleUpdate')); this.register(require('./ChannelCreate')); this.register(require('./ChannelDelete')); this.register(require('./ChannelUpdate')); - this.register(require('./EntitlementCreate')); - this.register(require('./EntitlementDelete')); - this.register(require('./EntitlementUpdate')); - this.register(require('./GuildAuditLogEntryCreate')); - this.register(require('./GuildBanAdd')); - this.register(require('./GuildBanRemove')); this.register(require('./GuildChannelsPositionUpdate')); - this.register(require('./GuildDelete')); this.register(require('./GuildEmojiCreate')); this.register(require('./GuildEmojiDelete')); this.register(require('./GuildEmojiUpdate')); this.register(require('./GuildEmojisUpdate')); - this.register(require('./GuildIntegrationsUpdate')); this.register(require('./GuildMemberRemove')); this.register(require('./GuildMemberUpdate')); this.register(require('./GuildRoleCreate')); this.register(require('./GuildRoleDelete')); - this.register(require('./GuildRoleUpdate')); this.register(require('./GuildRolesPositionUpdate')); - this.register(require('./GuildScheduledEventCreate')); this.register(require('./GuildScheduledEventDelete')); - this.register(require('./GuildScheduledEventUpdate')); this.register(require('./GuildScheduledEventUserAdd')); this.register(require('./GuildScheduledEventUserRemove')); this.register(require('./GuildStickerCreate')); @@ -49,8 +33,6 @@ class ActionsManager { this.register(require('./GuildStickersUpdate')); this.register(require('./GuildUpdate')); this.register(require('./InteractionCreate')); - this.register(require('./InviteCreate')); - this.register(require('./InviteDelete')); this.register(require('./MessageCreate')); this.register(require('./MessageDelete')); this.register(require('./MessageDeleteBulk')); @@ -61,19 +43,13 @@ class ActionsManager { this.register(require('./MessageReactionRemoveAll')); this.register(require('./MessageReactionRemoveEmoji')); this.register(require('./MessageUpdate')); - this.register(require('./PresenceUpdate')); this.register(require('./StageInstanceCreate')); this.register(require('./StageInstanceDelete')); this.register(require('./StageInstanceUpdate')); this.register(require('./ThreadCreate')); - this.register(require('./ThreadDelete')); - this.register(require('./ThreadListSync')); - this.register(require('./ThreadMemberUpdate')); this.register(require('./ThreadMembersUpdate')); this.register(require('./TypingStart')); this.register(require('./UserUpdate')); - this.register(require('./VoiceStateUpdate')); - this.register(require('./WebhooksUpdate')); } register(Action) { diff --git a/packages/discord.js/src/client/actions/ApplicationCommandPermissionsUpdate.js b/packages/discord.js/src/client/actions/ApplicationCommandPermissionsUpdate.js deleted file mode 100644 index f2bc214d2..000000000 --- a/packages/discord.js/src/client/actions/ApplicationCommandPermissionsUpdate.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -/** - * The data received in the {@link Client#event:applicationCommandPermissionsUpdate} event - * @typedef {Object} ApplicationCommandPermissionsUpdateData - * @property {Snowflake} id The id of the command or global entity that was updated - * @property {Snowflake} guildId The id of the guild in which permissions were updated - * @property {Snowflake} applicationId The id of the application that owns the command or entity being updated - * @property {ApplicationCommandPermissions[]} permissions The updated permissions - */ - -class ApplicationCommandPermissionsUpdateAction extends Action { - handle(data) { - const client = this.client; - /** - * Emitted whenever permissions for an application command in a guild were updated. - * This includes permission updates for other applications in addition to the logged in client, - * check `data.applicationId` to verify which application the update is for - * @event Client#applicationCommandPermissionsUpdate - * @param {ApplicationCommandPermissionsUpdateData} data The updated permissions - */ - client.emit(Events.ApplicationCommandPermissionsUpdate, { - permissions: data.permissions, - id: data.id, - guildId: data.guild_id, - applicationId: data.application_id, - }); - } -} - -module.exports = ApplicationCommandPermissionsUpdateAction; diff --git a/packages/discord.js/src/client/actions/AutoModerationActionExecution.js b/packages/discord.js/src/client/actions/AutoModerationActionExecution.js deleted file mode 100644 index ad601167c..000000000 --- a/packages/discord.js/src/client/actions/AutoModerationActionExecution.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const AutoModerationActionExecution = require('../../structures/AutoModerationActionExecution'); -const Events = require('../../util/Events'); - -class AutoModerationActionExecutionAction extends Action { - handle(data) { - const { client } = this; - const guild = client.guilds.cache.get(data.guild_id); - - if (guild) { - /** - * Emitted whenever an auto moderation rule is triggered. - * This event requires the {@link PermissionFlagsBits.ManageGuild} permission. - * @event Client#autoModerationActionExecution - * @param {AutoModerationActionExecution} autoModerationActionExecution The data of the execution - */ - client.emit(Events.AutoModerationActionExecution, new AutoModerationActionExecution(data, guild)); - } - - return {}; - } -} - -module.exports = AutoModerationActionExecutionAction; diff --git a/packages/discord.js/src/client/actions/AutoModerationRuleCreate.js b/packages/discord.js/src/client/actions/AutoModerationRuleCreate.js deleted file mode 100644 index 775b1d370..000000000 --- a/packages/discord.js/src/client/actions/AutoModerationRuleCreate.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class AutoModerationRuleCreateAction extends Action { - handle(data) { - const { client } = this; - const guild = client.guilds.cache.get(data.guild_id); - - if (guild) { - const autoModerationRule = guild.autoModerationRules._add(data); - - /** - * Emitted whenever an auto moderation rule is created. - * This event requires the {@link PermissionFlagsBits.ManageGuild} permission. - * @event Client#autoModerationRuleCreate - * @param {AutoModerationRule} autoModerationRule The created auto moderation rule - */ - client.emit(Events.AutoModerationRuleCreate, autoModerationRule); - } - - return {}; - } -} - -module.exports = AutoModerationRuleCreateAction; diff --git a/packages/discord.js/src/client/actions/AutoModerationRuleDelete.js b/packages/discord.js/src/client/actions/AutoModerationRuleDelete.js deleted file mode 100644 index 641822c24..000000000 --- a/packages/discord.js/src/client/actions/AutoModerationRuleDelete.js +++ /dev/null @@ -1,31 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class AutoModerationRuleDeleteAction extends Action { - handle(data) { - const { client } = this; - const guild = client.guilds.cache.get(data.guild_id); - - if (guild) { - const autoModerationRule = guild.autoModerationRules.cache.get(data.id); - - if (autoModerationRule) { - guild.autoModerationRules.cache.delete(autoModerationRule.id); - - /** - * Emitted whenever an auto moderation rule is deleted. - * This event requires the {@link PermissionFlagsBits.ManageGuild} permission. - * @event Client#autoModerationRuleDelete - * @param {AutoModerationRule} autoModerationRule The deleted auto moderation rule - */ - client.emit(Events.AutoModerationRuleDelete, autoModerationRule); - } - } - - return {}; - } -} - -module.exports = AutoModerationRuleDeleteAction; diff --git a/packages/discord.js/src/client/actions/AutoModerationRuleUpdate.js b/packages/discord.js/src/client/actions/AutoModerationRuleUpdate.js deleted file mode 100644 index 56e39568c..000000000 --- a/packages/discord.js/src/client/actions/AutoModerationRuleUpdate.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class AutoModerationRuleUpdateAction extends Action { - handle(data) { - const { client } = this; - const guild = client.guilds.cache.get(data.guild_id); - - if (guild) { - const oldAutoModerationRule = guild.autoModerationRules.cache.get(data.id)?._clone() ?? null; - const newAutoModerationRule = guild.autoModerationRules._add(data); - - /** - * Emitted whenever an auto moderation rule gets updated. - * This event requires the {@link PermissionFlagsBits.ManageGuild} permission. - * @event Client#autoModerationRuleUpdate - * @param {?AutoModerationRule} oldAutoModerationRule The auto moderation rule before the update - * @param {AutoModerationRule} newAutoModerationRule The auto moderation rule after the update - */ - client.emit(Events.AutoModerationRuleUpdate, oldAutoModerationRule, newAutoModerationRule); - } - - return {}; - } -} - -module.exports = AutoModerationRuleUpdateAction; diff --git a/packages/discord.js/src/client/actions/EntitlementCreate.js b/packages/discord.js/src/client/actions/EntitlementCreate.js deleted file mode 100644 index 63082af25..000000000 --- a/packages/discord.js/src/client/actions/EntitlementCreate.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class EntitlementCreateAction extends Action { - handle(data) { - const client = this.client; - - const entitlement = client.application.entitlements._add(data); - - /** - * Emitted whenever an entitlement is created. - * @event Client#entitlementCreate - * @param {Entitlement} entitlement The entitlement that was created - */ - client.emit(Events.EntitlementCreate, entitlement); - - return {}; - } -} - -module.exports = EntitlementCreateAction; diff --git a/packages/discord.js/src/client/actions/EntitlementDelete.js b/packages/discord.js/src/client/actions/EntitlementDelete.js deleted file mode 100644 index 7a397548f..000000000 --- a/packages/discord.js/src/client/actions/EntitlementDelete.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class EntitlementDeleteAction extends Action { - handle(data) { - const client = this.client; - - const entitlement = client.application.entitlements._add(data, false); - - client.application.entitlements.cache.delete(entitlement.id); - - /** - * Emitted whenever an entitlement is deleted. - * Entitlements are not deleted when they expire. - * This is only triggered when Discord issues a refund or deletes the entitlement manually. - * @event Client#entitlementDelete - * @param {Entitlement} entitlement The entitlement that was deleted - */ - client.emit(Events.EntitlementDelete, entitlement); - - return {}; - } -} - -module.exports = EntitlementDeleteAction; diff --git a/packages/discord.js/src/client/actions/EntitlementUpdate.js b/packages/discord.js/src/client/actions/EntitlementUpdate.js deleted file mode 100644 index 10a61dbc0..000000000 --- a/packages/discord.js/src/client/actions/EntitlementUpdate.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class EntitlementUpdateAction extends Action { - handle(data) { - const client = this.client; - - const oldEntitlement = client.application.entitlements.cache.get(data.id)?._clone() ?? null; - const newEntitlement = client.application.entitlements._add(data); - - /** - * Emitted whenever an entitlement is updated - i.e. when a user's subscription renews. - * @event Client#entitlementUpdate - * @param {?Entitlement} oldEntitlement The entitlement before the update - * @param {Entitlement} newEntitlement The entitlement after the update - */ - client.emit(Events.EntitlementUpdate, oldEntitlement, newEntitlement); - - return {}; - } -} - -module.exports = EntitlementUpdateAction; diff --git a/packages/discord.js/src/client/actions/GuildAuditLogEntryCreate.js b/packages/discord.js/src/client/actions/GuildAuditLogEntryCreate.js deleted file mode 100644 index fa16de60b..000000000 --- a/packages/discord.js/src/client/actions/GuildAuditLogEntryCreate.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const GuildAuditLogsEntry = require('../../structures/GuildAuditLogsEntry'); -const Events = require('../../util/Events'); - -class GuildAuditLogEntryCreateAction extends Action { - handle(data) { - const client = this.client; - const guild = client.guilds.cache.get(data.guild_id); - let auditLogEntry; - - if (guild) { - auditLogEntry = new GuildAuditLogsEntry(guild, data); - - /** - * Emitted whenever a guild audit log entry is created. - * @event Client#guildAuditLogEntryCreate - * @param {GuildAuditLogsEntry} auditLogEntry The entry that was created - * @param {Guild} guild The guild where the entry was created - */ - client.emit(Events.GuildAuditLogEntryCreate, auditLogEntry, guild); - } - - return { auditLogEntry }; - } -} - -module.exports = GuildAuditLogEntryCreateAction; diff --git a/packages/discord.js/src/client/actions/GuildBanAdd.js b/packages/discord.js/src/client/actions/GuildBanAdd.js deleted file mode 100644 index 2ef4b11ac..000000000 --- a/packages/discord.js/src/client/actions/GuildBanAdd.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class GuildBanAdd extends Action { - handle(data) { - const client = this.client; - const guild = client.guilds.cache.get(data.guild_id); - - /** - * Emitted whenever a member is banned from a guild. - * @event Client#guildBanAdd - * @param {GuildBan} ban The ban that occurred - */ - if (guild) client.emit(Events.GuildBanAdd, guild.bans._add(data)); - } -} - -module.exports = GuildBanAdd; diff --git a/packages/discord.js/src/client/actions/GuildBanRemove.js b/packages/discord.js/src/client/actions/GuildBanRemove.js deleted file mode 100644 index 8048efd8e..000000000 --- a/packages/discord.js/src/client/actions/GuildBanRemove.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const GuildBan = require('../../structures/GuildBan'); -const Events = require('../../util/Events'); - -class GuildBanRemove extends Action { - handle(data) { - const client = this.client; - const guild = client.guilds.cache.get(data.guild_id); - - /** - * Emitted whenever a member is unbanned from a guild. - * @event Client#guildBanRemove - * @param {GuildBan} ban The ban that was removed - */ - if (guild) { - const ban = guild.bans.cache.get(data.user.id) ?? new GuildBan(client, data, guild); - guild.bans.cache.delete(ban.user.id); - client.emit(Events.GuildBanRemove, ban); - } - } -} - -module.exports = GuildBanRemove; diff --git a/packages/discord.js/src/client/actions/GuildDelete.js b/packages/discord.js/src/client/actions/GuildDelete.js deleted file mode 100644 index eb0a44d1e..000000000 --- a/packages/discord.js/src/client/actions/GuildDelete.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class GuildDeleteAction extends Action { - handle(data) { - const client = this.client; - - let guild = client.guilds.cache.get(data.id); - if (guild) { - if (data.unavailable) { - // Guild is unavailable - guild.available = false; - - /** - * Emitted whenever a guild becomes unavailable, likely due to a server outage. - * @event Client#guildUnavailable - * @param {Guild} guild The guild that has become unavailable - */ - client.emit(Events.GuildUnavailable, guild); - - // Stops the GuildDelete packet thinking a guild was actually deleted, - // handles emitting of event itself - return; - } - - for (const channel of guild.channels.cache.values()) this.client.channels._remove(channel.id); - client.voice.adapters.get(data.id)?.destroy(); - - // Delete guild - client.guilds.cache.delete(guild.id); - - /** - * Emitted whenever a guild kicks the client or the guild is deleted/left. - * @event Client#guildDelete - * @param {Guild} guild The guild that was deleted - */ - client.emit(Events.GuildDelete, guild); - } - } -} - -module.exports = GuildDeleteAction; diff --git a/packages/discord.js/src/client/actions/GuildIntegrationsUpdate.js b/packages/discord.js/src/client/actions/GuildIntegrationsUpdate.js deleted file mode 100644 index 28b9bbb15..000000000 --- a/packages/discord.js/src/client/actions/GuildIntegrationsUpdate.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class GuildIntegrationsUpdate extends Action { - handle(data) { - const client = this.client; - const guild = client.guilds.cache.get(data.guild_id); - /** - * Emitted whenever a guild integration is updated - * @event Client#guildIntegrationsUpdate - * @param {Guild} guild The guild whose integrations were updated - */ - if (guild) client.emit(Events.GuildIntegrationsUpdate, guild); - } -} - -module.exports = GuildIntegrationsUpdate; diff --git a/packages/discord.js/src/client/actions/GuildRoleUpdate.js b/packages/discord.js/src/client/actions/GuildRoleUpdate.js deleted file mode 100644 index b0632c599..000000000 --- a/packages/discord.js/src/client/actions/GuildRoleUpdate.js +++ /dev/null @@ -1,39 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class GuildRoleUpdateAction extends Action { - handle(data) { - const client = this.client; - const guild = client.guilds.cache.get(data.guild_id); - - if (guild) { - let old = null; - - const role = guild.roles.cache.get(data.role.id); - if (role) { - old = role._update(data.role); - /** - * Emitted whenever a guild role is updated. - * @event Client#roleUpdate - * @param {Role} oldRole The role before the update - * @param {Role} newRole The role after the update - */ - client.emit(Events.GuildRoleUpdate, old, role); - } - - return { - old, - updated: role, - }; - } - - return { - old: null, - updated: null, - }; - } -} - -module.exports = GuildRoleUpdateAction; diff --git a/packages/discord.js/src/client/actions/GuildScheduledEventCreate.js b/packages/discord.js/src/client/actions/GuildScheduledEventCreate.js deleted file mode 100644 index 0a2fb9b6b..000000000 --- a/packages/discord.js/src/client/actions/GuildScheduledEventCreate.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class GuildScheduledEventCreateAction extends Action { - handle(data) { - const client = this.client; - const guild = client.guilds.cache.get(data.guild_id); - if (guild) { - const guildScheduledEvent = guild.scheduledEvents._add(data); - - /** - * Emitted whenever a guild scheduled event is created. - * @event Client#guildScheduledEventCreate - * @param {GuildScheduledEvent} guildScheduledEvent The created guild scheduled event - */ - client.emit(Events.GuildScheduledEventCreate, guildScheduledEvent); - - return { guildScheduledEvent }; - } - - return {}; - } -} - -module.exports = GuildScheduledEventCreateAction; diff --git a/packages/discord.js/src/client/actions/GuildScheduledEventUpdate.js b/packages/discord.js/src/client/actions/GuildScheduledEventUpdate.js deleted file mode 100644 index 7cabd850b..000000000 --- a/packages/discord.js/src/client/actions/GuildScheduledEventUpdate.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class GuildScheduledEventUpdateAction extends Action { - handle(data) { - const client = this.client; - const guild = client.guilds.cache.get(data.guild_id); - - if (guild) { - const oldGuildScheduledEvent = guild.scheduledEvents.cache.get(data.id)?._clone() ?? null; - const newGuildScheduledEvent = guild.scheduledEvents._add(data); - - /** - * Emitted whenever a guild scheduled event gets updated. - * @event Client#guildScheduledEventUpdate - * @param {?GuildScheduledEvent} oldGuildScheduledEvent The guild scheduled event object before the update - * @param {GuildScheduledEvent} newGuildScheduledEvent The guild scheduled event object after the update - */ - client.emit(Events.GuildScheduledEventUpdate, oldGuildScheduledEvent, newGuildScheduledEvent); - - return { oldGuildScheduledEvent, newGuildScheduledEvent }; - } - - return {}; - } -} - -module.exports = GuildScheduledEventUpdateAction; diff --git a/packages/discord.js/src/client/actions/InviteCreate.js b/packages/discord.js/src/client/actions/InviteCreate.js deleted file mode 100644 index dc03f07a2..000000000 --- a/packages/discord.js/src/client/actions/InviteCreate.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class InviteCreateAction extends Action { - handle(data) { - const client = this.client; - const channel = client.channels.cache.get(data.channel_id); - const guild = client.guilds.cache.get(data.guild_id); - if (!channel) return false; - - const inviteData = Object.assign(data, { channel, guild }); - const invite = guild.invites._add(inviteData); - - /** - * Emitted when an invite is created. - * This event requires the {@link PermissionFlagsBits.ManageChannels} permission for the channel. - * @event Client#inviteCreate - * @param {Invite} invite The invite that was created - */ - client.emit(Events.InviteCreate, invite); - return { invite }; - } -} - -module.exports = InviteCreateAction; diff --git a/packages/discord.js/src/client/actions/InviteDelete.js b/packages/discord.js/src/client/actions/InviteDelete.js deleted file mode 100644 index 58be00c81..000000000 --- a/packages/discord.js/src/client/actions/InviteDelete.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Invite = require('../../structures/Invite'); -const Events = require('../../util/Events'); - -class InviteDeleteAction extends Action { - handle(data) { - const client = this.client; - const channel = client.channels.cache.get(data.channel_id); - const guild = client.guilds.cache.get(data.guild_id); - if (!channel) return false; - - const inviteData = Object.assign(data, { channel, guild }); - const invite = new Invite(client, inviteData); - guild.invites.cache.delete(invite.code); - - /** - * Emitted when an invite is deleted. - * This event requires the {@link PermissionFlagsBits.ManageChannels} permission for the channel. - * @event Client#inviteDelete - * @param {Invite} invite The invite that was deleted - */ - client.emit(Events.InviteDelete, invite); - return { invite }; - } -} - -module.exports = InviteDeleteAction; diff --git a/packages/discord.js/src/client/actions/PresenceUpdate.js b/packages/discord.js/src/client/actions/PresenceUpdate.js deleted file mode 100644 index eea2008a4..000000000 --- a/packages/discord.js/src/client/actions/PresenceUpdate.js +++ /dev/null @@ -1,45 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); -const Partials = require('../util/Partials'); - -class PresenceUpdateAction extends Action { - handle(data) { - let user = this.client.users.cache.get(data.user.id); - if (!user && ('username' in data.user || this.client.options.partials.includes(Partials.User))) { - user = this.client.users._add(data.user); - } - if (!user) return; - - if (data.user.username) { - if (!user._equals(data.user)) this.client.actions.UserUpdate.handle(data.user); - } - - const guild = this.client.guilds.cache.get(data.guild_id); - if (!guild) return; - - const oldPresence = guild.presences.cache.get(user.id)?._clone() ?? null; - let member = guild.members.cache.get(user.id); - if (!member && data.status !== 'offline') { - member = guild.members._add({ - user, - deaf: false, - mute: false, - }); - this.client.emit(Events.GuildMemberAvailable, member); - } - const newPresence = guild.presences._add(Object.assign(data, { guild })); - if (this.client.listenerCount(Events.PresenceUpdate) && !newPresence.equals(oldPresence)) { - /** - * Emitted whenever a guild member's presence (e.g. status, activity) is changed. - * @event Client#presenceUpdate - * @param {?Presence} oldPresence The presence before the update, if one at all - * @param {Presence} newPresence The presence after the update - */ - this.client.emit(Events.PresenceUpdate, oldPresence, newPresence); - } - } -} - -module.exports = PresenceUpdateAction; diff --git a/packages/discord.js/src/client/actions/ThreadDelete.js b/packages/discord.js/src/client/actions/ThreadDelete.js deleted file mode 100644 index 3ec81a4e4..000000000 --- a/packages/discord.js/src/client/actions/ThreadDelete.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class ThreadDeleteAction extends Action { - handle(data) { - const client = this.client; - const thread = client.channels.cache.get(data.id); - - if (thread) { - client.channels._remove(thread.id); - - /** - * Emitted whenever a thread is deleted. - * @event Client#threadDelete - * @param {ThreadChannel} thread The thread that was deleted - */ - client.emit(Events.ThreadDelete, thread); - } - - return { thread }; - } -} - -module.exports = ThreadDeleteAction; diff --git a/packages/discord.js/src/client/actions/ThreadListSync.js b/packages/discord.js/src/client/actions/ThreadListSync.js deleted file mode 100644 index b336a8c9d..000000000 --- a/packages/discord.js/src/client/actions/ThreadListSync.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict'; - -const { Collection } = require('@discordjs/collection'); -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class ThreadListSyncAction extends Action { - handle(data) { - const client = this.client; - - const guild = client.guilds.cache.get(data.guild_id); - if (!guild) return {}; - - if (data.channel_ids) { - for (const id of data.channel_ids) { - const channel = client.channels.cache.get(id); - if (channel) this.removeStale(channel); - } - } else { - for (const channel of guild.channels.cache.values()) { - this.removeStale(channel); - } - } - - const syncedThreads = data.threads.reduce((coll, rawThread) => { - const thread = client.channels._add(rawThread); - return coll.set(thread.id, thread); - }, new Collection()); - - for (const rawMember of Object.values(data.members)) { - // Discord sends the thread id as id in this object - const thread = client.channels.cache.get(rawMember.id); - if (thread) { - thread.members._add(rawMember); - } - } - - /** - * Emitted whenever the client user gains access to a text or announcement channel that contains threads - * @event Client#threadListSync - * @param {Collection} threads The threads that were synced - * @param {Guild} guild The guild that the threads were synced in - */ - client.emit(Events.ThreadListSync, syncedThreads, guild); - - return { - syncedThreads, - }; - } - - removeStale(channel) { - channel.threads?.cache.forEach(thread => { - if (!thread.archived) { - this.client.channels._remove(thread.id); - } - }); - } -} - -module.exports = ThreadListSyncAction; diff --git a/packages/discord.js/src/client/actions/ThreadMemberUpdate.js b/packages/discord.js/src/client/actions/ThreadMemberUpdate.js deleted file mode 100644 index 0b17f7055..000000000 --- a/packages/discord.js/src/client/actions/ThreadMemberUpdate.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class ThreadMemberUpdateAction extends Action { - handle(data) { - const client = this.client; - // Discord sends the thread id as id in this object - const thread = client.channels.cache.get(data.id); - if (thread) { - const member = thread.members.cache.get(data.user_id); - if (!member) { - const newMember = thread.members._add(data); - return { newMember }; - } - const old = member._update(data); - /** - * Emitted whenever the client user's thread member is updated. - * @event Client#threadMemberUpdate - * @param {ThreadMember} oldMember The member before the update - * @param {ThreadMember} newMember The member after the update - */ - client.emit(Events.ThreadMemberUpdate, old, member); - } - return {}; - } -} - -module.exports = ThreadMemberUpdateAction; diff --git a/packages/discord.js/src/client/actions/VoiceStateUpdate.js b/packages/discord.js/src/client/actions/VoiceStateUpdate.js deleted file mode 100644 index fc7400f2e..000000000 --- a/packages/discord.js/src/client/actions/VoiceStateUpdate.js +++ /dev/null @@ -1,43 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const VoiceState = require('../../structures/VoiceState'); -const Events = require('../../util/Events'); - -class VoiceStateUpdate extends Action { - handle(data) { - const client = this.client; - const guild = client.guilds.cache.get(data.guild_id); - if (guild) { - // Update the state - const oldState = - guild.voiceStates.cache.get(data.user_id)?._clone() ?? new VoiceState(guild, { user_id: data.user_id }); - - const newState = guild.voiceStates._add(data); - - // Get the member - let member = guild.members.cache.get(data.user_id); - if (member && data.member) { - member._patch(data.member); - } else if (data.member?.user && data.member.joined_at) { - member = guild.members._add(data.member); - } - - // Emit event - if (member?.user.id === client.user.id) { - client.emit('debug', `[VOICE] received voice state update: ${JSON.stringify(data)}`); - client.voice.onVoiceStateUpdate(data); - } - - /** - * Emitted whenever a member changes voice state - e.g. joins/leaves a channel, mutes/unmutes. - * @event Client#voiceStateUpdate - * @param {VoiceState} oldState The voice state before the update - * @param {VoiceState} newState The voice state after the update - */ - client.emit(Events.VoiceStateUpdate, oldState, newState); - } - } -} - -module.exports = VoiceStateUpdate; diff --git a/packages/discord.js/src/client/actions/WebhooksUpdate.js b/packages/discord.js/src/client/actions/WebhooksUpdate.js deleted file mode 100644 index 53dbfc175..000000000 --- a/packages/discord.js/src/client/actions/WebhooksUpdate.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict'; - -const Action = require('./Action'); -const Events = require('../../util/Events'); - -class WebhooksUpdate extends Action { - handle(data) { - const client = this.client; - const channel = client.channels.cache.get(data.channel_id); - if (!channel) return; - - /** - * Emitted whenever a channel has its webhooks changed. - * @event Client#webhooksUpdate - * @param {TextChannel|AnnouncementChannel|VoiceChannel|StageChannel|ForumChannel|MediaChannel} channel - * The channel that had a webhook update - */ - client.emit(Events.WebhooksUpdate, channel); - } -} - -module.exports = WebhooksUpdate; diff --git a/packages/discord.js/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.js index 73d4ec47f..4ae1a6555 100644 --- a/packages/discord.js/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.js @@ -1,5 +1,19 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.ApplicationCommandPermissionsUpdate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + /** + * Emitted whenever permissions for an application command in a guild were updated. + * This includes permission updates for other applications in addition to the logged in client, + * check `data.applicationId` to verify which application the update is for + * @event Client#applicationCommandPermissionsUpdate + * @param {ApplicationCommandPermissionsUpdateData} data The updated permissions + */ + client.emit(Events.ApplicationCommandPermissionsUpdate, { + permissions: data.permissions, + id: data.id, + guildId: data.guild_id, + applicationId: data.application_id, + }); }; diff --git a/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_ACTION_EXECUTION.js b/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_ACTION_EXECUTION.js index 22463b6e1..c7d8dd538 100644 --- a/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_ACTION_EXECUTION.js +++ b/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_ACTION_EXECUTION.js @@ -1,5 +1,17 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.AutoModerationActionExecution.handle(packet.d); +const AutoModerationActionExecution = require('../../../structures/AutoModerationActionExecution.js'); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + /** + * Emitted whenever an auto moderation rule is triggered. + * This event requires the {@link PermissionFlagsBits.ManageGuild} permission. + * @event Client#autoModerationActionExecution + * @param {AutoModerationActionExecution} autoModerationActionExecution The data of the execution + */ + client.emit(Events.AutoModerationActionExecution, new AutoModerationActionExecution(data, guild)); }; diff --git a/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_CREATE.js b/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_CREATE.js index af64b9cbc..239fbf439 100644 --- a/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_CREATE.js +++ b/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_CREATE.js @@ -1,5 +1,18 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.AutoModerationRuleCreate.handle(packet.d); +const Events = require('../../../util/Events'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + const autoModerationRule = guild.autoModerationRules._add(data); + + /** + * Emitted whenever an auto moderation rule is created. + * This event requires the {@link PermissionFlagsBits.ManageGuild} permission. + * @event Client#autoModerationRuleCreate + * @param {AutoModerationRule} autoModerationRule The created auto moderation rule + */ + client.emit(Events.AutoModerationRuleCreate, autoModerationRule); }; diff --git a/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_DELETE.js b/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_DELETE.js index 56ec504a9..ccc318751 100644 --- a/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_DELETE.js +++ b/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_DELETE.js @@ -1,5 +1,21 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.AutoModerationRuleDelete.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + const autoModerationRule = guild.autoModerationRules.cache.get(data.id); + if (!autoModerationRule) return; + + guild.autoModerationRules.cache.delete(autoModerationRule.id); + + /** + * Emitted whenever an auto moderation rule is deleted. + * This event requires the {@link PermissionFlagsBits.ManageGuild} permission. + * @event Client#autoModerationRuleDelete + * @param {AutoModerationRule} autoModerationRule The deleted auto moderation rule + */ + client.emit(Events.AutoModerationRuleDelete, autoModerationRule); }; diff --git a/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_UPDATE.js index 3caf6ba55..23c60a158 100644 --- a/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/AUTO_MODERATION_RULE_UPDATE.js @@ -1,5 +1,20 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.AutoModerationRuleUpdate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + const oldAutoModerationRule = guild.autoModerationRules.cache.get(data.id)?._clone() ?? null; + const newAutoModerationRule = guild.autoModerationRules._add(data); + + /** + * Emitted whenever an auto moderation rule gets updated. + * This event requires the {@link PermissionFlagsBits.ManageGuild} permission. + * @event Client#autoModerationRuleUpdate + * @param {?AutoModerationRule} oldAutoModerationRule The auto moderation rule before the update + * @param {AutoModerationRule} newAutoModerationRule The auto moderation rule after the update + */ + client.emit(Events.AutoModerationRuleUpdate, oldAutoModerationRule, newAutoModerationRule); }; diff --git a/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_CREATE.js b/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_CREATE.js index 08e37b819..fe252ba7b 100644 --- a/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_CREATE.js +++ b/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_CREATE.js @@ -1,5 +1,14 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.EntitlementCreate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const entitlement = client.application.entitlements._add(data); + + /** + * Emitted whenever an entitlement is created. + * @event Client#entitlementCreate + * @param {Entitlement} entitlement The entitlement that was created + */ + client.emit(Events.EntitlementCreate, entitlement); }; diff --git a/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_DELETE.js b/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_DELETE.js index d731d1bb5..46ca48957 100644 --- a/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_DELETE.js +++ b/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_DELETE.js @@ -1,5 +1,18 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.EntitlementDelete.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const entitlement = client.application.entitlements._add(data, false); + + client.application.entitlements.cache.delete(entitlement.id); + + /** + * Emitted whenever an entitlement is deleted. + * Entitlements are not deleted when they expire. + * This is only triggered when Discord issues a refund or deletes the entitlement manually. + * @event Client#entitlementDelete + * @param {Entitlement} entitlement The entitlement that was deleted + */ + client.emit(Events.EntitlementDelete, entitlement); }; diff --git a/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_UPDATE.js index 51534bbe4..b68ec0059 100644 --- a/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/ENTITLEMENT_UPDATE.js @@ -1,5 +1,16 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.EntitlementUpdate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const oldEntitlement = client.application.entitlements.cache.get(data.id)?._clone() ?? null; + const newEntitlement = client.application.entitlements._add(data); + + /** + * Emitted whenever an entitlement is updated - i.e. when a user's subscription renews. + * @event Client#entitlementUpdate + * @param {?Entitlement} oldEntitlement The entitlement before the update + * @param {Entitlement} newEntitlement The entitlement after the update + */ + client.emit(Events.EntitlementUpdate, oldEntitlement, newEntitlement); }; diff --git a/packages/discord.js/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js b/packages/discord.js/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js index 862314166..42958e1c7 100644 --- a/packages/discord.js/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js +++ b/packages/discord.js/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js @@ -1,5 +1,19 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.GuildAuditLogEntryCreate.handle(packet.d); +const GuildAuditLogsEntry = require('../../../structures/GuildAuditLogsEntry.js'); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + const auditLogEntry = new GuildAuditLogsEntry(guild, data); + + /** + * Emitted whenever a guild audit log entry is created. + * @event Client#guildAuditLogEntryCreate + * @param {GuildAuditLogsEntry} auditLogEntry The entry that was created + * @param {Guild} guild The guild where the entry was created + */ + client.emit(Events.GuildAuditLogEntryCreate, auditLogEntry, guild); }; diff --git a/packages/discord.js/src/client/websocket/handlers/GUILD_BAN_ADD.js b/packages/discord.js/src/client/websocket/handlers/GUILD_BAN_ADD.js index d8dc0f9da..e493052c5 100644 --- a/packages/discord.js/src/client/websocket/handlers/GUILD_BAN_ADD.js +++ b/packages/discord.js/src/client/websocket/handlers/GUILD_BAN_ADD.js @@ -1,5 +1,15 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.GuildBanAdd.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + /** + * Emitted whenever a member is banned from a guild. + * @event Client#guildBanAdd + * @param {GuildBan} ban The ban that occurred + */ + client.emit(Events.GuildBanAdd, guild.bans._add(data)); }; diff --git a/packages/discord.js/src/client/websocket/handlers/GUILD_BAN_REMOVE.js b/packages/discord.js/src/client/websocket/handlers/GUILD_BAN_REMOVE.js index 8389e46e8..fa77da326 100644 --- a/packages/discord.js/src/client/websocket/handlers/GUILD_BAN_REMOVE.js +++ b/packages/discord.js/src/client/websocket/handlers/GUILD_BAN_REMOVE.js @@ -1,5 +1,20 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.GuildBanRemove.handle(packet.d); +const GuildBan = require('../../../structures/GuildBan.js'); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + const ban = guild.bans.cache.get(data.user.id) ?? new GuildBan(client, data, guild); + + guild.bans.cache.delete(ban.user.id); + + /** + * Emitted whenever a member is unbanned from a guild. + * @event Client#guildBanRemove + * @param {GuildBan} ban The ban that was removed + */ + client.emit(Events.GuildBanRemove, ban); }; diff --git a/packages/discord.js/src/client/websocket/handlers/GUILD_DELETE.js b/packages/discord.js/src/client/websocket/handlers/GUILD_DELETE.js index 27a325623..d7e7606d9 100644 --- a/packages/discord.js/src/client/websocket/handlers/GUILD_DELETE.js +++ b/packages/discord.js/src/client/websocket/handlers/GUILD_DELETE.js @@ -1,5 +1,35 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.GuildDelete.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.id); + if (!guild) return; + + if (data.unavailable) { + guild.available = false; + + /** + * Emitted whenever a guild becomes unavailable, likely due to a server outage. + * @event Client#guildUnavailable + * @param {Guild} guild The guild that has become unavailable + */ + client.emit(Events.GuildUnavailable, guild); + + // Stops the GuildDelete packet thinking a guild was actually deleted, + // handles emitting of event itself + return; + } + + for (const channel of guild.channels.cache.values()) client.channels._remove(channel.id); + client.voice.adapters.get(data.id)?.destroy(); + + client.guilds.cache.delete(guild.id); + + /** + * Emitted whenever a guild kicks the client or the guild is deleted/left. + * @event Client#guildDelete + * @param {Guild} guild The guild that was deleted + */ + client.emit(Events.GuildDelete, guild); }; diff --git a/packages/discord.js/src/client/websocket/handlers/GUILD_INTEGRATIONS_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/GUILD_INTEGRATIONS_UPDATE.js index e90a72c25..a8b7f4a74 100644 --- a/packages/discord.js/src/client/websocket/handlers/GUILD_INTEGRATIONS_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/GUILD_INTEGRATIONS_UPDATE.js @@ -1,5 +1,15 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.GuildIntegrationsUpdate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + /** + * Emitted whenever a guild integration is updated + * @event Client#guildIntegrationsUpdate + * @param {Guild} guild The guild whose integrations were updated + */ + client.emit(Events.GuildIntegrationsUpdate, guild); }; diff --git a/packages/discord.js/src/client/websocket/handlers/GUILD_ROLE_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/GUILD_ROLE_UPDATE.js index 3a9b62e80..1a90af6ae 100644 --- a/packages/discord.js/src/client/websocket/handlers/GUILD_ROLE_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/GUILD_ROLE_UPDATE.js @@ -1,5 +1,21 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.GuildRoleUpdate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + const role = guild.roles.cache.get(data.role.id); + if (!role) return; + + const old = role._update(data.role); + + /** + * Emitted whenever a guild role is updated. + * @event Client#roleUpdate + * @param {Role} oldRole The role before the update + * @param {Role} newRole The role after the update + */ + client.emit(Events.GuildRoleUpdate, old, role); }; diff --git a/packages/discord.js/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_CREATE.js b/packages/discord.js/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_CREATE.js index 04ff2df82..58812c3f3 100644 --- a/packages/discord.js/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_CREATE.js +++ b/packages/discord.js/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_CREATE.js @@ -1,5 +1,17 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.GuildScheduledEventCreate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + const guildScheduledEvent = guild.scheduledEvents._add(data); + + /** + * Emitted whenever a guild scheduled event is created. + * @event Client#guildScheduledEventCreate + * @param {GuildScheduledEvent} guildScheduledEvent The created guild scheduled event + */ + client.emit(Events.GuildScheduledEventCreate, guildScheduledEvent); }; diff --git a/packages/discord.js/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_UPDATE.js index 006470831..93f6b07af 100644 --- a/packages/discord.js/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_UPDATE.js @@ -1,5 +1,19 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.GuildScheduledEventUpdate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + const oldGuildScheduledEvent = guild.scheduledEvents.cache.get(data.id)?._clone() ?? null; + const newGuildScheduledEvent = guild.scheduledEvents._add(data); + + /** + * Emitted whenever a guild scheduled event gets updated. + * @event Client#guildScheduledEventUpdate + * @param {?GuildScheduledEvent} oldGuildScheduledEvent The guild scheduled event object before the update + * @param {GuildScheduledEvent} newGuildScheduledEvent The guild scheduled event object after the update + */ + client.emit(Events.GuildScheduledEventUpdate, oldGuildScheduledEvent, newGuildScheduledEvent); }; diff --git a/packages/discord.js/src/client/websocket/handlers/INVITE_CREATE.js b/packages/discord.js/src/client/websocket/handlers/INVITE_CREATE.js index 50a2e72dd..7f5429eb5 100644 --- a/packages/discord.js/src/client/websocket/handlers/INVITE_CREATE.js +++ b/packages/discord.js/src/client/websocket/handlers/INVITE_CREATE.js @@ -1,5 +1,20 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.InviteCreate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const channel = client.channels.cache.get(data.channel_id); + const guild = client.guilds.cache.get(data.guild_id); + if (!channel) return; + + const inviteData = Object.assign(data, { channel, guild }); + const invite = guild.invites._add(inviteData); + + /** + * Emitted when an invite is created. + * This event requires the {@link PermissionFlagsBits.ManageChannels} permission for the channel. + * @event Client#inviteCreate + * @param {Invite} invite The invite that was created + */ + client.emit(Events.InviteCreate, invite); }; diff --git a/packages/discord.js/src/client/websocket/handlers/INVITE_DELETE.js b/packages/discord.js/src/client/websocket/handlers/INVITE_DELETE.js index 597185234..5821013cd 100644 --- a/packages/discord.js/src/client/websocket/handlers/INVITE_DELETE.js +++ b/packages/discord.js/src/client/websocket/handlers/INVITE_DELETE.js @@ -1,5 +1,23 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.InviteDelete.handle(packet.d); +const Invite = require('../../../structures/Invite.js'); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const channel = client.channels.cache.get(data.channel_id); + const guild = client.guilds.cache.get(data.guild_id); + if (!channel) return; + + const inviteData = Object.assign(data, { channel, guild }); + const invite = new Invite(client, inviteData); + + guild.invites.cache.delete(invite.code); + + /** + * Emitted when an invite is deleted. + * This event requires the {@link PermissionFlagsBits.ManageChannels} permission for the channel. + * @event Client#inviteDelete + * @param {Invite} invite The invite that was deleted + */ + client.emit(Events.InviteDelete, invite); }; diff --git a/packages/discord.js/src/client/websocket/handlers/PRESENCE_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/PRESENCE_UPDATE.js index bde36297d..15b06d2d1 100644 --- a/packages/discord.js/src/client/websocket/handlers/PRESENCE_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/PRESENCE_UPDATE.js @@ -1,5 +1,43 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.PresenceUpdate.handle(packet.d); +const Events = require('../../../util/Events.js'); +const Partials = require('../../../util/Partials.js'); + +module.exports = (client, { d: data }) => { + let user = client.users.cache.get(data.user.id); + if (!user && ('username' in data.user || client.options.partials.includes(Partials.User))) { + user = client.users._add(data.user); + } + if (!user) return; + + if (data.user.username) { + if (!user._equals(data.user)) client.actions.UserUpdate.handle(data.user); + } + + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + const oldPresence = guild.presences.cache.get(user.id)?._clone() ?? null; + + let member = guild.members.cache.get(user.id); + if (!member && data.status !== 'offline') { + member = guild.members._add({ + user, + deaf: false, + mute: false, + }); + + client.emit(Events.GuildMemberAvailable, member); + } + + const newPresence = guild.presences._add(Object.assign(data, { guild })); + if (client.listenerCount(Events.PresenceUpdate) > 0 && !newPresence.equals(oldPresence)) { + /** + * Emitted whenever a guild member's presence (e.g. status, activity) is changed. + * @event Client#presenceUpdate + * @param {?Presence} oldPresence The presence before the update, if one at all + * @param {Presence} newPresence The presence after the update + */ + client.emit(Events.PresenceUpdate, oldPresence, newPresence); + } }; diff --git a/packages/discord.js/src/client/websocket/handlers/THREAD_DELETE.js b/packages/discord.js/src/client/websocket/handlers/THREAD_DELETE.js index 1140a08e8..5eff37848 100644 --- a/packages/discord.js/src/client/websocket/handlers/THREAD_DELETE.js +++ b/packages/discord.js/src/client/websocket/handlers/THREAD_DELETE.js @@ -1,5 +1,17 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.ThreadDelete.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const thread = client.channels.cache.get(data.id); + if (!thread) return; + + client.channels._remove(thread.id); + + /** + * Emitted whenever a thread is deleted. + * @event Client#threadDelete + * @param {ThreadChannel} thread The thread that was deleted + */ + client.emit(Events.ThreadDelete, thread); }; diff --git a/packages/discord.js/src/client/websocket/handlers/THREAD_LIST_SYNC.js b/packages/discord.js/src/client/websocket/handlers/THREAD_LIST_SYNC.js index 17b173adf..ffc2ff8b7 100644 --- a/packages/discord.js/src/client/websocket/handlers/THREAD_LIST_SYNC.js +++ b/packages/discord.js/src/client/websocket/handlers/THREAD_LIST_SYNC.js @@ -1,5 +1,49 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.ThreadListSync.handle(packet.d); +const { Collection } = require('@discordjs/collection'); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + if (data.channel_ids) { + for (const id of data.channel_ids) { + const channel = client.channels.cache.get(id); + if (channel) removeStaleThreads(client, channel); + } + } else { + for (const channel of guild.channels.cache.values()) { + removeStaleThreads(client, channel); + } + } + + const syncedThreads = data.threads.reduce((coll, rawThread) => { + const thread = client.channels._add(rawThread); + return coll.set(thread.id, thread); + }, new Collection()); + + for (const rawMember of Object.values(data.members)) { + // Discord sends the thread id as id in this object + const thread = client.channels.cache.get(rawMember.id); + if (thread) { + thread.members._add(rawMember); + } + } + + /** + * Emitted whenever the client user gains access to a text or announcement channel that contains threads + * @event Client#threadListSync + * @param {Collection} threads The threads that were synced + * @param {Guild} guild The guild that the threads were synced in + */ + client.emit(Events.ThreadListSync, syncedThreads, guild); }; + +function removeStaleThreads(client, channel) { + channel.threads?.cache.forEach(thread => { + if (!thread.archived) { + client.channels._remove(thread.id); + } + }); +} diff --git a/packages/discord.js/src/client/websocket/handlers/THREAD_MEMBER_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/THREAD_MEMBER_UPDATE.js index a111b0ac6..b96723649 100644 --- a/packages/discord.js/src/client/websocket/handlers/THREAD_MEMBER_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/THREAD_MEMBER_UPDATE.js @@ -1,5 +1,25 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.ThreadMemberUpdate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + // Discord sends the thread id as id in this object + const thread = client.channels.cache.get(data.id); + if (!thread) return; + + const member = thread.members.cache.get(data.user_id); + if (!member) { + thread.members._add(data); + return; + } + + const old = member._update(data); + + /** + * Emitted whenever the client user's thread member is updated. + * @event Client#threadMemberUpdate + * @param {ThreadMember} oldMember The member before the update + * @param {ThreadMember} newMember The member after the update + */ + client.emit(Events.ThreadMemberUpdate, old, member); }; diff --git a/packages/discord.js/src/client/websocket/handlers/VOICE_STATE_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/VOICE_STATE_UPDATE.js index dbff6ea2d..bcd90f33b 100644 --- a/packages/discord.js/src/client/websocket/handlers/VOICE_STATE_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/VOICE_STATE_UPDATE.js @@ -1,5 +1,37 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.VoiceStateUpdate.handle(packet.d); +const VoiceState = require('../../../structures/VoiceState.js'); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const guild = client.guilds.cache.get(data.guild_id); + if (!guild) return; + + // Update the state + const oldState = + guild.voiceStates.cache.get(data.user_id)?._clone() ?? new VoiceState(guild, { user_id: data.user_id }); + + const newState = guild.voiceStates._add(data); + + // Get the member + let member = guild.members.cache.get(data.user_id); + if (member && data.member) { + member._patch(data.member); + } else if (data.member?.user && data.member.joined_at) { + member = guild.members._add(data.member); + } + + // Emit event + if (member?.user.id === client.user.id) { + client.emit('debug', `[VOICE] received voice state update: ${JSON.stringify(data)}`); + client.voice.onVoiceStateUpdate(data); + } + + /** + * Emitted whenever a member changes voice state - e.g. joins/leaves a channel, mutes/unmutes. + * @event Client#voiceStateUpdate + * @param {VoiceState} oldState The voice state before the update + * @param {VoiceState} newState The voice state after the update + */ + client.emit(Events.VoiceStateUpdate, oldState, newState); }; diff --git a/packages/discord.js/src/client/websocket/handlers/WEBHOOKS_UPDATE.js b/packages/discord.js/src/client/websocket/handlers/WEBHOOKS_UPDATE.js index 46cacee0b..9a7b8aa1d 100644 --- a/packages/discord.js/src/client/websocket/handlers/WEBHOOKS_UPDATE.js +++ b/packages/discord.js/src/client/websocket/handlers/WEBHOOKS_UPDATE.js @@ -1,5 +1,16 @@ 'use strict'; -module.exports = (client, packet) => { - client.actions.WebhooksUpdate.handle(packet.d); +const Events = require('../../../util/Events.js'); + +module.exports = (client, { d: data }) => { + const channel = client.channels.cache.get(data.channel_id); + if (!channel) return; + + /** + * Emitted whenever a channel has its webhooks changed. + * @event Client#webhooksUpdate + * @param {TextChannel|AnnouncementChannel|VoiceChannel|StageChannel|ForumChannel|MediaChannel} channel + * The channel that had a webhook update + */ + client.emit(Events.WebhooksUpdate, channel); };