feat(events:member): add guildMemberAdd, guildMemberRemove, guildMemberUpdate
This commit is contained in:
parent
943b946e5b
commit
44303dacb1
7 changed files with 75 additions and 6 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
15
src/gateway/handlers/guildMemberAdd.ts
Normal file
15
src/gateway/handlers/guildMemberAdd.ts
Normal 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)
|
||||
}
|
21
src/gateway/handlers/guildMemberRemove.ts
Normal file
21
src/gateway/handlers/guildMemberRemove.ts
Normal 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)
|
||||
}
|
||||
}
|
20
src/gateway/handlers/guildMemberUpdate.ts
Normal file
20
src/gateway/handlers/guildMemberUpdate.ts
Normal 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)
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue