✨ Rate Limit model
This commit is contained in:
		
							parent
							
								
									15de8b3563
								
							
						
					
					
						commit
						229e741bfd
					
				
							
								
								
									
										26
									
								
								src/models/RateLimit.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/models/RateLimit.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | ||||
| import { Schema, Document, Types } from "mongoose"; | ||||
| import db from "../util/Database"; | ||||
| import { ChannelModel } from "./Channel"; | ||||
| import { UserModel } from "./User"; | ||||
| import { GuildModel } from "./Guild"; | ||||
| 
 | ||||
| export interface Bucket { | ||||
| 	id: "global" | string; // channel_239842397 | guild_238927349823 | webhook_238923423498
 | ||||
| 	user: string; | ||||
| 	hits: number; | ||||
| 	blocked: boolean; | ||||
| } | ||||
| 
 | ||||
| export interface BucketDocument extends Bucket, Document { | ||||
| 	id: string; | ||||
| } | ||||
| 
 | ||||
| export const BucketSchema = new Schema({ | ||||
| 	id: { type: String, required: true }, | ||||
| 	user_id: { type: String, required: true }, // bot, user, oauth_application, webhook
 | ||||
| 	hits: { type: Number, required: true }, // Number of times the user hit this bucket
 | ||||
| 	blocked: { type: Boolean, required: true }, | ||||
| }); | ||||
| 
 | ||||
| // @ts-ignore
 | ||||
| export const BucketModel = db.model<BucketDocument>("Bucket", BucketSchema, "ratelimits"); | ||||
| @ -46,6 +46,7 @@ export interface MongooseCache { | ||||
| export class MongooseCache extends EventEmitter { | ||||
| 	public stream: ChangeStream; | ||||
| 	public data: any; | ||||
| 	public initalizing?: Promise<void>; | ||||
| 
 | ||||
| 	constructor( | ||||
| 		public collection: Collection, | ||||
| @ -59,19 +60,24 @@ export class MongooseCache extends EventEmitter { | ||||
| 		if (this.opts.array == null) this.opts.array = true; | ||||
| 	} | ||||
| 
 | ||||
| 	init = async () => { | ||||
| 		// @ts-ignore
 | ||||
| 		this.stream = this.collection.watch(this.pipeline, { fullDocument: "updateLookup" }); | ||||
| 	init = () => { | ||||
| 		if (this.initalizing) return this.initalizing; | ||||
| 		this.initalizing = new Promise(async (resolve, reject) => { | ||||
| 			// @ts-ignore
 | ||||
| 			this.stream = this.collection.watch(this.pipeline, { fullDocument: "updateLookup" }); | ||||
| 
 | ||||
| 		this.stream.on("change", this.change); | ||||
| 		this.stream.on("close", this.destroy); | ||||
| 		this.stream.on("error", console.error); | ||||
| 			this.stream.on("change", this.change); | ||||
| 			this.stream.on("close", this.destroy); | ||||
| 			this.stream.on("error", console.error); | ||||
| 
 | ||||
| 		if (!this.opts.onlyEvents) { | ||||
| 			const arr = await this.collection.aggregate(this.pipeline).toArray(); | ||||
| 			if (this.opts.array) this.data = arr || []; | ||||
| 			else this.data = arr?.[0]; | ||||
| 		} | ||||
| 			if (!this.opts.onlyEvents) { | ||||
| 				const arr = await this.collection.aggregate(this.pipeline).toArray(); | ||||
| 				if (this.opts.array) this.data = arr || []; | ||||
| 				else this.data = arr?.[0]; | ||||
| 			} | ||||
| 			resolve(); | ||||
| 		}); | ||||
| 		return this.initalizing; | ||||
| 	}; | ||||
| 
 | ||||
| 	changeStream = (pipeline: any) => { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Flam3rboy
						Flam3rboy