Add additional caching and fix ready
This commit is contained in:
		
							parent
							
								
									acfeab68e6
								
							
						
					
					
						commit
						7913b68c41
					
				
					 1 changed files with 47 additions and 4 deletions
				
			
		| 
						 | 
					@ -24,12 +24,21 @@ class DiscordClient {
 | 
				
			||||||
				encoding: "json"
 | 
									encoding: "json"
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		/** @type {import("discord-typings").User | null} */
 | 
							/** @type {import("discord-typings").User} */
 | 
				
			||||||
 | 
							// @ts-ignore avoid setting as or null because we know we need to wait for ready anyways
 | 
				
			||||||
		this.user = null
 | 
							this.user = null
 | 
				
			||||||
		/** @type {import("discord-typings").Application | null} */
 | 
							/** @type {import("discord-typings").Application} */
 | 
				
			||||||
 | 
							// @ts-ignore
 | 
				
			||||||
		this.application = null
 | 
							this.application = null
 | 
				
			||||||
		/** @type {Map<string, import("discord-typings").Channel>} */
 | 
							/** @type {Map<string, import("discord-typings").Channel>} */
 | 
				
			||||||
		this.channels = new Map()
 | 
							this.channels = new Map()
 | 
				
			||||||
 | 
							/** @type {Map<string, import("discord-typings").Guild>} */
 | 
				
			||||||
 | 
							this.guilds = new Map()
 | 
				
			||||||
 | 
							/**
 | 
				
			||||||
 | 
							 * @type {Map<string, Array<string>>}
 | 
				
			||||||
 | 
							 * @private
 | 
				
			||||||
 | 
							 */
 | 
				
			||||||
 | 
							this.guildChannelMap = new Map()
 | 
				
			||||||
		this.cloud.on("event", this.onPacket.bind(this))
 | 
							this.cloud.on("event", this.onPacket.bind(this))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -40,22 +49,56 @@ class DiscordClient {
 | 
				
			||||||
	onPacket(message) {
 | 
						onPacket(message) {
 | 
				
			||||||
		if (message.t === "READY") {
 | 
							if (message.t === "READY") {
 | 
				
			||||||
			if (wasReadyBefore) return
 | 
								if (wasReadyBefore) return
 | 
				
			||||||
 | 
								wasReadyBefore = true
 | 
				
			||||||
			/** @type {import("discord-typings").ReadyPayload} */
 | 
								/** @type {import("discord-typings").ReadyPayload} */
 | 
				
			||||||
			const typed = message.d
 | 
								const typed = message.d
 | 
				
			||||||
			this.user = typed.user
 | 
								this.user = typed.user
 | 
				
			||||||
			this.application = typed.application
 | 
								this.application = typed.application
 | 
				
			||||||
			console.log(`Discord logged in as ${this.user.username}#${this.user.discriminator} (${this.user.id})`)
 | 
								console.log(`Discord logged in as ${this.user.username}#${this.user.discriminator} (${this.user.id})`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		} else if (message.t === "GUILD_CREATE") {
 | 
							} else if (message.t === "GUILD_CREATE") {
 | 
				
			||||||
			/** @type {import("discord-typings").Guild} */
 | 
								/** @type {import("discord-typings").Guild} */
 | 
				
			||||||
			const typed = message.d
 | 
								const typed = message.d
 | 
				
			||||||
 | 
								this.guilds.set(typed.id, typed)
 | 
				
			||||||
 | 
								const arr = []
 | 
				
			||||||
 | 
								this.guildChannelMap.set(typed.id, arr)
 | 
				
			||||||
			for (const channel of typed.channels || []) {
 | 
								for (const channel of typed.channels || []) {
 | 
				
			||||||
 | 
									arr.push(channel.id)
 | 
				
			||||||
				this.channels.set(channel.id, channel)
 | 
									this.channels.set(channel.id, channel)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							} else if (message.t === "GUILD_DELETE") {
 | 
				
			||||||
 | 
								/** @type {import("discord-typings").Guild} */
 | 
				
			||||||
 | 
								const typed = message.d
 | 
				
			||||||
 | 
								this.guilds.delete(typed.id)
 | 
				
			||||||
 | 
								const channels = this.guildChannelMap.get(typed.id)
 | 
				
			||||||
 | 
								if (channels) {
 | 
				
			||||||
 | 
									for (const id of channels) this.channels.delete(id)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								this.guildChannelMap.delete(typed.id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		} else if (message.t === "CHANNEL_CREATE" || message.t === "CHANNEL_DELETE") {
 | 
							} else if (message.t === "CHANNEL_CREATE" || message.t === "CHANNEL_DELETE") {
 | 
				
			||||||
			/** @type {import("discord-typings").Channel} */
 | 
								/** @type {import("discord-typings").Channel} */
 | 
				
			||||||
			const typed = message.d
 | 
								const typed = message.d
 | 
				
			||||||
			if (message.t === "CHANNEL_CREATE") this.channels.set(typed.id, typed)
 | 
								if (message.t === "CHANNEL_CREATE") {
 | 
				
			||||||
			else this.channels.delete(typed.id)
 | 
									this.channels.set(typed.id, typed)
 | 
				
			||||||
 | 
									if (typed["guild_id"]) { // obj[prop] notation can be used to access a property without typescript complaining that it doesn't exist on all values something can have
 | 
				
			||||||
 | 
										const channels = this.guildChannelMap.get(typed["guild_id"])
 | 
				
			||||||
 | 
										if (channels && !channels.includes(typed.id)) channels.push(typed.id)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									this.channels.delete(typed.id)
 | 
				
			||||||
 | 
									if (typed["guild_id"]) {
 | 
				
			||||||
 | 
										const channels = this.guildChannelMap.get(typed["guild_id"])
 | 
				
			||||||
 | 
										if (channels) {
 | 
				
			||||||
 | 
											const previous = channels.indexOf(typed.id)
 | 
				
			||||||
 | 
											if (previous !== -1) channels.splice(previous, 1)
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue