feat(events:member): add guildMemberAdd, guildMemberRemove, guildMemberUpdate

This commit is contained in:
DjDeveloperr 2020-11-15 15:18:18 +05:30
parent 943b946e5b
commit 44303dacb1
7 changed files with 75 additions and 6 deletions

View file

@ -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)
}
}

View file

@ -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)
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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,

View file

@ -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)

View file

@ -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)