sadbot
Version:
SadBot is a tooling _to thwart_ (someone/"somebot") from accomplishing an action in a given resource, it blocks, throttles and time-restricts the access like a security guard guy when the party is packed or you're not so sober to be welcome.
35 lines (27 loc) • 1.04 kB
text/typescript
import { RedisClientType } from '@node-redis/client'
import { createClient } from 'redis'
export type SadBotBaseParams = {
namespace?: string,
prefix?: string,
redisClient?: RedisClientType<Record<string, never>, Record<string, never>>
}
export abstract class SadBotBase {
redisClient: RedisClientType<Record<string, never>, Record<string, never>>
namespace: string = 'sadbot'
prefix: string = 'sadbot'
constructor (params:SadBotBaseParams = {}) {
this.redisClient = params.redisClient || createClient()
this.namespace = params.namespace || this.namespace
this.prefix = params.prefix || this.prefix
}
connectedRedisClient = async ():Promise<RedisClientType> => {
if (!this.redisClient.isOpen) {
await this.redisClient.connect()
}
return this.redisClient
}
basePrefix = ():string[] => [this.namespace, this.prefix]
basePrefixKey = ():string => this.basePrefix().join(':')
resourcePrefixKey = (resourceId:string) : string =>
[...this.basePrefix(), resourceId].join(':')
}