From 40afbc1d7e9e22760018bef6ea604113864454fc Mon Sep 17 00:00:00 2001 From: Ryan Munro Date: Sat, 25 Jan 2020 02:34:59 +1100 Subject: [PATCH] feat(Client): backport INVITE_CREATE and INVITE_DELETE events (#3728) * Backport INVITE_CREATE and INVITE_DELETE * Register events to Websocket * Dont create an Invite if the guild is null * Null check channel too --- src/client/actions/ActionsManager.js | 2 ++ src/client/actions/InviteCreate.js | 29 +++++++++++++++++++ src/client/actions/InviteDelete.js | 25 ++++++++++++++++ .../packets/WebSocketPacketManager.js | 2 ++ .../packets/handlers/InviteCreate.js | 11 +++++++ .../packets/handlers/InviteDelete.js | 11 +++++++ src/util/Constants.js | 4 +++ typings/index.d.ts | 1 + 8 files changed, 85 insertions(+) create mode 100644 src/client/actions/InviteCreate.js create mode 100644 src/client/actions/InviteDelete.js create mode 100644 src/client/websocket/packets/handlers/InviteCreate.js create mode 100644 src/client/websocket/packets/handlers/InviteDelete.js diff --git a/src/client/actions/ActionsManager.js b/src/client/actions/ActionsManager.js index 8341e7453..2bc466c1f 100644 --- a/src/client/actions/ActionsManager.js +++ b/src/client/actions/ActionsManager.js @@ -20,6 +20,8 @@ class ActionsManager { this.register(require('./GuildRoleCreate')); this.register(require('./GuildRoleDelete')); this.register(require('./GuildRoleUpdate')); + this.register(require('./InviteCreate')); + this.register(require('./InviteDelete')); this.register(require('./UserGet')); this.register(require('./UserUpdate')); this.register(require('./UserNoteUpdate')); diff --git a/src/client/actions/InviteCreate.js b/src/client/actions/InviteCreate.js new file mode 100644 index 000000000..36e8cf10d --- /dev/null +++ b/src/client/actions/InviteCreate.js @@ -0,0 +1,29 @@ +'use strict'; + +const Action = require('./Action'); +const Invite = require('../../structures/Invite'); +const { Events } = require('../../util/Constants'); + +class InviteCreateAction extends Action { + handle(data) { + const client = this.client; + const guild = client.guilds.get(data.guild_id); + const channel = client.channels.get(data.channel_id); + if (guild && channel) { + const inviteData = Object.assign(data, { guild, channel }); + const invite = new Invite(client, inviteData); + /** + * Emitted when an invite is created. + * This event only triggers if the client has `MANAGE_GUILD` permissions for the guild, + * or `MANAGE_CHANNEL` permissions for the channel. + * @event Client#inviteCreate + * @param {Invite} invite The invite that was created + */ + client.emit(Events.INVITE_CREATE, invite); + return { invite }; + } + return { invite: null }; + } +} + +module.exports = InviteCreateAction; diff --git a/src/client/actions/InviteDelete.js b/src/client/actions/InviteDelete.js new file mode 100644 index 000000000..a25eb8808 --- /dev/null +++ b/src/client/actions/InviteDelete.js @@ -0,0 +1,25 @@ +const Action = require('./Action'); +const Invite = require('../../structures/Invite'); +const { Events } = require('../../util/Constants'); + +class InviteDeleteAction extends Action { + handle(data) { + const client = this.client; + const guild = client.guilds.get(data.guild_id); + const channel = client.channels.get(data.channel_id); + if (guild && channel) { + const inviteData = Object.assign(data, { guild, channel }); + const invite = new Invite(client, inviteData); + /** + * Emitted when an invite is deleted. + * This event only triggers if the client has `MANAGE_GUILD` permissions for the guild, + * or `MANAGE_CHANNEL` permissions for the channel. + * @event Client#inviteDelete + * @param {Invite} invite The invite that was deleted + */ + client.emit(Events.INVITE_DELETE, invite); + } + } +} + +module.exports = InviteDeleteAction; diff --git a/src/client/websocket/packets/WebSocketPacketManager.js b/src/client/websocket/packets/WebSocketPacketManager.js index 37be7371e..5ad417b06 100644 --- a/src/client/websocket/packets/WebSocketPacketManager.js +++ b/src/client/websocket/packets/WebSocketPacketManager.js @@ -32,6 +32,8 @@ class WebSocketPacketManager { this.register(Constants.WSEvents.GUILD_EMOJIS_UPDATE, require('./handlers/GuildEmojisUpdate')); this.register(Constants.WSEvents.GUILD_MEMBERS_CHUNK, require('./handlers/GuildMembersChunk')); this.register(Constants.WSEvents.GUILD_INTEGRATIONS_UPDATE, require('./handlers/GuildIntegrationsUpdate')); + this.register(Constants.WSEvents.INVITE_CREATE, require('./handlers/InviteCreate')); + this.register(Constants.WSEvents.INVITE_DELETE, require('./handlers/InviteDelete')); this.register(Constants.WSEvents.CHANNEL_CREATE, require('./handlers/ChannelCreate')); this.register(Constants.WSEvents.CHANNEL_DELETE, require('./handlers/ChannelDelete')); this.register(Constants.WSEvents.CHANNEL_UPDATE, require('./handlers/ChannelUpdate')); diff --git a/src/client/websocket/packets/handlers/InviteCreate.js b/src/client/websocket/packets/handlers/InviteCreate.js new file mode 100644 index 000000000..00efb67d4 --- /dev/null +++ b/src/client/websocket/packets/handlers/InviteCreate.js @@ -0,0 +1,11 @@ +const AbstractHandler = require('./AbstractHandler'); + +class InviteCreateHandler extends AbstractHandler { + handle(packet) { + const client = this.packetManager.client; + const data = packet.d; + client.actions.InviteCreate.handle(data); + } +} + +module.exports = InviteCreateHandler; diff --git a/src/client/websocket/packets/handlers/InviteDelete.js b/src/client/websocket/packets/handlers/InviteDelete.js new file mode 100644 index 000000000..0b8da426d --- /dev/null +++ b/src/client/websocket/packets/handlers/InviteDelete.js @@ -0,0 +1,11 @@ +const AbstractHandler = require('./AbstractHandler'); + +class InviteDeleteHandler extends AbstractHandler { + handle(packet) { + const client = this.packetManager.client; + const data = packet.d; + client.actions.InviteDelete.handle(data); + } +} + +module.exports = InviteDeleteHandler; diff --git a/src/util/Constants.js b/src/util/Constants.js index f23926635..5053792a8 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -337,6 +337,8 @@ exports.Events = { GUILD_EMOJI_UPDATE: 'emojiUpdate', GUILD_BAN_ADD: 'guildBanAdd', GUILD_BAN_REMOVE: 'guildBanRemove', + INVITE_CREATE: 'inviteCreate', + INVITE_DELETE: 'inviteDelete', CHANNEL_CREATE: 'channelCreate', CHANNEL_DELETE: 'channelDelete', CHANNEL_UPDATE: 'channelUpdate', @@ -447,6 +449,8 @@ exports.WSEvents = { GUILD_BAN_ADD: 'GUILD_BAN_ADD', GUILD_BAN_REMOVE: 'GUILD_BAN_REMOVE', GUILD_EMOJIS_UPDATE: 'GUILD_EMOJIS_UPDATE', + INVITE_CREATE: 'INVITE_CREATE', + INVITE_DELETE: 'INVITE_DELETE', CHANNEL_CREATE: 'CHANNEL_CREATE', CHANNEL_DELETE: 'CHANNEL_DELETE', CHANNEL_UPDATE: 'CHANNEL_UPDATE', diff --git a/typings/index.d.ts b/typings/index.d.ts index 44a28cc7f..b1f9c2b1c 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -132,6 +132,7 @@ declare module 'discord.js' { public on(event: 'guildUnavailable', listener: (guild: Guild) => void): this; public on(event: 'guildUpdate', listener: (oldGuild: Guild, newGuild: Guild) => void): this; public on(event: 'guildIntegrationsUpdate', listener: (guild: Guild) => void): this; + public on(event: 'inviteCreate' | 'inviteDelete', listener: (invite: Invite) => void): this; public on(event: 'message', listener: (message: Message) => void): this; public on(event: 'messageDelete', listener: (message: Message) => void): this; public on(event: 'messageDeleteBulk', listener: (messages: Collection) => void): this;