mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
i think cron works
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
import type { Result } from 'ts-results-es'
|
||||
export type VoidResult = Result<void, void>;
|
||||
@@ -22,7 +22,6 @@ export interface Emitter {
|
||||
addListener(eventName: string | symbol, listener: AnyFunction): this;
|
||||
removeListener(eventName: string | symbol, listener: AnyFunction): this;
|
||||
emit(eventName: string | symbol, ...payload: any[]): boolean;
|
||||
on(eventName: string | symbol, listener: AnyFunction): this
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import type { UnpackedDependencies } from '../../types/utility';
|
||||
|
||||
/**
|
||||
* A semi-generic container that provides error handling, emitter, and module store.
|
||||
* For the handler to operate correctly, The only user provided dependency needs to be @sern/client
|
||||
|
||||
@@ -18,8 +18,8 @@ import {
|
||||
import type { Emitter, ErrorHandling, Logging } from './interfaces';
|
||||
import util from 'node:util';
|
||||
import type { PluginResult } from '../types/core-plugin';
|
||||
import type { VoidResult } from './_internal';
|
||||
import { Result } from 'ts-results-es';
|
||||
import { VoidResult } from '../types/utility';
|
||||
/**
|
||||
* if {src} is true, mapTo V, else ignore
|
||||
* @param item
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import type { LogPayload, Logging, ErrorHandling } from '../interfaces';
|
||||
import { AnyFunction } from '../../types/utility';
|
||||
import cron from 'node-cron'
|
||||
import { EventEmitter } from 'events';
|
||||
import type { CronEventCommand, Module } from '../../types/core-modules'
|
||||
import { EventType } from './enums';
|
||||
/**
|
||||
* @internal
|
||||
* @since 2.0.0
|
||||
@@ -41,3 +46,39 @@ export class DefaultLogging implements Logging {
|
||||
console.warn(`WARN: ${this.date().toISOString()} -> ${payload.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
export class Cron extends EventEmitter {
|
||||
tasks: string[] = [];
|
||||
modules: Map<string, CronEventCommand> = new Map();
|
||||
private sanityCheck(eventName: string | symbol) : asserts eventName is string {
|
||||
if(typeof eventName === 'symbol') throw Error("Cron cannot add symbol based listener")
|
||||
if(!cron.validate(eventName)) {
|
||||
throw Error("Invalid cron expression while adding")
|
||||
}
|
||||
}
|
||||
addCronModule(module: Module) {
|
||||
if(module.type !== EventType.Cron) {
|
||||
throw Error("Can only add cron modules");
|
||||
}
|
||||
this.modules.set(module.name!, module as CronEventCommand);
|
||||
}
|
||||
addListener(eventName: string | symbol, listener: AnyFunction): this {
|
||||
this.sanityCheck(eventName);
|
||||
const retrievedModule = this.modules.get(eventName);
|
||||
if(!retrievedModule) throw Error("Adding task: module " +eventName +"was not found");
|
||||
cron.schedule(retrievedModule.pattern, listener, {
|
||||
name: retrievedModule?.name!
|
||||
});
|
||||
return this;
|
||||
}
|
||||
removeListener(eventName: string | symbol, listener: AnyFunction) {
|
||||
this.sanityCheck(eventName);
|
||||
const retrievedModule = this.modules.get(eventName);
|
||||
if(!retrievedModule) throw Error("Removing cron: module " +eventName +"was not found");
|
||||
const task= cron.getTasks().get(retrievedModule.name!)
|
||||
if(!task) throw Error("Finding cron task with"+ retrievedModule.name + " not found");
|
||||
task.stop();
|
||||
super.removeListener(eventName, listener);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,14 +13,11 @@ import {
|
||||
finalize,
|
||||
pipe
|
||||
} from 'rxjs';
|
||||
import {
|
||||
type VoidResult,
|
||||
} from '../core/_internal';
|
||||
import * as Id from '../core/id'
|
||||
import type { Emitter, ErrorHandling, Logging } from '../core/interfaces';
|
||||
import { PayloadType, SernError } from '../core/structures/enums'
|
||||
import { Err, Ok, Result } from 'ts-results-es';
|
||||
import type { Awaitable, UnpackedDependencies } from '../types/utility';
|
||||
import type { Awaitable, UnpackedDependencies, VoidResult } from '../types/utility';
|
||||
import type { ControlPlugin } from '../types/core-plugin';
|
||||
import type { CommandModule, Module, Processed } from '../types/core-modules';
|
||||
import { EventEmitter } from 'node:events';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { EventType, PayloadType, SernError } from '../core/structures/enums';
|
||||
import { eventDispatcher, handleCrash } from './event-utils'
|
||||
import { EventModule, Module, Processed } from '../types/core-modules';
|
||||
import { EventModule, Module } from '../types/core-modules';
|
||||
import * as Files from '../core/module-loading'
|
||||
import type { UnpackedDependencies } from '../types/utility';
|
||||
import { resultPayload } from '../core/functions';
|
||||
|
||||
@@ -51,6 +51,7 @@ export interface ExternalEventCommand extends Module {
|
||||
}
|
||||
export interface CronEventCommand extends Module {
|
||||
name?: string;
|
||||
pattern: string;
|
||||
type: EventType.Cron;
|
||||
execute(...args: unknown[]): Awaitable<unknown>;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ import type {
|
||||
TextCommand,
|
||||
UserSelectCommand,
|
||||
} from './core-modules';
|
||||
import type { Args, Awaitable, Payload, SlashOptions } from './utility';
|
||||
import type { Args, Awaitable, Payload, SlashOptions, VoidResult } from './utility';
|
||||
import type { CommandType, EventType, PluginType } from '../core/structures/enums'
|
||||
import type { Context } from '../core/structures/context'
|
||||
import type {
|
||||
@@ -48,7 +48,6 @@ import type {
|
||||
UserContextMenuCommandInteraction,
|
||||
UserSelectMenuInteraction,
|
||||
} from 'discord.js';
|
||||
import { VoidResult } from '../core/_internal';
|
||||
|
||||
export type PluginResult = Awaitable<VoidResult>;
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import type { CommandInteractionOptionResolver, InteractionReplyOptions, MessageReplyOptions } from 'discord.js';
|
||||
import type { PayloadType } from '../core/structures/enums';
|
||||
import type { Module } from './core-modules';
|
||||
import type { Result } from 'ts-results-es';
|
||||
|
||||
export type Awaitable<T> = PromiseLike<T> | T;
|
||||
|
||||
export type AnyFunction = (...args: never[]) => unknown;
|
||||
export type VoidResult = Result<void, void>;
|
||||
export type AnyFunction = (...args: any[]) => unknown;
|
||||
|
||||
// Thanks to @kelsny
|
||||
type ParseType<T> = {
|
||||
|
||||
Reference in New Issue
Block a user