7 Commits

Author SHA1 Message Date
renovate[bot]
992619f8e5 chore(deps): update dependency @typescript-eslint/parser to v5.36.2 (#131)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: xxDeveloper <77380166+Murtatrxx@users.noreply.github.com>
2022-09-11 14:24:50 +03:00
renovate[bot]
b995560ec6 chore(deps): update dependency @typescript-eslint/eslint-plugin to v5.36.2 (#130)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: xxDeveloper <77380166+Murtatrxx@users.noreply.github.com>
2022-09-11 14:19:40 +03:00
renovate[bot]
f01ef9b86c chore(deps): lock file maintenance (#129)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2022-09-11 14:16:19 +03:00
xxDeveloper
702c5750fc docs: Fix the code example (#128) 2022-09-03 14:52:26 +03:00
renovate[bot]
d5d1b4129b chore(deps): update dependency @typescript-eslint/eslint-plugin to v5.36.1 (#126)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2022-09-03 11:26:55 +03:00
renovate[bot]
7658d3e3ab chore(deps): update dependency @typescript-eslint/parser to v5.36.1 (#127)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2022-09-03 11:24:09 +03:00
renovate[bot]
9c1abc6b2e chore(deps): lock file maintenance (#118) 2022-08-29 14:07:45 +03:00
27 changed files with 1259 additions and 459 deletions

View File

@@ -6,8 +6,6 @@
"@typescript-eslint/no-non-null-assertion": "off",
"quotes": [2, "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
"semi": ["error", "always"],
"@typescript-eslint/no-empty-interface": 0,
"@typescript-eslint/ban-types" : 0
"@typescript-eslint/no-empty-interface": 0
}
}

View File

@@ -71,7 +71,7 @@ client.login(token);
#### ` ping.js (CommonJS)`
```js
const { CommandType } = require('@sern/handler');
const { CommandType, commandModule } = require('@sern/handler');
exports.default = commandModule({
name: 'ping',

1408
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -17,8 +17,7 @@
"lint": "eslint src/**/*.ts",
"format": "eslint src/**/*.ts --fix",
"build": "tsup && node scripts/mkjson.mjs dist/cjs dist/esm && tsup --dts-only --outDir dist",
"publish": "npm run build && npm publish",
"pretty": "prettier --write ."
"publish": "npm run build && npm publish"
},
"keywords": [
"sern-handler",
@@ -32,14 +31,13 @@
"author": "SernDevs",
"license": "MIT",
"dependencies": {
"iti": "^0.4.2",
"rxjs": "^7.5.6",
"ts-pattern": "^4.0.2",
"ts-results-es": "^3.5.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "5.35.1",
"@typescript-eslint/parser": "5.35.1",
"@typescript-eslint/eslint-plugin": "5.36.1",
"@typescript-eslint/parser": "5.36.2",
"eslint": "8.22.0",
"prettier": "2.7.1",
"tsup": "^6.1.3",

View File

@@ -1,13 +0,0 @@
import type { SernEventsMapping } from '../../types/handler';
import type { ScopedPlugin } from '../../types/handler';
export interface Logging extends ScopedPlugin {
error(...payload: SernEventsMapping['error']): void;
warning(...payload: SernEventsMapping['warning']): void;
moduleActivate(...warning: SernEventsMapping['module.activate']): void;
moduleRegister(...register: SernEventsMapping['module.register']): void;
}
export interface LoggingConstructor {
new (): Logging;
}

View File

@@ -1,15 +0,0 @@
import type {
CommandModuleDefs,
} from '../../types/module';
import type { CommandType } from '../structures/enums';
import type { CommandModule } from '../../types/module';
import type { ModuleStore } from '../structures/moduleStore';
import type { ScopedPlugin } from '../../types/handler';
export interface ModuleManager extends ScopedPlugin {
readonly moduleStore : ModuleStore;
getModule<T extends CommandType>(type: T, name: string) : CommandModuleDefs[T] | undefined
setModule<T extends CommandType>(type : T, value: Required<CommandModule>) : void
}

View File

@@ -1,3 +1,12 @@
import type {
BothCommand,
ButtonCommand,
ContextMenuMsg,
ContextMenuUser,
ModalSubmitCommand,
SelectMenuCommand,
SlashCommand,
} from '../structures/module';
import Context from '../structures/context';
import type { SlashOptions } from '../../types/handler';
import { asyncResolveArray } from '../utilities/asyncResolveArray';
@@ -12,20 +21,12 @@ import type {
UserContextMenuCommandInteraction,
MessageContextMenuCommandInteraction,
} from 'discord.js';
import { isAutocomplete } from '../utilities/predicates';
import { SernError } from '../structures/errors';
import treeSearch from '../utilities/treeSearch';
import type {
BothCommand,
ButtonCommand,
ContextMenuMsg,
ContextMenuUser,
ModalSubmitCommand,
SelectMenuCommand,
SlashCommand,
} from '../../types/module';
export function applicationCommandDispatcher(interaction: Interaction) {
if (interaction.isAutocomplete()) {
if (isAutocomplete(interaction)) {
return dispatchAutocomplete(interaction);
} else {
const ctx = Context.wrap(interaction as ChatInputCommandInteraction);

View File

@@ -2,7 +2,14 @@ import type { Interaction } from 'discord.js';
import { catchError, concatMap, from, fromEvent, map, Observable } from 'rxjs';
import type Wrapper from '../structures/wrapper';
import { EventsHandler } from './eventsHandler';
import {
isApplicationCommand,
isAutocomplete,
isMessageComponent,
isModalSubmit,
} from '../utilities/predicates';
import * as Files from '../utilities/readFile';
import type { CommandModule } from '../structures/module';
import { SernError } from '../structures/errors';
import { CommandType, PayloadType } from '../structures/enums';
import { match, P } from 'ts-pattern';
@@ -22,7 +29,6 @@ import type {
MessageContextMenuCommandInteraction,
} from 'discord.js';
import { executeModule } from './observableHandling';
import type { CommandModule } from '../../types/module';
export default class InteractionHandler extends EventsHandler<{
event: Interaction;
@@ -54,15 +60,15 @@ export default class InteractionHandler extends EventsHandler<{
override init() {
this.discordEvent.subscribe({
next: event => {
if (event.isMessageComponent()) {
if (isMessageComponent(event)) {
const mod = Files.MessageCompCommands[event.componentType].get(event.customId);
this.setState({ event, mod });
} else if (event.isCommand() || event.isAutocomplete()) {
} else if (isApplicationCommand(event) || isAutocomplete(event)) {
const mod =
Files.ApplicationCommands[event.commandType].get(event.commandName) ??
Files.BothCommands.get(event.commandName);
this.setState({ event, mod });
} else if (event.isModalSubmit()) {
} else if (isModalSubmit(event)) {
/**
* maybe move modal submits into message component object maps?
*/

View File

@@ -6,10 +6,10 @@ import { executeModule, ignoreNonBot, isOneOfCorrectModules } from './observable
import { fmt } from '../utilities/messageHelpers';
import Context from '../structures/context';
import * as Files from '../utilities/readFile';
import type { TextCommand } from '../structures/module';
import { CommandType, PayloadType } from '../structures/enums';
import { asyncResolveArray } from '../utilities/asyncResolveArray';
import { controller } from '../sern';
import type { TextCommand } from '../../types/module';
export default class MessageHandler extends EventsHandler<{
ctx: Context;

View File

@@ -1,11 +1,11 @@
import type { Message } from 'discord.js';
import { concatMap, from, Observable, of, tap, throwError } from 'rxjs';
import { SernError } from '../structures/errors';
import type { Module, CommandModuleDefs, CommandModule } from '../structures/module';
import { Result } from 'ts-results-es';
import type { CommandType } from '../structures/enums';
import type Wrapper from '../structures/wrapper';
import { PayloadType } from '../structures/enums';
import type { CommandModule, CommandModuleDefs, Module } from '../../types/module';
export function ignoreNonBot(prefix: string) {
return (src: Observable<Message>) =>

View File

@@ -1,8 +1,10 @@
import { EventsHandler } from './eventsHandler';
import type Wrapper from '../structures/wrapper';
import { concatMap, fromEvent, Observable, map, take, of, from, toArray, switchMap } from 'rxjs';
import type { CommandModule } from '../structures/module';
import * as Files from '../utilities/readFile';
import { errTap } from './observableHandling';
import type { DefinedCommandModule } from '../../types/handler';
import { basename } from 'path';
import { CommandType, PayloadType, PluginType } from '../structures/enums';
import { processCommandPlugins } from './userDefinedEventsHandling';
@@ -11,8 +13,6 @@ import { SernError } from '../structures/errors';
import { match } from 'ts-pattern';
import { type Result, Err, Ok } from 'ts-results-es';
import { ApplicationCommandType, ComponentType } from 'discord.js';
import type { CommandModule } from '../../types/module';
import type { DefinedCommandModule } from '../../types/handler';
export default class ReadyHandler extends EventsHandler<{
mod: DefinedCommandModule;

View File

@@ -8,13 +8,13 @@ import type {
EventInput,
SpreadParams,
} from '../../types/handler';
import type { EventModule } from '../structures/module';
import { PayloadType } from '../structures/enums';
import type Wrapper from '../structures/wrapper';
import { basename } from 'path';
import { match } from 'ts-pattern';
import { isDiscordEvent, isSernEvent } from '../utilities/predicates';
import { errTap } from './observableHandling';
import type { EventModule } from '../../types/module';
/**
* Utility function to process command plugins for all Modules

View File

@@ -15,7 +15,7 @@ import type { AutocompleteInteraction, Awaitable, Client, ClientEvents } from 'd
import type { Result, Ok, Err } from 'ts-results-es';
import type { CommandType, DefinitelyDefined, Override, SernEventsMapping } from '../../index';
import { EventType, PluginType } from '../../index';
import type { BaseModule, CommandModuleDefs, EventModuleDefs } from '../../types/module';
import type { BaseModule, CommandModuleDefs, EventModuleDefs } from '../structures/module';
import type { EventEmitter } from 'events';
import type {
DiscordEventCommand,

View File

@@ -3,7 +3,8 @@ import { Err, Ok } from 'ts-results-es';
import { ExternalEventEmitters } from './utilities/readFile';
import type { EventEmitter } from 'events';
import { processEvents } from './events/userDefinedEventsHandling';
import { CommandType, EventType, PluginType } from './structures/enums';
import type { CommandModule, EventModule } from './structures/module';
import { EventType, PluginType } from './structures/enums';
import type {
CommandPlugin,
EventModuleCommandPluginDefs,
@@ -16,10 +17,6 @@ import { SernError } from './structures/errors';
import InteractionHandler from './events/interactionHandler';
import ReadyHandler from './events/readyHandler';
import MessageHandler from './events/messageHandler';
import type { CommandModule, EventModule } from '../types/module';
import type { ModuleStore } from './structures/moduleStore';
import type { Client } from 'discord.js';
import type { ModuleConfig, ModuleManagerConstructor } from './contracts/moduleManager';
/**
*
@@ -122,10 +119,3 @@ export function eventModule(mod: InputEventModule): EventModule {
plugins,
} as EventModule;
}
export function ModuleConfiguration<T extends ModuleStore>(
moduleManager : ModuleManagerConstructor<T>,
moduleStore: T,
) : ModuleConfig<T> {
return ( client: Client ) => new moduleManager(client, moduleStore);
}

View File

@@ -1,5 +0,0 @@
import ModuleManager from '../contracts/moduleManager';
import type { CommandModule, CommandModuleDefs } from '../../types/module';
import type { CommandType } from './enums';
import { ModuleStore } from './moduleStore';
import { Logging, LoggingConstructor } from '../contracts/logging';

View File

@@ -1,4 +1,5 @@
import type { Override, SernEventsMapping } from '../../types/handler';
import type { BaseModule } from './module';
import type {
DiscordEmitterPlugin,
DiscordEventPlugin,
@@ -9,7 +10,6 @@ import type {
} from '../plugins/plugin';
import type { Awaitable, ClientEvents } from 'discord.js';
import type { EventType } from './enums';
import type { BaseModule } from '../../types/module';
/*
* Mapped type to generate all sern event modules

View File

@@ -17,21 +17,17 @@ import type {
SelectMenuInteraction,
UserContextMenuCommandInteraction,
} from 'discord.js';
import type {
DiscordEventCommand,
ExternalEventCommand,
SernEventCommand,
} from '../handler/structures/events';
import { CommandType } from '../handler/structures/enums';
import type { Args, Override, SlashOptions } from './handler';
import type Context from '../handler/structures/context';
import type { AutocompletePlugin, CommandPlugin, EventPlugin } from '../handler/plugins/plugin';
import { EventType } from '../handler/structures/enums';
import type { Args, Override, SlashOptions } from '../../types/handler';
import type { AutocompletePlugin, CommandPlugin, EventPlugin } from '../plugins/plugin';
import type Context from './context';
import { CommandType, EventType, PluginType } from './enums';
import type { DiscordEventCommand, ExternalEventCommand, SernEventCommand } from './events';
export interface BaseModule {
type: CommandType;
type: CommandType | PluginType;
name?: string;
description?: string;
execute: (ctx: Context, args: Args) => Awaitable<void | unknown>;
}
export type TextCommand = Override<
@@ -163,6 +159,9 @@ export type EventModuleDefs = {
[EventType.External]: ExternalEventCommand;
};
//TODO: support deeply nested Autocomplete
// objective: construct union of ApplicationCommandOptionData change any Autocomplete data
// into Sern autocomplete data.
export type SernAutocompleteData = Override<
BaseApplicationCommandOptionsData,
@@ -208,4 +207,4 @@ export type SernOptionsData<U extends ApplicationCommandOptionData = Application
? SernSubCommandData
: U extends ApplicationCommandSubGroupData
? SernSubCommandGroupData
: BaseOptions;
: BaseOptions;

View File

@@ -1,72 +0,0 @@
import type { CommandModule } from '../../types/module';
import { ApplicationCommandOptionType, ApplicationCommandType, ComponentType, InteractionType } from 'discord.js';
import type { EventEmitter } from 'events';
import { CommandType } from './enums.js';
import { match } from 'ts-pattern';
import constFn from '../utilities/constantFunction.js';
/**
* Would be a little overkill for a class just for the event emitters?
*/
export const ExternalEventEmitters = new Map<string, EventEmitter>();
/**
* Storing all command modules
*/
export class ModuleStore {
protected readonly BothCommands = new Map<string, CommandModule>();
protected readonly ApplicationCommands = {
[ApplicationCommandType.User]: new Map<string, CommandModule>(),
[ApplicationCommandType.Message]: new Map<string, CommandModule>(),
[ApplicationCommandType.ChatInput]: new Map<string, CommandModule>(),
};
protected readonly InteractionHandlers = {
[ComponentType.Button]: new Map<string, CommandModule>(),
[ComponentType.SelectMenu]: new Map<string, CommandModule>(),
[InteractionType.ModalSubmit] : new Map<string, CommandModule>()
};
protected readonly TextCommands = {
text: new Map<string, CommandModule>(),
aliases: new Map<string, CommandModule>(),
};
/**
*
* @param appType
* Resolves the current map with an ApplicationCommandType
*/
resolveApplicationCommandMap(appType: ApplicationCommandType) {
return this.ApplicationCommands[appType];
}
/**
*
* @param mapping
* Resolves the handler by given component type, or if the type of interaction is a modal submit
*/
resolveInteractionHandlersMap(mapping: ComponentType.Button | ComponentType.SelectMenu | InteractionType.ModalSubmit) {
return this.InteractionHandlers[mapping];
}
/**
*
* @param type
* Resolves the map with a CommandType
*/
resolveMap<T extends CommandType>(type: T) {
return match(type as CommandType)
.with(CommandType.Text, constFn(this.TextCommands.text))
.with(CommandType.Modal, constFn(this.InteractionHandlers[InteractionType.ModalSubmit]))
.with(CommandType.Button, constFn(this.InteractionHandlers[ComponentType.Button]))
.with(CommandType.MenuSelect, constFn(this.InteractionHandlers[ComponentType.SelectMenu]))
.with(CommandType.Both, constFn(this.BothCommands))
.with(CommandType.MenuMsg, constFn(this.ApplicationCommands[ApplicationCommandType.Message]))
.with(CommandType.MenuUser, constFn(this.ApplicationCommands[ApplicationCommandType.User]))
.with(CommandType.Slash, constFn(this.ApplicationCommands[ApplicationCommandType.ChatInput]))
.otherwise(() => {
throw Error('Cannot find a map under this type');
});
}
}

View File

@@ -1,8 +1,28 @@
import Context from './context';
import type {
BothCommand,
Module,
SlashCommand,
TextCommand,
SernOptionsData,
BaseOptions,
SernAutocompleteData,
SernSubCommandData,
SernSubCommandGroupData,
} from './module';
import type Wrapper from './wrapper';
export * from './enums';
export {
Context,
SlashCommand,
TextCommand,
BothCommand,
Module,
Wrapper,
SernOptionsData,
BaseOptions,
SernAutocompleteData,
SernSubCommandData,
SernSubCommandGroupData,
};

View File

@@ -1,14 +1,12 @@
import type { Client } from 'discord.js';
import type SernEmitter from '../sernEmitter';
import type { EventModule } from '../../types/module';
import type { NodeApi } from 'iti';
import type { EventModule } from './module';
/**
* An object to be passed into Sern#init() function.
* @typedef {object} Wrapper
*/
interface Wrapper {
//@deprecrated Use Wrapper#addDependencies instead.
readonly client: Client;
readonly sernEmitter?: SernEmitter;
readonly defaultPrefix?: string;
@@ -17,7 +15,6 @@ interface Wrapper {
| string
| { mod: EventModule; absPath: string }[]
| (() => { mod: EventModule; absPath: string }[]);
readonly addDependencies: (rootBuilder: NodeApi<{}>) => void
}
export default Wrapper;

View File

@@ -1,3 +0,0 @@
export default <T>(value: T) =>
() =>
value;

View File

@@ -1,9 +1,21 @@
import type { CommandModuleDefs, EventModule, Module } from '../structures/module';
import {
AutocompleteInteraction,
Interaction,
InteractionType,
type ModalSubmitInteraction,
type ButtonInteraction,
type SelectMenuInteraction,
type ChatInputCommandInteraction,
type UserContextMenuCommandInteraction,
type MessageContextMenuCommandInteraction,
} from 'discord.js';
import type {
DiscordEventCommand,
ExternalEventCommand,
SernEventCommand,
} from '../structures/events';
import { EventType } from '../..';
import type { CommandModuleDefs, EventModule, Module } from '../../types/module';
export function correctModuleType<T extends keyof CommandModuleDefs>(
plug: Module | undefined,
@@ -14,6 +26,26 @@ export function correctModuleType<T extends keyof CommandModuleDefs>(
return plug !== undefined && (plug.type & type) !== 0;
}
export function isApplicationCommand(
interaction: Interaction,
): interaction is
| ChatInputCommandInteraction
| UserContextMenuCommandInteraction
| MessageContextMenuCommandInteraction {
return interaction.type === InteractionType.ApplicationCommand;
}
export function isModalSubmit(interaction: Interaction): interaction is ModalSubmitInteraction {
return interaction.type === InteractionType.ModalSubmit;
}
export function isAutocomplete(interaction: Interaction): interaction is AutocompleteInteraction {
return interaction.type === InteractionType.ApplicationCommandAutocomplete;
}
export function isMessageComponent(
interaction: Interaction,
): interaction is ButtonInteraction | SelectMenuInteraction {
return interaction.type === InteractionType.MessageComponent;
}
export function isDiscordEvent(el: EventModule): el is DiscordEventCommand {
return el.type === EventType.Discord;
@@ -22,3 +54,17 @@ export function isSernEvent(el: EventModule): el is SernEventCommand {
return el.type === EventType.Sern;
}
export function isExternalEvent(el: EventModule): el is ExternalEventCommand {
return el.type === EventType.External && 'emitter' in el;
}
// export function isEventPlugin<T extends CommandType>(
// e: CommandModulePlugin<T>,
// ): e is EventPlugin<T> {
// return e.type === PluginType.Event;
// }
// export function isCommandPlugin<T extends CommandType>(
// e: CommandModulePlugin<T>,
// ): e is CommandPlugin<T> {
// return !isEventPlugin(e);
// }

View File

@@ -2,10 +2,10 @@ import { ApplicationCommandType, ComponentType } from 'discord.js';
import { readdirSync, statSync } from 'fs';
import { join } from 'path';
import { type Observable, from, concatAll } from 'rxjs';
import type { CommandModule } from '../structures/module';
import { SernError } from '../structures/errors';
import { type Result, Err, Ok } from 'ts-results-es';
import type { EventEmitter } from 'events';
import type { CommandModule } from '../../types/module';
//Maybe move this? this probably doesnt belong in utlities/
export const BothCommands = new Map<string, CommandModule>();

View File

@@ -1,4 +1,4 @@
import type { SernOptionsData } from '../../types/module';
import type { SernOptionsData } from '../structures/module';
import { ApplicationCommandOptionType, AutocompleteInteraction } from 'discord.js';
export default function treeSearch(

View File

@@ -2,7 +2,6 @@ import SernEmitter from './handler/sernEmitter';
export { eventModule, commandModule } from './handler/sern';
export * as Sern from './handler/sern';
export * from './types/handler';
export * from './types/module';
export * from './handler/structures/structxports';
export * from './handler/plugins/plugin';
export { SernEmitter };

View File

@@ -1,9 +1,6 @@
import type { CommandInteractionOptionResolver } from 'discord.js';
import type { CommandModule, EventModule, Module } from '../handler/structures/module';
import type { PayloadType } from '../handler/structures/enums';
import type { CommandModule, EventModule, Module } from './module';
import type { Logging } from '../handler/contracts/logging';
import type { Database } from '../handler/contracts/database';
import type { ModuleManager } from '../handler/contracts/moduleManager';
export type Nullish<T> = T | undefined | null;
// Thanks to @kelsny
@@ -29,6 +26,11 @@ export type EventInput =
| { mod: EventModule; absPath: string }[]
| (() => { mod: EventModule; absPath: string }[]);
export type Reconstruct<T> = T extends Omit<infer O, never> ? O & Reconstruct<O> : T;
export type IsOptional<T> = {
[K in keyof T]-?: T[K] extends Required<T>[K] ? false : true;
};
/**
* Turns a function with a union of array of args into a single union
@@ -42,6 +44,7 @@ export type SpreadParams<T extends (...args: never) => unknown> = (
* After modules are transformed, name and description are given default values if none
* are provided to Module. This type represents that transformation
*/
export type DefinedModule = DefinitelyDefined<Module, 'name' | 'description'>;
export type DefinedCommandModule = DefinitelyDefined<CommandModule, 'name' | 'description'>;
export type DefinedEventModule = DefinitelyDefined<EventModule, 'name' | 'description'>;
export type Payload =
@@ -53,15 +56,3 @@ export type SernEventsMapping = {
['error']: [Payload];
['warning']: [string];
};
export interface ScopedPlugin {
init (dependencies: Map<string, ScopedPlugin>) : void
}
type DependencyMap = {
logger : Logging;
db : Database;
//errorHandling: '';
moduleManager : ModuleManager;
}

View File

@@ -4,7 +4,6 @@
"strict": true,
"esModuleInterop": true,
"noImplicitAny": true,
"experimentalDecorators": true,
"strictNullChecks": true,
"importsNotUsedAsValues": "error",
"moduleResolution": "node",