mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 09:26:54 +00:00
Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad36875be2 | ||
|
|
50288867a5 | ||
|
|
6b8995d149 | ||
|
|
82bbddac8d | ||
|
|
03936eb2ea | ||
|
|
c4019f7a08 | ||
|
|
992619f8e5 | ||
|
|
b995560ec6 | ||
|
|
f01ef9b86c | ||
|
|
702c5750fc | ||
|
|
d5d1b4129b | ||
|
|
7658d3e3ab | ||
|
|
9c1abc6b2e | ||
|
|
20bc135a7d | ||
|
|
8a373de880 | ||
|
|
395549c173 | ||
|
|
dbc180fa78 | ||
|
|
ff5c161469 | ||
|
|
82637245f3 | ||
|
|
bc3ed918da | ||
|
|
62bfcb1eb7 | ||
|
|
fbf54c9d36 | ||
|
|
b999918b71 | ||
|
|
224ce97fe8 | ||
|
|
f52dd01ca5 | ||
|
|
b8841e926d | ||
|
|
f0f54cb7f2 | ||
|
|
ebe759f5e0 | ||
|
|
58d97e08a5 |
10
.eslintrc
10
.eslintrc
@@ -3,9 +3,9 @@
|
||||
"extends": ["plugin:@typescript-eslint/recommended"],
|
||||
"parserOptions": { "ecmaVersion": "latest", "sourceType": "script" },
|
||||
"rules": {
|
||||
"@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/no-non-null-assertion": "off",
|
||||
"quotes": [2, "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
|
||||
"semi": ["error", "always"],
|
||||
"@typescript-eslint/no-empty-interface": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
2
.github/CODEOWNERS
vendored
2
.github/CODEOWNERS
vendored
@@ -1 +1 @@
|
||||
src/* @jacoobes
|
||||
* @jacoobes
|
||||
|
||||
44
.github/workflows/continuous-integration.yml
vendored
Normal file
44
.github/workflows/continuous-integration.yml
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
name: Continuous Integration
|
||||
|
||||
on:
|
||||
# Trigger the workflow on push or pull request or custom
|
||||
push:
|
||||
branches:
|
||||
main
|
||||
pull_request_target:
|
||||
branches:
|
||||
main
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
Prettier:
|
||||
name: Run Prettier
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Check out Git repository
|
||||
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3
|
||||
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # tag=v3
|
||||
with:
|
||||
node-version: 17
|
||||
|
||||
# Prettier must be in `package.json`
|
||||
- name: Install Node.js dependencies
|
||||
run: npm i -g prettier && npm i
|
||||
|
||||
- name: Run Prettier
|
||||
run: prettier --write .
|
||||
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: peter-evans/create-pull-request@v4
|
||||
with:
|
||||
commit-message: "style: pretty please"
|
||||
branch: prettier
|
||||
delete-branch: true
|
||||
branch-suffix: short-commit-hash
|
||||
title: "style: pretty please"
|
||||
body: "pretty pretty prettier"
|
||||
reviewers: EvolutionX-10
|
||||
@@ -1,2 +1,3 @@
|
||||
.github/
|
||||
*.md
|
||||
*.md
|
||||
dist/
|
||||
12
.prettierrc
12
.prettierrc
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"semi": true,
|
||||
"trailingComma": "all",
|
||||
"singleQuote": true,
|
||||
"printWidth": 100,
|
||||
"tabWidth": 4,
|
||||
"arrowParens": "avoid"
|
||||
"semi": true,
|
||||
"trailingComma": "all",
|
||||
"singleQuote": true,
|
||||
"printWidth": 100,
|
||||
"tabWidth": 4,
|
||||
"arrowParens": "avoid"
|
||||
}
|
||||
|
||||
26
CHANGELOG.md
26
CHANGELOG.md
@@ -1,5 +1,31 @@
|
||||
# Changelog
|
||||
|
||||
## [1.2.0](https://github.com/sern-handler/handler/compare/v1.1.0...v1.2.0) (2022-09-28)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* allow constructable modules ([#133](https://github.com/sern-handler/handler/issues/133)) ([03936eb](https://github.com/sern-handler/handler/commit/03936eb2ea1d1af7cada04d77bb8345d63a5e20f))
|
||||
* classmodules@arcs ([#143](https://github.com/sern-handler/handler/issues/143)) ([5028886](https://github.com/sern-handler/handler/commit/50288867a5b171511941a1be3877d721694e9f77))
|
||||
* update CODEOWNERS ([6b8995d](https://github.com/sern-handler/handler/commit/6b8995d149c857558415a6c151a3f575ec373445))
|
||||
|
||||
|
||||
### Reverts
|
||||
|
||||
* feat of allow constructable modules ([#138](https://github.com/sern-handler/handler/issues/138)) ([82bbdda](https://github.com/sern-handler/handler/commit/82bbddac8d656b60b3a1fb2471ea03ee5224f5c3))
|
||||
|
||||
## [1.1.0](https://github.com/sern-handler/handler/compare/v1.0.0...v1.1.0) (2022-08-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add proper error handling ([#115](https://github.com/sern-handler/handler/issues/115)) ([395549c](https://github.com/sern-handler/handler/commit/395549c173cb62a18205e451bf2cb5579ba9a6e0))
|
||||
|
||||
|
||||
### Miscellaneous Chores
|
||||
|
||||
* release 1.1.0 ([8a373de](https://github.com/sern-handler/handler/commit/8a373de880ff18df85af812adf9f6f6a4f45028d))
|
||||
|
||||
## 1.0.0 (2022-08-15)
|
||||
|
||||
|
||||
|
||||
14
README.md
14
README.md
@@ -1,17 +1,17 @@
|
||||
<div align="center">
|
||||
<img src="https://raw.githubusercontent.com/sern-handler/.github/main/banner.png" width="900px">
|
||||
<img src="https://raw.githubusercontent.com/sern-handler/.github/main/banner.png" width="900px" />
|
||||
</div>
|
||||
|
||||
<h1 align="center">Handlers. Redefined.</h1>
|
||||
<h4 align="center">A customizable, batteries-included, powerful discord.js framework to streamline bot development.</h4>
|
||||
|
||||
<div align="center" style="margin-top: 10px">
|
||||
<img src="https://img.shields.io/badge/open-source-brightgreen">
|
||||
<div align="center" styles="margin-top: 10px">
|
||||
<img src="https://img.shields.io/badge/open-source-brightgreen" />
|
||||
<a href="https://www.npmjs.com/package/@sern/handler"><img src="https://img.shields.io/npm/v/@sern/handler?maxAge=3600" alt="NPM version" /></a>
|
||||
<a href="https://www.npmjs.com/package/@sern/handler"><img src="https://img.shields.io/npm/dt/@sern/handler?maxAge=3600" alt="NPM downloads" /></a>
|
||||
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/license-MIT-brightgreen" alt="License MIT"></a>
|
||||
<a href="https://sern-handler.js.org"><img alt="docs.rs" src="https://img.shields.io/docsrs/docs"></a>
|
||||
<img alt="Lines of code" src="https://img.shields.io/badge/total%20lines-2k-blue">
|
||||
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/license-MIT-brightgreen" alt="License MIT" /></a>
|
||||
<a href="https://sern-handler.js.org"><img alt="docs.rs" src="https://img.shields.io/docsrs/docs" /></a>
|
||||
<img alt="Lines of code" src="https://img.shields.io/badge/total%20lines-2k-blue" />
|
||||
</div>
|
||||
|
||||
|
||||
@@ -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',
|
||||
|
||||
9818
package-lock.json
generated
9818
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
104
package.json
104
package.json
@@ -1,54 +1,54 @@
|
||||
{
|
||||
"name": "@sern/handler",
|
||||
"version": "1.0.0",
|
||||
"description": "A customizable, batteries-included, powerful discord.js framework to automate and streamline bot development.",
|
||||
"main": "dist/cjs/index.cjs",
|
||||
"module": "dist/esm/index.mjs",
|
||||
"types": "dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/esm/index.mjs",
|
||||
"require": "./dist/cjs/index.cjs"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"watch": "tsup --watch --dts",
|
||||
"clean-modules": "rimraf node_modules/ && npm install",
|
||||
"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"
|
||||
},
|
||||
"keywords": [
|
||||
"sern-handler",
|
||||
"sern",
|
||||
"handler",
|
||||
"sern handler",
|
||||
"wrapper",
|
||||
"discord.js",
|
||||
"framework"
|
||||
],
|
||||
"author": "SernDevs",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"rxjs": "^7.5.6",
|
||||
"ts-pattern": "^4.0.2",
|
||||
"ts-results-es": "^3.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "5.32.0",
|
||||
"@typescript-eslint/parser": "5.32.0",
|
||||
"eslint": "8.21.0",
|
||||
"prettier": "2.7.1",
|
||||
"tsup": "^6.1.3",
|
||||
"typescript": "4.7.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"discord.js": "^14.2.x"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/sern-handler/handler.git"
|
||||
},
|
||||
"homepage": "https://sern-handler.js.org"
|
||||
"name": "@sern/handler",
|
||||
"version": "1.2.0",
|
||||
"description": "A customizable, batteries-included, powerful discord.js framework to automate and streamline bot development.",
|
||||
"main": "dist/cjs/index.cjs",
|
||||
"module": "dist/esm/index.mjs",
|
||||
"types": "dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/esm/index.mjs",
|
||||
"require": "./dist/cjs/index.cjs"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"watch": "tsup --watch --dts",
|
||||
"clean-modules": "rimraf node_modules/ && npm install",
|
||||
"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"
|
||||
},
|
||||
"keywords": [
|
||||
"sern-handler",
|
||||
"sern",
|
||||
"handler",
|
||||
"sern handler",
|
||||
"wrapper",
|
||||
"discord.js",
|
||||
"framework"
|
||||
],
|
||||
"author": "SernDevs",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"rxjs": "^7.5.6",
|
||||
"ts-pattern": "^4.0.2",
|
||||
"ts-results-es": "^3.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@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",
|
||||
"typescript": "^4.8.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"discord.js": "^14.5.x"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/sern-handler/handler.git"
|
||||
},
|
||||
"homepage": "https://sern-handler.js.org"
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
{
|
||||
"major": {
|
||||
"dependencyDashboardApproval": true,
|
||||
"reviewers": ["EvolutionX-10", "jacoobes", "Murtatrxx"]
|
||||
},
|
||||
"minor": {
|
||||
"reviewers": ["jacoobes", "Murtatrxx"]
|
||||
},
|
||||
"schedule": ["every weekend"],
|
||||
"lockFileMaintenance": {
|
||||
"enabled": true,
|
||||
"automerge": false
|
||||
}
|
||||
"major": {
|
||||
"dependencyDashboardApproval": true,
|
||||
"reviewers": ["EvolutionX-10", "jacoobes", "Murtatrxx"]
|
||||
},
|
||||
"minor": {
|
||||
"reviewers": ["jacoobes", "Murtatrxx"]
|
||||
},
|
||||
"schedule": ["every weekend"],
|
||||
"lockFileMaintenance": {
|
||||
"enabled": true,
|
||||
"automerge": false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
import { writeFile } from 'fs/promises'
|
||||
import { writeFile } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
// A quick script to regenerate package.jsons for each cjs and esm after tsup cleans distributions
|
||||
const locations = process.argv;
|
||||
locations.shift();
|
||||
locations.shift();
|
||||
for(const loc of locations) {
|
||||
if(loc.endsWith('cjs')) {
|
||||
await writeFile(join(loc, 'package.json'), JSON.stringify({ type : 'commonjs' }))
|
||||
for (const loc of locations) {
|
||||
if (loc.endsWith('cjs')) {
|
||||
await writeFile(join(loc, 'package.json'), JSON.stringify({ type: 'commonjs' }));
|
||||
} else {
|
||||
await writeFile(join(loc, 'package.json'), JSON.stringify({ type : 'module' }))
|
||||
await writeFile(join(loc, 'package.json'), JSON.stringify({ type: 'module' }));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Interaction } from 'discord.js';
|
||||
import { concatMap, from, fromEvent, map, Observable } from 'rxjs';
|
||||
import { catchError, concatMap, from, fromEvent, map, Observable } from 'rxjs';
|
||||
import type Wrapper from '../structures/wrapper';
|
||||
import { EventsHandler } from './eventsHandler';
|
||||
import {
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
import * as Files from '../utilities/readFile';
|
||||
import type { CommandModule } from '../structures/module';
|
||||
import { SernError } from '../structures/errors';
|
||||
import { CommandType } from '../structures/enums';
|
||||
import { CommandType, PayloadType } from '../structures/enums';
|
||||
import { match, P } from 'ts-pattern';
|
||||
import {
|
||||
applicationCommandDispatcher,
|
||||
@@ -49,27 +49,24 @@ export default class InteractionHandler extends EventsHandler<{
|
||||
return from(eventPluginRes).pipe(map(res => ({ mod, res, execute })));
|
||||
}),
|
||||
concatMap(payload => executeModule(wrapper, payload)),
|
||||
)
|
||||
.subscribe({
|
||||
error: err => {
|
||||
catchError((err, caught) => {
|
||||
wrapper.sernEmitter?.emit('error', err);
|
||||
},
|
||||
});
|
||||
return caught;
|
||||
}),
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
override init() {
|
||||
this.discordEvent.subscribe({
|
||||
next: event => {
|
||||
if (isMessageComponent(event)) {
|
||||
const mod = Files.MessageCompCommands[event.componentType].get(
|
||||
event.customId,
|
||||
);
|
||||
const mod = Files.MessageCompCommands[event.componentType].get(event.customId);
|
||||
this.setState({ event, mod });
|
||||
} else if (isApplicationCommand(event) || isAutocomplete(event)) {
|
||||
const mod =
|
||||
Files.ApplicationCommands[event.commandType].get(
|
||||
event.commandName,
|
||||
) ?? Files.BothCommands.get(event.commandName);
|
||||
Files.ApplicationCommands[event.commandType].get(event.commandName) ??
|
||||
Files.BothCommands.get(event.commandName);
|
||||
this.setState({ event, mod });
|
||||
} else if (isModalSubmit(event)) {
|
||||
/**
|
||||
@@ -81,8 +78,8 @@ export default class InteractionHandler extends EventsHandler<{
|
||||
throw Error('This interaction is not supported yet');
|
||||
}
|
||||
},
|
||||
error: e => {
|
||||
this.wrapper.sernEmitter?.emit('error', e);
|
||||
error: reason => {
|
||||
this.wrapper.sernEmitter?.emit('error', { type: PayloadType.Failure, reason });
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -106,10 +103,7 @@ export default class InteractionHandler extends EventsHandler<{
|
||||
{ type: CommandType.Modal },
|
||||
modalCommandDispatcher(event as ModalSubmitInteraction),
|
||||
)
|
||||
.with(
|
||||
{ type: CommandType.Button },
|
||||
buttonCommandDispatcher(event as ButtonInteraction),
|
||||
)
|
||||
.with({ type: CommandType.Button }, buttonCommandDispatcher(event as ButtonInteraction))
|
||||
.with(
|
||||
{ type: CommandType.MenuSelect },
|
||||
selectMenuCommandDispatcher(event as SelectMenuInteraction),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { EventsHandler } from './eventsHandler';
|
||||
import { concatMap, from, fromEvent, map, Observable, of, switchMap } from 'rxjs';
|
||||
import { catchError, concatMap, from, fromEvent, map, Observable, of, switchMap } from 'rxjs';
|
||||
import type Wrapper from '../structures/wrapper';
|
||||
import type { Message } from 'discord.js';
|
||||
import { executeModule, ignoreNonBot, isOneOfCorrectModules } from './observableHandling';
|
||||
@@ -7,7 +7,7 @@ import { fmt } from '../utilities/messageHelpers';
|
||||
import Context from '../structures/context';
|
||||
import * as Files from '../utilities/readFile';
|
||||
import type { TextCommand } from '../structures/module';
|
||||
import { CommandType } from '../structures/enums';
|
||||
import { CommandType, PayloadType } from '../structures/enums';
|
||||
import { asyncResolveArray } from '../utilities/asyncResolveArray';
|
||||
import { controller } from '../sern';
|
||||
|
||||
@@ -36,12 +36,12 @@ export default class MessageHandler extends EventsHandler<{
|
||||
return from(res).pipe(map(res => ({ mod, execute, res })));
|
||||
}),
|
||||
concatMap(payload => executeModule(wrapper, payload)),
|
||||
)
|
||||
.subscribe({
|
||||
error: err => {
|
||||
catchError((err, caught) => {
|
||||
wrapper.sernEmitter?.emit('error', err);
|
||||
},
|
||||
});
|
||||
return caught;
|
||||
}),
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
protected init(): void {
|
||||
@@ -61,16 +61,17 @@ export default class MessageHandler extends EventsHandler<{
|
||||
Files.TextCommands.aliases.get(prefix),
|
||||
};
|
||||
}),
|
||||
concatMap(element => {
|
||||
return of(element.mod).pipe(
|
||||
concatMap(element =>
|
||||
of(element.mod).pipe(
|
||||
isOneOfCorrectModules(CommandType.Text),
|
||||
map(mod => ({ ...element, mod })),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
.subscribe({
|
||||
next: value => this.setState(value),
|
||||
error: err => this.wrapper.sernEmitter?.emit('error', err),
|
||||
error: reason =>
|
||||
this.wrapper.sernEmitter?.emit('error', { type: PayloadType.Failure, reason }),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import type { Message } from 'discord.js';
|
||||
import { from, Observable, of, tap, throwError } from 'rxjs';
|
||||
import { concatMap, from, Observable, of, tap, throwError } from 'rxjs';
|
||||
import { SernError } from '../structures/errors';
|
||||
import type { Module, CommandModuleDefs, CommandModule } from '../structures/module';
|
||||
import type { Result } from 'ts-results-es';
|
||||
import { Result } from 'ts-results-es';
|
||||
import type { CommandType } from '../structures/enums';
|
||||
import type Wrapper from '../structures/wrapper';
|
||||
import { PayloadType } from '../structures/enums';
|
||||
|
||||
|
||||
export function ignoreNonBot(prefix: string) {
|
||||
return (src: Observable<Message>) =>
|
||||
new Observable<Message>(subscriber => {
|
||||
@@ -80,12 +79,26 @@ export function executeModule(
|
||||
},
|
||||
) {
|
||||
if (payload.res.every(el => el.ok)) {
|
||||
return from(Promise.resolve(payload.execute())).pipe(
|
||||
tap(() => {
|
||||
wrapper.sernEmitter?.emit('module.activate', {
|
||||
type: PayloadType.Success,
|
||||
module: payload.mod,
|
||||
});
|
||||
const executeFn = Result.wrapAsync<unknown, Error | string>(() =>
|
||||
Promise.resolve(payload.execute()),
|
||||
);
|
||||
return from(executeFn).pipe(
|
||||
concatMap(res => {
|
||||
if (res.err) {
|
||||
return throwError(() => ({
|
||||
type: PayloadType.Failure,
|
||||
reason: res.val,
|
||||
module: payload.mod,
|
||||
}));
|
||||
}
|
||||
return of(res.val).pipe(
|
||||
tap(() =>
|
||||
wrapper.sernEmitter?.emit('module.activate', {
|
||||
type: PayloadType.Success,
|
||||
module: payload.mod,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
@@ -94,6 +107,6 @@ export function executeModule(
|
||||
module: payload.mod,
|
||||
reason: SernError.PluginFailure,
|
||||
});
|
||||
return of(null);
|
||||
return of(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,13 @@ import { Err, Ok } from 'ts-results-es';
|
||||
import { ExternalEventEmitters } from './utilities/readFile';
|
||||
import type { EventEmitter } from 'events';
|
||||
import { processEvents } from './events/userDefinedEventsHandling';
|
||||
import type { CommandModule, EventModule } from './structures/module';
|
||||
import { EventType, PluginType } from './structures/enums';
|
||||
import type {
|
||||
CommandModule,
|
||||
CommandModuleDefs,
|
||||
EventModule,
|
||||
EventModuleDefs,
|
||||
} from './structures/module';
|
||||
import { CommandType, EventType, PluginType } from './structures/enums';
|
||||
import type {
|
||||
CommandPlugin,
|
||||
EventModuleCommandPluginDefs,
|
||||
@@ -42,7 +47,7 @@ export function init(wrapper: Wrapper) {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @deprecated - use Sern#makeDependencies instead
|
||||
* @param emitter Any external event emitter.
|
||||
* The object will be stored in a map, and then fetched by the name of the instance's class.
|
||||
* As there are infinite possibilities to adding external event emitters,
|
||||
@@ -119,3 +124,17 @@ export function eventModule(mod: InputEventModule): EventModule {
|
||||
plugins,
|
||||
} as EventModule;
|
||||
}
|
||||
|
||||
export abstract class CommandExecutable<Type extends CommandType> {
|
||||
abstract type: Type;
|
||||
plugins: CommandPlugin<Type>[] = [];
|
||||
onEvent: EventPlugin<Type>[] = [];
|
||||
abstract execute: CommandModuleDefs[Type]['execute'];
|
||||
}
|
||||
|
||||
export abstract class EventExecutable<Type extends EventType> {
|
||||
abstract type: Type;
|
||||
plugins: EventModuleCommandPluginDefs[Type][] = [];
|
||||
onEvent: EventModuleEventPluginDefs[Type][] = [];
|
||||
abstract execute: EventModuleDefs[Type]['execute'];
|
||||
}
|
||||
|
||||
@@ -6,13 +6,13 @@ import type {
|
||||
GuildMember,
|
||||
InteractionReplyOptions,
|
||||
Message,
|
||||
ReplyMessageOptions,
|
||||
Snowflake,
|
||||
TextBasedChannel,
|
||||
MessageReplyOptions,
|
||||
User,
|
||||
} from 'discord.js';
|
||||
import { type Option, None, Some } from 'ts-results-es';
|
||||
import type { Nullish } from '../../types/handler';
|
||||
import type { Nullish, ReplyOptions } from '../../types/handler';
|
||||
import { SernError } from './errors';
|
||||
|
||||
function firstSome<T>(...args: Option<T>[]): Nullish<T> {
|
||||
@@ -130,9 +130,7 @@ export default class Context {
|
||||
return this.oMsg.none && this.oInterac.none;
|
||||
}
|
||||
//Make queueable
|
||||
public reply(
|
||||
content: string | Omit<InteractionReplyOptions, 'fetchReply'> | ReplyMessageOptions,
|
||||
) {
|
||||
public reply(content: ReplyOptions) {
|
||||
return firstSome(
|
||||
this.oInterac.map(i => {
|
||||
return i
|
||||
@@ -140,7 +138,7 @@ export default class Context {
|
||||
.then(() => i.fetchReply());
|
||||
}),
|
||||
this.oMsg.map(m => {
|
||||
return m.reply(content as string | ReplyMessageOptions);
|
||||
return m.reply(content as string | MessageReplyOptions);
|
||||
}),
|
||||
)!;
|
||||
}
|
||||
|
||||
@@ -7,13 +7,17 @@ import type { EventModule } from './module';
|
||||
* @typedef {object} Wrapper
|
||||
*/
|
||||
interface Wrapper {
|
||||
//@deprecated - Use Sern#makeDependencies instead
|
||||
readonly client: Client;
|
||||
//@deprecated - Use Sern#makeDependencies instead
|
||||
readonly sernEmitter?: SernEmitter;
|
||||
readonly defaultPrefix?: string;
|
||||
readonly commands: string;
|
||||
readonly events?:
|
||||
| string
|
||||
//@deprecated - array and function options will be removed
|
||||
| { mod: EventModule; absPath: string }[]
|
||||
//@deprecated - array and function options will be removed
|
||||
| (() => { mod: EventModule; absPath: string }[]);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ApplicationCommandType, ComponentType } from 'discord.js';
|
||||
import { readdirSync, statSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { type Observable, from, concatAll } from 'rxjs';
|
||||
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';
|
||||
@@ -63,19 +63,26 @@ export function buildData<T>(commandDir: string): Observable<
|
||||
>
|
||||
> {
|
||||
const commands = getCommands(commandDir);
|
||||
return from(Promise.all(commands.map(async absPath => {
|
||||
let mod : T | undefined;
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
mod = require(absPath).default;
|
||||
} catch {
|
||||
mod = (await import(`file:///` +absPath)).default;
|
||||
}
|
||||
if (mod !== undefined) {
|
||||
return from(
|
||||
Promise.all(
|
||||
commands.map(async absPath => {
|
||||
let mod: T | undefined;
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
mod = require(absPath).default;
|
||||
} catch {
|
||||
mod = (await import(`file:///` + absPath)).default;
|
||||
}
|
||||
if (mod === undefined) {
|
||||
return Err(SernError.UndefinedModule);
|
||||
}
|
||||
try {
|
||||
mod = new (mod as unknown as new () => T)();
|
||||
} catch {}
|
||||
return Ok({ mod, absPath });
|
||||
} else return Err(SernError.UndefinedModule);
|
||||
})
|
||||
)).pipe(concatAll());
|
||||
}),
|
||||
),
|
||||
).pipe(concatAll());
|
||||
}
|
||||
|
||||
export function getCommands(dir: string): string[] {
|
||||
|
||||
@@ -1,31 +1,40 @@
|
||||
import type { SernOptionsData } from '../structures/module';
|
||||
import { ApplicationCommandOptionType, AutocompleteInteraction } from 'discord.js';
|
||||
|
||||
export default function treeSearch(iAutocomplete: AutocompleteInteraction, options: SernOptionsData[] | undefined) {
|
||||
if(options === undefined) return undefined;
|
||||
export default function treeSearch(
|
||||
iAutocomplete: AutocompleteInteraction,
|
||||
options: SernOptionsData[] | undefined,
|
||||
) {
|
||||
if (options === undefined) return undefined;
|
||||
const _options = options.slice(); // required to prevent direct mutation of options
|
||||
while ( _options.length > 0 ) {
|
||||
while (_options.length > 0) {
|
||||
const cur = _options.pop()!;
|
||||
switch ( cur.type ) {
|
||||
case ApplicationCommandOptionType.Subcommand : {
|
||||
for ( const option of cur.options ?? [] ) {
|
||||
_options.push(option);
|
||||
}
|
||||
} break;
|
||||
case ApplicationCommandOptionType.SubcommandGroup : {
|
||||
for (const command of cur.options ?? []) {
|
||||
_options.push(command);
|
||||
}
|
||||
} break;
|
||||
default : {
|
||||
if(cur.autocomplete) {
|
||||
const choice = iAutocomplete.options.getFocused(true);
|
||||
if(cur.name === choice.name && cur.autocomplete) {
|
||||
return cur;
|
||||
switch (cur.type) {
|
||||
case ApplicationCommandOptionType.Subcommand:
|
||||
{
|
||||
for (const option of cur.options ?? []) {
|
||||
_options.push(option);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
} break;
|
||||
break;
|
||||
case ApplicationCommandOptionType.SubcommandGroup:
|
||||
{
|
||||
for (const command of cur.options ?? []) {
|
||||
_options.push(command);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
if (cur.autocomplete) {
|
||||
const choice = iAutocomplete.options.getFocused(true);
|
||||
if (cur.name === choice.name && cur.autocomplete) {
|
||||
return cur;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import SernEmitter from './handler/sernEmitter';
|
||||
export { eventModule, commandModule } from './handler/sern';
|
||||
export { eventModule, commandModule, EventExecutable, CommandExecutable } from './handler/sern';
|
||||
export * as Sern from './handler/sern';
|
||||
export * from './types/handler';
|
||||
export * from './handler/structures/structxports';
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { CommandInteractionOptionResolver } from 'discord.js';
|
||||
import type { CommandModule, EventModule, Module } from '../handler/structures/module';
|
||||
import type { PayloadType } from '../handler/structures/enums';
|
||||
import type { InteractionReplyOptions, MessageReplyOptions } from 'discord.js';
|
||||
export type Nullish<T> = T | undefined | null;
|
||||
|
||||
// Thanks to @kelsny
|
||||
@@ -26,12 +27,6 @@ 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
|
||||
* [ T , V , B ] | [ A ] => T | V | B | A
|
||||
@@ -49,10 +44,15 @@ export type DefinedCommandModule = DefinitelyDefined<CommandModule, 'name' | 'de
|
||||
export type DefinedEventModule = DefinitelyDefined<EventModule, 'name' | 'description'>;
|
||||
export type Payload =
|
||||
| { type: PayloadType.Success; module: Module }
|
||||
| { type: PayloadType.Failure; module: Module | undefined; reason: string | Error };
|
||||
| { type: PayloadType.Failure; module?: Module; reason: string | Error };
|
||||
export type SernEventsMapping = {
|
||||
['module.register']: [Payload];
|
||||
['module.activate']: [Payload];
|
||||
['error']: [Error | string];
|
||||
['error']: [Payload];
|
||||
['warning']: [string];
|
||||
};
|
||||
|
||||
export type ReplyOptions =
|
||||
| string
|
||||
| Omit<InteractionReplyOptions, 'fetchReply'>
|
||||
| MessageReplyOptions;
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"importsNotUsedAsValues": "error",
|
||||
"moduleResolution": "node",
|
||||
"skipLibCheck": true,
|
||||
"declaration": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
},
|
||||
"exclude": ["node_modules", "dist"],
|
||||
"include": ["src"],
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"importsNotUsedAsValues": "error",
|
||||
"moduleResolution": "node",
|
||||
"skipLibCheck": true,
|
||||
"declaration": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"exclude": ["node_modules", "dist"],
|
||||
"include": ["src"]
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"extends": "./tsconfig-base.json",
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"outDir": "dist/cjs",
|
||||
"target": "esnext"
|
||||
}
|
||||
}
|
||||
"extends": "./tsconfig-base.json",
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"outDir": "dist/cjs",
|
||||
"target": "esnext"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"extends": "./tsconfig-base.json",
|
||||
"compilerOptions": {
|
||||
"module": "esnext",
|
||||
"outDir": "dist/esm",
|
||||
"target": "esnext"
|
||||
}
|
||||
}
|
||||
"extends": "./tsconfig-base.json",
|
||||
"compilerOptions": {
|
||||
"module": "esnext",
|
||||
"outDir": "dist/esm",
|
||||
"target": "esnext"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,36 +3,36 @@ import { defineConfig } from 'tsup';
|
||||
export default defineConfig([
|
||||
{
|
||||
entry: ['src/index.ts'],
|
||||
format : 'esm',
|
||||
format: 'esm',
|
||||
sourcemap: false,
|
||||
target: 'node16',
|
||||
tsconfig : './tsconfig-esm.json',
|
||||
outDir : './dist/esm',
|
||||
tsconfig: './tsconfig-esm.json',
|
||||
outDir: './dist/esm',
|
||||
platform: 'node',
|
||||
external: ['discord.js'],
|
||||
clean: true,
|
||||
treeshake: true,
|
||||
outExtension() {
|
||||
return {
|
||||
js : '.mjs'
|
||||
js: '.mjs',
|
||||
};
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
entry: ['src/index.ts'],
|
||||
format : 'cjs',
|
||||
format: 'cjs',
|
||||
splitting: false,
|
||||
sourcemap: false,
|
||||
external: ['discord.js'],
|
||||
clean: true,
|
||||
target: 'node16',
|
||||
tsconfig : './tsconfig-cjs.json',
|
||||
outDir : './dist/cjs',
|
||||
tsconfig: './tsconfig-cjs.json',
|
||||
outDir: './dist/cjs',
|
||||
platform: 'node',
|
||||
outExtension() {
|
||||
return {
|
||||
js : '.cjs'
|
||||
js: '.cjs',
|
||||
};
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user