From 44303dacb14f2bc880fb2709ae6eb80981a4bc88 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sun, 15 Nov 2020 15:18:18 +0530 Subject: [PATCH] feat(events:member): add guildMemberAdd, guildMemberRemove, guildMemberUpdate --- src/gateway/handlers/guildBanAdd.ts | 2 +- src/gateway/handlers/guildMemberAdd.ts | 15 +++++++++++++++ src/gateway/handlers/guildMemberRemove.ts | 21 +++++++++++++++++++++ src/gateway/handlers/guildMemberUpdate.ts | 20 ++++++++++++++++++++ src/gateway/handlers/index.ts | 12 ++++++++---- src/gateway/handlers/messageUpdate.ts | 2 +- src/test/cmd.ts | 9 +++++++++ 7 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 src/gateway/handlers/guildMemberAdd.ts create mode 100644 src/gateway/handlers/guildMemberRemove.ts create mode 100644 src/gateway/handlers/guildMemberUpdate.ts diff --git a/src/gateway/handlers/guildBanAdd.ts b/src/gateway/handlers/guildBanAdd.ts index 1f9a687..50a7360 100644 --- a/src/gateway/handlers/guildBanAdd.ts +++ b/src/gateway/handlers/guildBanAdd.ts @@ -11,7 +11,7 @@ export const guildBanAdd: GatewayEventHandler = async ( const user: User = await gateway.client.users.get(d.user.id) ?? new User(gateway.client, d.user) if (guild !== undefined) { - await guild.members.delete(user.id) + // We don't have to delete member, already done with guildMemberRemove event gateway.client.emit('guildBanAdd', guild, user) } } diff --git a/src/gateway/handlers/guildMemberAdd.ts b/src/gateway/handlers/guildMemberAdd.ts new file mode 100644 index 0000000..c499269 --- /dev/null +++ b/src/gateway/handlers/guildMemberAdd.ts @@ -0,0 +1,15 @@ +import { Gateway, GatewayEventHandler } from '../index.ts' +import { Guild } from '../../structures/guild.ts' + +export const guildMemberAdd: GatewayEventHandler = async ( + gateway: Gateway, + d: any +) => { + const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id) + // Weird case, shouldn't happen + if (guild === undefined) return + + await guild.members.set(d.id, d) + const member = await guild.members.get(d.id) + gateway.client.emit('guildMemberAdd', member) +} \ No newline at end of file diff --git a/src/gateway/handlers/guildMemberRemove.ts b/src/gateway/handlers/guildMemberRemove.ts new file mode 100644 index 0000000..9ddb515 --- /dev/null +++ b/src/gateway/handlers/guildMemberRemove.ts @@ -0,0 +1,21 @@ +import { Gateway, GatewayEventHandler } from '../index.ts' +import { Guild } from '../../structures/guild.ts' +import { User } from "../../structures/user.ts" + +export const guildMemberRemove: GatewayEventHandler = async ( + gateway: Gateway, + d: any +) => { + const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id) + // Weird case, shouldn't happen + if (guild === undefined) return + + const member = await guild.members.get(d.id) + await guild.members.delete(d.id) + + if (member !== undefined) gateway.client.emit('guildMemberRemove', member) + else { + const user = new User(gateway.client, d.user) + gateway.client.emit('guildMemberRemoveUncached', user) + } +} \ No newline at end of file diff --git a/src/gateway/handlers/guildMemberUpdate.ts b/src/gateway/handlers/guildMemberUpdate.ts new file mode 100644 index 0000000..2690cb4 --- /dev/null +++ b/src/gateway/handlers/guildMemberUpdate.ts @@ -0,0 +1,20 @@ +import { Gateway, GatewayEventHandler } from '../index.ts' +import { Guild } from '../../structures/guild.ts' + +export const guildMemberUpdate: GatewayEventHandler = async ( + gateway: Gateway, + d: any +) => { + const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id) + // Weird case, shouldn't happen + if (guild === undefined) return + + const member = await guild.members.get(d.id) + await guild.members.set(d.id, d) + const newMember = await guild.members.get(d.id) + + if (member !== undefined) gateway.client.emit('guildMemberRemove', member, newMember) + else { + gateway.client.emit('guildMemberUpdateUncached', newMember) + } +} \ No newline at end of file diff --git a/src/gateway/handlers/index.ts b/src/gateway/handlers/index.ts index 3236c5a..01baf35 100644 --- a/src/gateway/handlers/index.ts +++ b/src/gateway/handlers/index.ts @@ -15,6 +15,10 @@ import { resume } from './resume.ts' import { reconnect } from './reconnect.ts' import { messageDelete } from "./messageDelete.ts" import { messageUpdate } from "./messageUpdate.ts" +import { guildEmojiUpdate } from "./guildEmojiUpdate.ts" +import { guildMemberAdd } from "./guildMemberAdd.ts" +import { guildMemberRemove } from "./guildMemberRemove.ts" +import { guildMemberUpdate } from "./guildMemberUpdate.ts" export const gatewayHandlers: { [eventCode in GatewayEvents]: GatewayEventHandler | undefined @@ -31,11 +35,11 @@ export const gatewayHandlers: { GUILD_UPDATE: guildUpdate, GUILD_BAN_ADD: guildBanAdd, GUILD_BAN_REMOVE: guildBanRemove, - GUILD_EMOJIS_UPDATE: undefined, + GUILD_EMOJIS_UPDATE: guildEmojiUpdate, GUILD_INTEGRATIONS_UPDATE: undefined, - GUILD_MEMBER_ADD: undefined, - GUILD_MEMBER_REMOVE: undefined, - GUILD_MEMBER_UPDATE: undefined, + GUILD_MEMBER_ADD: guildMemberAdd, + GUILD_MEMBER_REMOVE: guildMemberRemove, + GUILD_MEMBER_UPDATE: guildMemberUpdate, GUILD_MEMBERS_CHUNK: undefined, GUILD_ROLE_CREATE: undefined, GUILD_ROLE_UPDATE: undefined, diff --git a/src/gateway/handlers/messageUpdate.ts b/src/gateway/handlers/messageUpdate.ts index a444d43..bebedc8 100644 --- a/src/gateway/handlers/messageUpdate.ts +++ b/src/gateway/handlers/messageUpdate.ts @@ -14,7 +14,7 @@ export const messageUpdate: GatewayEventHandler = async ( channel = (await gateway.client.channels.fetch(d.channel_id)) as TextChannel const message = await channel.messages.get(d.id) - const author = new User(gateway.client, d.author) + const author = message?.author !== undefined ? message.author : new User(gateway.client, d) const newMsg = new Message(gateway.client, d, channel, author) if (message === undefined) { await channel.messages.set(d.id, d) diff --git a/src/test/cmd.ts b/src/test/cmd.ts index c62c7a8..7132b98 100644 --- a/src/test/cmd.ts +++ b/src/test/cmd.ts @@ -2,6 +2,7 @@ import { Command, CommandClient, Intents } from '../../mod.ts' import { GuildChannel } from "../managers/guildChannels.ts" import { CommandContext } from "../models/command.ts" import { Extension } from "../models/extensions.ts" +import { Member } from "../structures/member.ts" import { Message } from "../structures/message.ts" import { MessageDeletePayload } from "../types/gateway.ts" import { TOKEN } from './config.ts' @@ -36,6 +37,14 @@ client.on('messageUpdateUncached', (msg: Message) => { console.log(`Message: ${msg.author.tag}: ${msg.content}`) }) +client.on('guildMemberAdd', (member: Member) => { + console.log(`Member Join: ${member.user.tag}`) +}) + +client.on('guildMemberRemove', (member: Member) => { + console.log(`Member Leave: ${member.user.tag}`) +}) + // client.on('messageCreate', msg => console.log(`${msg.author.tag}: ${msg.content}`)) client.on("commandError", console.error)