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

hope dis works #116

Merged
merged 6 commits into from
Jul 24, 2024
Merged
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
65 changes: 41 additions & 24 deletions plugins/cooldown.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// @ts-nocheck
/**
* @plugin
* Allows you to set cooldowns (or "ratelimits") for commands, limits user/channel/guild actions.
* An extra function cooldown2 is exported if you want your cooldown to be local to the command.
* @author @trueharuu [<@504698587221852172>]
* @version 1.0.0
* @example
* ```ts
* import { cooldown } from "../plugins/cooldown";
* import { cooldown, cooldown2 } from "../plugins/cooldown";
* import { commandModule } from "@sern/handler";
* //IF you want this cooldown to be local to this command:
* const localCooldown = cooldown2()
* export default commandModule({
* plugins: [cooldown.add( [ ['channel', '1/4'] ] )], // limit to 1 action every 4 seconds per channel
* execute: (ctx) => {
* //your code here
* }
* plugins: [cooldown.add([['channel', '1/4']]), // limit to 1 action every 4 seconds per channel
* localCooldown.add([["user", "1/10"]])], // limit to 1 action every 10 seconds, local to command
* execute: (ctx) => { //your code here }
* })
* ```
* @end
Expand Down Expand Up @@ -60,7 +61,6 @@ export class ExpiryMap<K, V> extends Map<K, V> {
}
}

export const map = new ExpiryMap<string, number>();

function parseCooldown(
location: CooldownLocation,
Expand Down Expand Up @@ -107,17 +107,16 @@ export interface RecievedCooldown {
}
type CooldownResponse = (cooldown: RecievedCooldown) => any;

function add(
items: Array<
| [CooldownLocation | keyof typeof CooldownLocation, CooldownString]
| Cooldown
>,
message?: CooldownResponse,
) {
function __add(map : ExpiryMap<string, number>,
items: Array<| [CooldownLocation
| keyof typeof CooldownLocation, CooldownString]
| Cooldown>,
message?: CooldownResponse) {
const raw = items.map((c) => {
if (!Array.isArray(c)) return c;
return parseCooldown(c[0] as CooldownLocation, c[1]);
}) as Array<Cooldown>;

return CommandControlPlugin<CommandType.Both>(async (context, args) => {
for (const { location, actions, seconds } of raw) {
const id = getPropertyForLocation(context, location);
Expand Down Expand Up @@ -147,17 +146,35 @@ function add(
});
}

type Location = (value: CooldownString) => ReturnType<typeof add>;
type Location = (value: CooldownString) => ReturnType<typeof __add>;

const locationsFn = (map: ExpiryMap<string, number>)=> ({
[CooldownLocation.channel]: (value) => __add(map, [[CooldownLocation.channel, value]]),
[CooldownLocation.user]: (value) => __add(map, [[CooldownLocation.user, value]]),
[CooldownLocation.guild]: (value) => __add(map, [[CooldownLocation.guild, value]]),
} as Record<CooldownLocation, Location>);



const locations: Record<CooldownLocation, Location> = {
[CooldownLocation.channel]: (value) =>
add([[CooldownLocation.channel, value]]),
[CooldownLocation.user]: (value) => add([[CooldownLocation.user, value]]),
[CooldownLocation.guild]: (value) => add([[CooldownLocation.guild, value]]),
export const cooldown2 = () => {
const cooldownMap = new ExpiryMap<string, number>();
return {
add:(items: Array<| [CooldownLocation
| keyof typeof CooldownLocation, CooldownString]
| Cooldown>,
message?: CooldownResponse) => __add(cooldownMap, items, message),
locations: locationsFn(cooldownMap),
map: cooldownMap
}
};

const map = new ExpiryMap<string, number>();
export const cooldown = {
add,
locations,
map,
};
map,
locations: locationsFn(map),
add: (items: Array<| [CooldownLocation
| keyof typeof CooldownLocation, CooldownString]
| Cooldown>,
message?: CooldownResponse) => __add(map, items, message)
}