From 88876eb8c0a789ce04ae7d179e45d2da49b96ecd Mon Sep 17 00:00:00 2001 From: EvolutionX Date: Fri, 5 Aug 2022 09:16:54 +0530 Subject: [PATCH] style: pretty please --- .github/workflows/continuous-delivery.yml | 2 +- .github/workflows/continuous-integration.yml | 2 +- TypeScript/cooldown.ts | 322 ++++++++++--------- 3 files changed, 164 insertions(+), 162 deletions(-) diff --git a/.github/workflows/continuous-delivery.yml b/.github/workflows/continuous-delivery.yml index fa1ff67..e6a2d4d 100644 --- a/.github/workflows/continuous-delivery.yml +++ b/.github/workflows/continuous-delivery.yml @@ -23,7 +23,7 @@ jobs: run: npm i -g prettier && npm i - name: Run Prettier - run: prettier --check . + run: prettier --write . - name: Convert TypeScript to JavaScript run: npx babel TypeScript --out-dir JavaScript --extensions ".ts" diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 21da6ad..1af9355 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -29,4 +29,4 @@ jobs: run: npm i -g prettier && npm i - name: Run Prettier - run: prettier --check . \ No newline at end of file + run: prettier --write . \ No newline at end of file diff --git a/TypeScript/cooldown.ts b/TypeScript/cooldown.ts index e3cb267..42e9f47 100644 --- a/TypeScript/cooldown.ts +++ b/TypeScript/cooldown.ts @@ -1,160 +1,162 @@ -/** - * @author HighArcs - * @version 1.0.0 - * @description allows you to set cooldowns (or "ratelimits") for commands - * @license null - - * @example - * ```ts - * import { cooldown } from "../plugins/cooldown"; - * import { sernModule, CommandType } from "@sern/handler"; - * export default commandModule({ - * plugins: [cooldown.add( [ ['channel', '1/4'] ] )], // limit to 1 action every 4 seconds per channel - * execute: (ctx) => {} - * }) - * ``` - */ - - import { CommandType, Context, EventPlugin, PluginType } from "@sern/handler"; - import { GuildMember } from "discord.js"; - /** - * actions/seconds - */ - export type CooldownString = `${number}/${number}`; - export interface Cooldown { - location: CooldownLocation; - seconds: number; - actions: number; - } - export enum CooldownLocation { - channel = "channel", - user = "user", - guild = "guild", - } - - export class ExpiryMap extends Map { - public readonly expiry: number; - constructor( - expiry: number = Infinity, - iterable: [K, V][] | ReadonlyMap = [] - ) { - super(iterable); - this.expiry = expiry; - } - - public set(key: K, value: V, expiry: number = this.expiry): this { - super.set(key, value); - if (expiry !== Infinity) setTimeout(() => { - super.delete(key) - }, expiry); - return this; - } - } - - export const map = new ExpiryMap(); - - function parseCooldown( - location: CooldownLocation, - cooldown: CooldownString - ): Cooldown { - const [actions, seconds] = cooldown.split("/").map((s) => Number(s)); - - if ( - !Number.isSafeInteger(actions) || - !Number.isSafeInteger(seconds) || - actions === undefined || - seconds === undefined - ) { - throw new Error(`Invalid cooldown string: ${cooldown}`); - } - - return { - actions, - seconds, - location, - }; - } - - function getPropertyForLocation(context: Context, location: CooldownLocation) { - switch (location) { - case CooldownLocation.channel: - return context.channel!.id; - case CooldownLocation.user: - if (!context.member || !(context.member instanceof GuildMember)) { - throw new Error("context.member is not a GuildMember"); - } - return context.member.id; - case CooldownLocation.guild: - return context.guildId; - } - } - - export interface RecievedCooldown { - location: CooldownLocation; - actions: number; - maxActions: number; - seconds: number; - context: Context; - } - type CooldownResponse = (cooldown: RecievedCooldown) => any; - - function add( - items: Array< - | [CooldownLocation | keyof typeof CooldownLocation, CooldownString] - | Cooldown - >, - message?: CooldownResponse - ): EventPlugin { - const raw = items.map((c) => { - if (!Array.isArray(c)) return c; - return parseCooldown(c[0] as CooldownLocation, c[1]); - }) as Array; - - return { - name: "cooldown", - description: "limits user/channel/guild actions", - type: PluginType.Event, - async execute([context], controller) { - for (const { location, actions, seconds } of raw) { - const id = getPropertyForLocation(context, location); - const cooldown = map.get(id); - - if (!cooldown) { - map.set(id, 1, seconds * 1000); - continue; - } - - if (cooldown >= actions) { - if (message) { - await message({ - location, - actions: cooldown, - maxActions: actions, - seconds, - context, - }); - } - return controller.stop(); - } - - map.set(id, cooldown + 1, seconds * 1000); - } - return controller.next(); - }, - }; - } - - type Location = (value: CooldownString) => ReturnType; - - const locations: Record = { - [CooldownLocation.channel]: (value) => - add([[CooldownLocation.channel, value]]), - [CooldownLocation.user]: (value) => add([[CooldownLocation.user, value]]), - [CooldownLocation.guild]: (value) => add([[CooldownLocation.guild, value]]), - }; - - export const cooldown = { - add, - locations, - map, - }; \ No newline at end of file +// @ts-nocheck +/** + * @author HighArcs + * @version 1.0.0 + * @description allows you to set cooldowns (or "ratelimits") for commands + * @license null + + * @example + * ```ts + * import { cooldown } from "../plugins/cooldown"; + * import { sernModule, CommandType } from "@sern/handler"; + * export default commandModule({ + * plugins: [cooldown.add( [ ['channel', '1/4'] ] )], // limit to 1 action every 4 seconds per channel + * execute: (ctx) => {} + * }) + * ``` + */ + +import { CommandType, Context, EventPlugin, PluginType } from "@sern/handler"; +import { GuildMember } from "discord.js"; +/** + * actions/seconds + */ +export type CooldownString = `${number}/${number}`; +export interface Cooldown { + location: CooldownLocation; + seconds: number; + actions: number; +} +export enum CooldownLocation { + channel = "channel", + user = "user", + guild = "guild", +} + +export class ExpiryMap extends Map { + public readonly expiry: number; + constructor( + expiry: number = Infinity, + iterable: [K, V][] | ReadonlyMap = [] + ) { + super(iterable); + this.expiry = expiry; + } + + public set(key: K, value: V, expiry: number = this.expiry): this { + super.set(key, value); + if (expiry !== Infinity) + setTimeout(() => { + super.delete(key); + }, expiry); + return this; + } +} + +export const map = new ExpiryMap(); + +function parseCooldown( + location: CooldownLocation, + cooldown: CooldownString +): Cooldown { + const [actions, seconds] = cooldown.split("/").map((s) => Number(s)); + + if ( + !Number.isSafeInteger(actions) || + !Number.isSafeInteger(seconds) || + actions === undefined || + seconds === undefined + ) { + throw new Error(`Invalid cooldown string: ${cooldown}`); + } + + return { + actions, + seconds, + location, + }; +} + +function getPropertyForLocation(context: Context, location: CooldownLocation) { + switch (location) { + case CooldownLocation.channel: + return context.channel!.id; + case CooldownLocation.user: + if (!context.member || !(context.member instanceof GuildMember)) { + throw new Error("context.member is not a GuildMember"); + } + return context.member.id; + case CooldownLocation.guild: + return context.guildId; + } +} + +export interface RecievedCooldown { + location: CooldownLocation; + actions: number; + maxActions: number; + seconds: number; + context: Context; +} +type CooldownResponse = (cooldown: RecievedCooldown) => any; + +function add( + items: Array< + | [CooldownLocation | keyof typeof CooldownLocation, CooldownString] + | Cooldown + >, + message?: CooldownResponse +): EventPlugin { + const raw = items.map((c) => { + if (!Array.isArray(c)) return c; + return parseCooldown(c[0] as CooldownLocation, c[1]); + }) as Array; + + return { + name: "cooldown", + description: "limits user/channel/guild actions", + type: PluginType.Event, + async execute([context], controller) { + for (const { location, actions, seconds } of raw) { + const id = getPropertyForLocation(context, location); + const cooldown = map.get(id); + + if (!cooldown) { + map.set(id, 1, seconds * 1000); + continue; + } + + if (cooldown >= actions) { + if (message) { + await message({ + location, + actions: cooldown, + maxActions: actions, + seconds, + context, + }); + } + return controller.stop(); + } + + map.set(id, cooldown + 1, seconds * 1000); + } + return controller.next(); + }, + }; +} + +type Location = (value: CooldownString) => ReturnType; + +const locations: Record = { + [CooldownLocation.channel]: (value) => + add([[CooldownLocation.channel, value]]), + [CooldownLocation.user]: (value) => add([[CooldownLocation.user, value]]), + [CooldownLocation.guild]: (value) => add([[CooldownLocation.guild, value]]), +}; + +export const cooldown = { + add, + locations, + map, +};