Skip to content

Commit

Permalink
feat(Client): backport INVITE_CREATE and INVITE_DELETE events (#3728)
Browse files Browse the repository at this point in the history
* Backport INVITE_CREATE and INVITE_DELETE

* Register events to Websocket

* Dont create an Invite if the guild is null

* Null check channel too
  • Loading branch information
monbrey authored and SpaceEEC committed Jan 24, 2020
1 parent 17237c7 commit 40afbc1
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/client/actions/ActionsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
29 changes: 29 additions & 0 deletions src/client/actions/InviteCreate.js
Original file line number Diff line number Diff line change
@@ -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.
* <info> This event only triggers if the client has `MANAGE_GUILD` permissions for the guild,
* or `MANAGE_CHANNEL` permissions for the channel.</info>
* @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;
25 changes: 25 additions & 0 deletions src/client/actions/InviteDelete.js
Original file line number Diff line number Diff line change
@@ -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.
* <info> This event only triggers if the client has `MANAGE_GUILD` permissions for the guild,
* or `MANAGE_CHANNEL` permissions for the channel.</info>
* @event Client#inviteDelete
* @param {Invite} invite The invite that was deleted
*/
client.emit(Events.INVITE_DELETE, invite);
}
}
}

module.exports = InviteDeleteAction;
2 changes: 2 additions & 0 deletions src/client/websocket/packets/WebSocketPacketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
11 changes: 11 additions & 0 deletions src/client/websocket/packets/handlers/InviteCreate.js
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions src/client/websocket/packets/handlers/InviteDelete.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Snowflake, Message>) => void): this;
Expand Down

0 comments on commit 40afbc1

Please sign in to comment.