Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gateway): added presence caching, marking events as complete! #45

Merged
merged 4 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export * from './src/managers/guilds.ts'
export * from './src/managers/guildChannels.ts'
export * from './src/managers/guildEmojis.ts'
export * from './src/managers/members.ts'
export * from './src/managers/messageReactions.ts'
export * from './src/managers/reactionUsers.ts'
export * from './src/managers/messages.ts'
export * from './src/managers/roles.ts'
export * from './src/managers/users.ts'
Expand All @@ -39,6 +41,7 @@ export * from './src/structures/presence.ts'
export * from './src/structures/role.ts'
export * from './src/structures/snowflake.ts'
export * from './src/structures/textChannel.ts'
export * from './src/structures/messageReaction.ts'
export * from './src/structures/user.ts'
export * from './src/structures/webhook.ts'
export * from './src/types/application.ts'
Expand All @@ -64,4 +67,4 @@ export * from './src/utils/buildInfo.ts'
export * from './src/utils/permissions.ts'
export * from './src/utils/userFlags.ts'
export * from './src/utils/bitfield.ts'
export * from './src/utils/getChannelByType.ts'
export * from './src/utils/getChannelByType.ts'
2 changes: 2 additions & 0 deletions src/gateway/handlers/guildCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const guildCreate: GatewayEventHandler = async (

await guild.roles.fromPayload(d.roles)

if (d.presences !== undefined) await guild.presences.fromPayload(d.presences)

if (d.voice_states !== undefined)
await guild.voiceStates.fromPayload(d.voice_states)

Expand Down
1 change: 1 addition & 0 deletions src/gateway/handlers/guildDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const guildDelte: GatewayEventHandler = async (
await guild.members.flush()
await guild.channels.flush()
await guild.roles.flush()
await guild.presences.flush()
await gateway.client.guilds.delete(d.id)

gateway.client.emit('guildDelete', guild)
Expand Down
8 changes: 7 additions & 1 deletion src/gateway/handlers/guildMembersChunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ export const guildMembersChunk: GatewayEventHandler = async (
await guild.members.set(member.user.id, member)
}

// TODO: Cache Presences
if (d.chunk_index === 0) await guild.presences.flush()

if (d.presences !== undefined) {
for (const pres of d.presences) {
await guild.presences.set(pres.user.id, pres)
}
}

gateway.client.emit('guildMembersChunk', guild, {
members: d.members.map((m) => m.user.id),
Expand Down
10 changes: 10 additions & 0 deletions src/gateway/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ import { guildMembersChunk } from './guildMembersChunk.ts'
import { presenceUpdate } from './presenceUpdate.ts'
import { inviteCreate } from './inviteCreate.ts'
import { inviteDelete } from './inviteDelete.ts'
import { MessageReaction } from '../../structures/messageReaction.ts'
import { Invite } from '../../structures/invite.ts'
import { Presence } from '../../structures/presence.ts'

export const gatewayHandlers: {
[eventCode in GatewayEvents]: GatewayEventHandler | undefined
Expand Down Expand Up @@ -131,16 +134,23 @@ export interface ClientEvents extends EventTypes {
uncached: Set<string>
) => void
messageUpdate: (before: Message, after: Message) => void
messageReactionAdd: (reaction: MessageReaction, user: User) => void
messageReactionRemove: (reaction: MessageReaction, user: User) => void
messageReactionRemoveAll: (message: Message) => void
messageReactionRemoveEmoji: (message: Message, emoji: Emoji) => void
typingStart: (
user: User,
channel: TextChannel,
at: Date,
guildData?: TypingStartGuildData
) => void
inviteCreate: (invite: Invite) => void
inviteDelete: (invite: Invite) => void
userUpdate: (before: User, after: User) => void
voiceServerUpdate: (data: VoiceServerUpdateData) => void
voiceStateAdd: (state: VoiceState) => void
voiceStateRemove: (state: VoiceState) => void
voiceStateUpdate: (state: VoiceState, after: VoiceState) => void
presenceUpdate: (presence: Presence) => void
webhooksUpdate: (guild: Guild, channel: GuildTextChannel) => void
}
3 changes: 2 additions & 1 deletion src/gateway/handlers/inviteCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Gateway, GatewayEventHandler } from '../index.ts'
import { Guild } from '../../structures/guild.ts'
import { InviteCreatePayload } from '../../types/gateway.ts'
import { ChannelPayload, GuildPayload, InvitePayload } from '../../../mod.ts'
import { Invite } from '../../structures/invite.ts'

export const inviteCreate: GatewayEventHandler = async (
gateway: Gateway,
Expand Down Expand Up @@ -36,5 +37,5 @@ export const inviteCreate: GatewayEventHandler = async (

await guild.invites.set(d.code, dataConverted)
const invite = await guild.invites.get(d.code)
gateway.client.emit('inviteCreate', invite)
gateway.client.emit('inviteCreate', (invite as unknown) as Invite)
}
14 changes: 12 additions & 2 deletions src/gateway/handlers/presenceUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { PresenceUpdatePayload } from '../../types/gateway.ts'
import { Gateway, GatewayEventHandler } from '../index.ts'

export const presenceUpdate: GatewayEventHandler = async (
gateway: Gateway,
d: any
) => {}
d: PresenceUpdatePayload
) => {
const guild = await gateway.client.guilds.get(d.guild_id)
if (guild === undefined) return

await guild.presences.set(d.user.id, d)
const presence = await guild.presences.get(d.user.id)
if (presence === undefined) return

gateway.client.emit('presenceUpdate', presence)
}
39 changes: 39 additions & 0 deletions src/managers/presences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Client } from '../models/client.ts'
import { Guild } from '../structures/guild.ts'
import { Presence } from '../structures/presence.ts'
import { User } from '../structures/user.ts'
import { PresenceUpdatePayload } from '../types/gateway.ts'
import { BaseManager } from './base.ts'

export class GuildPresencesManager extends BaseManager<
PresenceUpdatePayload,
Presence
> {
guild: Guild

constructor(client: Client, guild: Guild) {
super(client, `presences:${guild.id}`, Presence)
this.guild = guild
}

async get(id: string): Promise<Presence | undefined> {
const raw = await this._get(id)
if (raw === undefined) return
let user = await this.client.users.get(raw.user.id)
if (user === undefined) user = new User(this.client, raw.user)
const guild = await this.client.guilds.get(raw.guild_id)
if (guild === undefined) return
const presence = new Presence(this.client, raw, user, guild)
return presence
}

async fromPayload(
data: PresenceUpdatePayload[]
): Promise<GuildPresencesManager> {
await this.flush()
for (const pres of data) {
await this.set(pres.user.id, pres)
}
return this
}
}
7 changes: 3 additions & 4 deletions src/structures/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
IntegrationAccountPayload,
IntegrationExpireBehavior,
} from '../types/guild.ts'
import { PresenceUpdatePayload } from '../types/gateway.ts'
import { Base } from './base.ts'
import { RolesManager } from '../managers/roles.ts'
import { InviteManager } from '../managers/invites.ts'
Expand All @@ -21,6 +20,7 @@ import { Application } from './application.ts'
import { GUILD_BAN, GUILD_BANS, GUILD_INTEGRATIONS } from '../types/endpoint.ts'
import { GuildVoiceStatesManager } from '../managers/guildVoiceStates.ts'
import { RequestMembersOptions } from '../gateway/index.ts'
import { GuildPresencesManager } from '../managers/presences.ts'

export class GuildBan extends Base {
guild: Guild
Expand Down Expand Up @@ -147,7 +147,7 @@ export class Guild extends Base {
voiceStates: GuildVoiceStatesManager
members: MembersManager
channels: GuildChannelsManager
presences?: PresenceUpdatePayload[]
presences: GuildPresencesManager
maxPresences?: number
maxMembers?: number
vanityURLCode?: string
Expand All @@ -169,6 +169,7 @@ export class Guild extends Base {
this.unavailable = data.unavailable
this.members = new MembersManager(this.client, this)
this.voiceStates = new GuildVoiceStatesManager(client, this)
this.presences = new GuildPresencesManager(client, this)
this.channels = new GuildChannelsManager(
this.client,
this.client.channels,
Expand Down Expand Up @@ -203,7 +204,6 @@ export class Guild extends Base {
this.joinedAt = data.joined_at
this.large = data.large
this.memberCount = data.member_count
this.presences = data.presences
this.maxPresences = data.max_presences
this.maxMembers = data.max_members
this.vanityURLCode = data.vanity_url_code
Expand Down Expand Up @@ -252,7 +252,6 @@ export class Guild extends Base {
this.joinedAt = data.joined_at ?? this.joinedAt
this.large = data.large ?? this.large
this.memberCount = data.member_count ?? this.memberCount
this.presences = data.presences ?? this.presences
this.maxPresences = data.max_presences ?? this.maxPresences
this.maxMembers = data.max_members ?? this.maxMembers
this.vanityURLCode = data.vanity_url_code ?? this.vanityURLCode
Expand Down
94 changes: 47 additions & 47 deletions src/structures/invite.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import { Client } from '../models/client.ts'
import { ChannelPayload } from '../types/channel.ts'
import { GuildPayload } from '../types/guild.ts'
import { InvitePayload } from '../types/invite.ts'
import { UserPayload } from '../types/user.ts'
import { Base } from './base.ts'

export class Invite extends Base {
code: string
guild?: GuildPayload
channel: ChannelPayload
inviter?: UserPayload
targetUser?: UserPayload
targetUserType?: number
approximatePresenceCount?: number
approximateMemberCount?: number

get link (): string {
return `https://discord.gg/${this.code}`
}

constructor (client: Client, data: InvitePayload) {
super(client)
this.code = data.code
this.guild = data.guild
this.channel = data.channel
this.inviter = data.inviter
this.targetUser = data.target_user
this.targetUserType = data.target_user_type
this.approximateMemberCount = data.approximate_member_count
this.approximatePresenceCount = data.approximate_presence_count
}

protected readFromData (data: InvitePayload): void {
super.readFromData(data)
this.code = data.code ?? this.code
this.guild = data.guild ?? this.guild
this.channel = data.channel ?? this.channel
this.inviter = data.inviter ?? this.inviter
this.targetUser = data.target_user ?? this.targetUser
this.targetUserType = data.target_user_type ?? this.targetUserType
this.approximateMemberCount =
data.approximate_member_count ?? this.approximateMemberCount
this.approximatePresenceCount =
data.approximate_presence_count ?? this.approximatePresenceCount
}
}
import { Client } from '../models/client.ts'
import { ChannelPayload } from '../types/channel.ts'
import { GuildPayload } from '../types/guild.ts'
import { InvitePayload } from '../types/invite.ts'
import { UserPayload } from '../types/user.ts'
import { Base } from './base.ts'
export class Invite extends Base {
code: string
guild?: GuildPayload
channel: ChannelPayload
inviter?: UserPayload
targetUser?: UserPayload
targetUserType?: number
approximatePresenceCount?: number
approximateMemberCount?: number
get link(): string {
return `https://discord.gg/${this.code}`
}
constructor(client: Client, data: InvitePayload) {
super(client)
this.code = data.code
this.guild = data.guild
this.channel = data.channel
this.inviter = data.inviter
this.targetUser = data.target_user
this.targetUserType = data.target_user_type
this.approximateMemberCount = data.approximate_member_count
this.approximatePresenceCount = data.approximate_presence_count
}
protected readFromData(data: InvitePayload): void {
super.readFromData(data)
this.code = data.code ?? this.code
this.guild = data.guild ?? this.guild
this.channel = data.channel ?? this.channel
this.inviter = data.inviter ?? this.inviter
this.targetUser = data.target_user ?? this.targetUser
this.targetUserType = data.target_user_type ?? this.targetUserType
this.approximateMemberCount =
data.approximate_member_count ?? this.approximateMemberCount
this.approximatePresenceCount =
data.approximate_presence_count ?? this.approximatePresenceCount
}
}
Loading