mirror of
https://github.com/sern-handler/handler
synced 2026-06-18 13:52:15 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d5c6c714f | ||
|
|
4f2387119a | ||
|
|
a6fa4e3dcb | ||
|
|
c281832db2 | ||
|
|
a359f73fa2 | ||
| 655bb8d358 | |||
| e8d5029834 | |||
|
|
b0399f9507 | ||
|
|
b962dae36c | ||
|
|
c73cf96cb2 | ||
|
|
7458befe8a | ||
|
|
efe49391e8 | ||
|
|
3140f80c10 | ||
|
|
504cdee7b2 | ||
|
|
c7661f272c | ||
|
|
daac37c288 | ||
|
|
a579e272d0 |
2
.github/workflows/continuous-integration.yml
vendored
2
.github/workflows/continuous-integration.yml
vendored
@@ -23,7 +23,7 @@ jobs:
|
|||||||
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
|
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
|
||||||
|
|
||||||
- name: Set up Node.js
|
- name: Set up Node.js
|
||||||
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3
|
uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3
|
||||||
with:
|
with:
|
||||||
node-version: 17
|
node-version: 17
|
||||||
|
|
||||||
|
|||||||
2
.github/workflows/npm-publish.yml
vendored
2
.github/workflows/npm-publish.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
|
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
|
||||||
- uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3
|
- uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3
|
||||||
with:
|
with:
|
||||||
node-version: 17
|
node-version: 17
|
||||||
- run: yarn --immutable
|
- run: yarn --immutable
|
||||||
|
|||||||
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@@ -20,7 +20,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
|
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
|
||||||
- name: Use Node.js ${{ matrix.node-version }}
|
- name: Use Node.js ${{ matrix.node-version }}
|
||||||
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3
|
uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3
|
||||||
with:
|
with:
|
||||||
node-version: ${{ matrix.node-version }}
|
node-version: ${{ matrix.node-version }}
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
|
|||||||
33
CHANGELOG.md
33
CHANGELOG.md
@@ -1,5 +1,38 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## [3.3.2](https://github.com/sern-handler/handler/compare/v3.3.1...v3.3.2) (2024-01-08)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* presence feature not working on cjs applications ([#351](https://github.com/sern-handler/handler/issues/351)) ([4f23871](https://github.com/sern-handler/handler/commit/4f2387119acfde036d0d1626553e9050f55627d1))
|
||||||
|
|
||||||
|
## [3.3.1](https://github.com/sern-handler/handler/compare/v3.3.0...v3.3.1) (2024-01-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* crashing when slash command is used as text command ([#349](https://github.com/sern-handler/handler/issues/349)) ([a359f73](https://github.com/sern-handler/handler/commit/a359f73fa24127a4964d411c8c1c0dfea5edc0f1))
|
||||||
|
|
||||||
|
|
||||||
|
### Reverts
|
||||||
|
|
||||||
|
* the last commit ([655bb8d](https://github.com/sern-handler/handler/commit/655bb8d35815fe0ce9797d8b169310a07b284ae0))
|
||||||
|
|
||||||
|
## [3.3.0](https://github.com/sern-handler/handler/compare/v3.2.1...v3.3.0) (2023-12-27)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* presence ([#345](https://github.com/sern-handler/handler/issues/345)) ([7458bef](https://github.com/sern-handler/handler/commit/7458befe8a5900480cd71900df02a8364837dc00))
|
||||||
|
|
||||||
|
## [3.2.1](https://github.com/sern-handler/handler/compare/v3.2.0...v3.2.1) (2023-12-21)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* logger swap failing ([daac37c](https://github.com/sern-handler/handler/commit/daac37c28858c42b21042bdcb8141239db634e7d))
|
||||||
|
|
||||||
## [3.2.0](https://github.com/sern-handler/handler/compare/v3.1.1...v3.2.0) (2023-12-15)
|
## [3.2.0](https://github.com/sern-handler/handler/compare/v3.1.1...v3.2.0) (2023-12-15)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
41
README.md
41
README.md
@@ -19,28 +19,16 @@
|
|||||||
- Lightweight. Does a lot while being small.
|
- Lightweight. Does a lot while being small.
|
||||||
- Latest features. Support for discord.js v14 and all of its interactions.
|
- Latest features. Support for discord.js v14 and all of its interactions.
|
||||||
- Start quickly. Plug and play or customize to your liking.
|
- Start quickly. Plug and play or customize to your liking.
|
||||||
- Switch and customize how errors are handled, logging, and more.
|
|
||||||
- works with [bun](https://bun.sh/) and [node](https://nodejs.org/en) out the box!
|
- works with [bun](https://bun.sh/) and [node](https://nodejs.org/en) out the box!
|
||||||
- Use it with TypeScript or JavaScript. CommonJS and ESM supported.
|
- Use it with TypeScript or JavaScript. CommonJS and ESM supported.
|
||||||
- Active and growing community, always here to help. [Join us](https://sern.dev/discord)
|
- Active and growing community, always here to help. [Join us](https://sern.dev/discord)
|
||||||
- Unleash its full potential with a powerful CLI and awesome plugins.
|
- Unleash its full potential with a powerful CLI and awesome plugins.
|
||||||
|
|
||||||
## 📜 Installation
|
## 📜 Installation
|
||||||
|
[Start here!!](https://sern.dev/docs/guide/walkthrough/new-project)
|
||||||
```sh
|
|
||||||
npm install @sern/handler
|
|
||||||
```
|
|
||||||
|
|
||||||
```sh
|
|
||||||
yarn add @sern/handler
|
|
||||||
```
|
|
||||||
|
|
||||||
```sh
|
|
||||||
pnpm add @sern/handler
|
|
||||||
```
|
|
||||||
|
|
||||||
## 👶 Basic Usage
|
## 👶 Basic Usage
|
||||||
<details open><summary>ping.ts</summary>
|
<details><summary>ping.ts</summary>
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
export default commandModule({
|
export default commandModule({
|
||||||
@@ -54,7 +42,7 @@ export default commandModule({
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
</details>
|
</details>
|
||||||
<details open><summary>modal.ts</summary>
|
<details><summary>modal.ts</summary>
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
export default commandModule({
|
export default commandModule({
|
||||||
@@ -74,31 +62,8 @@ export default commandModule({
|
|||||||
})
|
})
|
||||||
```
|
```
|
||||||
</details>
|
</details>
|
||||||
<details open><summary>index.ts</summary>
|
|
||||||
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { Client, GatewayIntentBits } from 'discord.js';
|
|
||||||
import { Sern, single } from '@sern/handler';
|
|
||||||
|
|
||||||
//client has been declared previously
|
|
||||||
//Version 3
|
|
||||||
await makeDependencies({
|
|
||||||
build: root => root
|
|
||||||
.add({ '@sern/client': single(() => client) })
|
|
||||||
});
|
|
||||||
|
|
||||||
//View docs for all options
|
|
||||||
Sern.init({
|
|
||||||
defaultPrefix: '!', // removing defaultPrefix will shut down text commands
|
|
||||||
commands: 'src/commands',
|
|
||||||
// events: 'src/events' (optional),
|
|
||||||
});
|
|
||||||
|
|
||||||
client.login("YOUR_BOT_TOKEN_HERE");
|
|
||||||
```
|
|
||||||
</details>
|
|
||||||
|
|
||||||
## 🤖 Bots Using sern
|
## 🤖 Bots Using sern
|
||||||
- [Community Bot](https://github.com/sern-handler/sern-community), the community bot for our [discord server](https://sern.dev/discord).
|
- [Community Bot](https://github.com/sern-handler/sern-community), the community bot for our [discord server](https://sern.dev/discord).
|
||||||
- [Vinci](https://github.com/SrIzan10/vinci), the bot for Mara Turing.
|
- [Vinci](https://github.com/SrIzan10/vinci), the bot for Mara Turing.
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@sern/handler",
|
"name": "@sern/handler",
|
||||||
"packageManager": "yarn@3.5.0",
|
"packageManager": "yarn@3.5.0",
|
||||||
"version": "3.2.0",
|
"version": "3.3.2",
|
||||||
"description": "A complete, customizable, typesafe, & reactive framework for discord bots.",
|
"description": "A complete, customizable, typesafe, & reactive framework for discord bots.",
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
"module": "./dist/index.mjs",
|
"module": "./dist/index.mjs",
|
||||||
@@ -37,6 +37,7 @@
|
|||||||
"author": "SernDevs",
|
"author": "SernDevs",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"callsites": "^3.1.0",
|
||||||
"iti": "^0.6.0",
|
"iti": "^0.6.0",
|
||||||
"rxjs": "^7.8.0",
|
"rxjs": "^7.8.0",
|
||||||
"ts-results-es": "^4.0.0"
|
"ts-results-es": "^4.0.0"
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { CommandType, EventType, PluginType } from './structures';
|
import { CommandType, EventType, PluginType } from './structures';
|
||||||
import type { Plugin, PluginResult, EventArgs, CommandArgs } from '../types/core-plugin';
|
import type { Plugin, PluginResult, EventArgs, CommandArgs } from '../types/core-plugin';
|
||||||
import type { ClientEvents } from 'discord.js';
|
import type { ClientEvents } from 'discord.js';
|
||||||
|
import { err, ok } from './functions';
|
||||||
|
|
||||||
export function makePlugin<V extends unknown[]>(
|
export function makePlugin<V extends unknown[]>(
|
||||||
type: PluginType,
|
type: PluginType,
|
||||||
@@ -60,3 +61,12 @@ export function DiscordEventControlPlugin<T extends keyof ClientEvents>(
|
|||||||
) {
|
) {
|
||||||
return makePlugin(PluginType.Control, execute);
|
return makePlugin(PluginType.Control, execute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 1.0.0
|
||||||
|
* The object passed into every plugin to control a command's behavior
|
||||||
|
*/
|
||||||
|
export const controller = {
|
||||||
|
next: ok,
|
||||||
|
stop: err,
|
||||||
|
};
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ export function treeSearch(
|
|||||||
const choice = iAutocomplete.options.getFocused(true);
|
const choice = iAutocomplete.options.getFocused(true);
|
||||||
assert(
|
assert(
|
||||||
'command' in cur,
|
'command' in cur,
|
||||||
'No command property found for autocomplete option',
|
'No `command` property found for autocomplete option',
|
||||||
);
|
);
|
||||||
if (subcommands.size > 0) {
|
if (subcommands.size > 0) {
|
||||||
const parent = iAutocomplete.options.getSubcommand();
|
const parent = iAutocomplete.options.getSubcommand();
|
||||||
|
|||||||
@@ -4,20 +4,20 @@ import { CommandType, EventType } from './structures';
|
|||||||
/**
|
/**
|
||||||
* Construct unique ID for a given interaction object.
|
* Construct unique ID for a given interaction object.
|
||||||
* @param event The interaction object for which to create an ID.
|
* @param event The interaction object for which to create an ID.
|
||||||
* @returns A unique string ID based on the type and properties of the interaction object.
|
* @returns An array of unique string IDs based on the type and properties of the interaction object.
|
||||||
*/
|
*/
|
||||||
export function reconstruct<T extends Interaction>(event: T) {
|
export function reconstruct<T extends Interaction>(event: T) {
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case InteractionType.MessageComponent: {
|
case InteractionType.MessageComponent: {
|
||||||
return `${event.customId}_C${event.componentType}`;
|
return [`${event.customId}_C${event.componentType}`];
|
||||||
}
|
}
|
||||||
case InteractionType.ApplicationCommand:
|
case InteractionType.ApplicationCommand:
|
||||||
case InteractionType.ApplicationCommandAutocomplete: {
|
case InteractionType.ApplicationCommandAutocomplete: {
|
||||||
return `${event.commandName}_A${event.commandType}`;
|
return [`${event.commandName}_A${event.commandType}`, `${event.commandName}_B`];
|
||||||
}
|
}
|
||||||
//Modal interactions are classified as components for sern
|
//Modal interactions are classified as components for sern
|
||||||
case InteractionType.ModalSubmit: {
|
case InteractionType.ModalSubmit: {
|
||||||
return `${event.customId}_C1`;
|
return [`${event.customId}_M`];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,29 +27,28 @@ export function reconstruct<T extends Interaction>(event: T) {
|
|||||||
*/
|
*/
|
||||||
const appBitField = 0b000000001111;
|
const appBitField = 0b000000001111;
|
||||||
|
|
||||||
// Each index represents the exponent of a CommandType.
|
|
||||||
// Every CommandType is a power of two.
|
const TypeMap = new Map<number, number>([
|
||||||
export const CommandTypeDiscordApi = [
|
[CommandType.Text, 0],
|
||||||
1, // CommandType.Text
|
[CommandType.Both, 0],
|
||||||
ApplicationCommandType.ChatInput,
|
[CommandType.Slash, ApplicationCommandType.ChatInput],
|
||||||
ApplicationCommandType.User,
|
[CommandType.CtxUser, ApplicationCommandType.User],
|
||||||
ApplicationCommandType.Message,
|
[CommandType.CtxMsg, ApplicationCommandType.Message],
|
||||||
ComponentType.Button,
|
[CommandType.Button, ComponentType.Button],
|
||||||
ComponentType.StringSelect,
|
[CommandType.Modal, InteractionType.ModalSubmit],
|
||||||
1, // CommandType.Modal
|
[CommandType.StringSelect, ComponentType.StringSelect],
|
||||||
ComponentType.UserSelect,
|
[CommandType.UserSelect, ComponentType.UserSelect],
|
||||||
ComponentType.RoleSelect,
|
[CommandType.MentionableSelect, ComponentType.MentionableSelect],
|
||||||
ComponentType.MentionableSelect,
|
[CommandType.RoleSelect, ComponentType.RoleSelect],
|
||||||
ComponentType.ChannelSelect,
|
[CommandType.ChannelSelect, ComponentType.ChannelSelect]]);
|
||||||
];
|
|
||||||
/*
|
/*
|
||||||
* Generates a number based on CommandType.
|
* Generates a number based on CommandType.
|
||||||
* This corresponds to an ApplicationCommandType or ComponentType
|
* This corresponds to an ApplicationCommandType or ComponentType
|
||||||
* TextCommands are 0 as they aren't either or.
|
* TextCommands are 0 as they aren't either or.
|
||||||
*/
|
*/
|
||||||
function apiType(t: CommandType | EventType) {
|
function apiType(t: CommandType | EventType) {
|
||||||
if (t === CommandType.Both || t === CommandType.Modal) return 1;
|
return TypeMap.get(t)!;
|
||||||
return CommandTypeDiscordApi[Math.log2(t)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -58,6 +57,18 @@ function apiType(t: CommandType | EventType) {
|
|||||||
* Then, another number generated by apiType function is appended
|
* Then, another number generated by apiType function is appended
|
||||||
*/
|
*/
|
||||||
export function create(name: string, type: CommandType | EventType) {
|
export function create(name: string, type: CommandType | EventType) {
|
||||||
const am = (appBitField & type) !== 0 ? 'A' : 'C';
|
if(type == CommandType.Text) {
|
||||||
return name + '_' + am + apiType(type);
|
return `${name}_T`;
|
||||||
}
|
}
|
||||||
|
if(type == CommandType.Both) {
|
||||||
|
return `${name}_B`;
|
||||||
|
}
|
||||||
|
if(type == CommandType.Modal) {
|
||||||
|
return `${name}_M`;
|
||||||
|
}
|
||||||
|
const am = (appBitField & type) !== 0 ? 'A' : 'C';
|
||||||
|
return `${name}_${am}${apiType(type)}`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { CoreContainer } from './container';
|
|||||||
import { Result } from 'ts-results-es'
|
import { Result } from 'ts-results-es'
|
||||||
import { DefaultServices } from '../_internal';
|
import { DefaultServices } from '../_internal';
|
||||||
import { AnyFunction } from '../../types/utility';
|
import { AnyFunction } from '../../types/utility';
|
||||||
|
import type { Logging } from '../contracts/logging';
|
||||||
//SIDE EFFECT: GLOBAL DI
|
//SIDE EFFECT: GLOBAL DI
|
||||||
let containerSubject: CoreContainer<Partial<Dependencies>>;
|
let containerSubject: CoreContainer<Partial<Dependencies>>;
|
||||||
|
|
||||||
@@ -22,6 +23,12 @@ export function useContainerRaw() {
|
|||||||
return containerSubject;
|
return containerSubject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function disposeAll(logger: Logging|undefined) {
|
||||||
|
containerSubject
|
||||||
|
?.disposeAll()
|
||||||
|
.then(() => logger?.info({ message: 'Cleaning container and crashing' }));
|
||||||
|
}
|
||||||
|
|
||||||
const dependencyBuilder = (container: any, excluded: string[]) => {
|
const dependencyBuilder = (container: any, excluded: string[]) => {
|
||||||
type Insertable =
|
type Insertable =
|
||||||
| ((container: CoreContainer<Dependencies>) => unknown )
|
| ((container: CoreContainer<Dependencies>) => unknown )
|
||||||
@@ -82,16 +89,16 @@ export const insertLogger = (containerSubject: CoreContainer<any>) => {
|
|||||||
}
|
}
|
||||||
export async function makeDependencies<const T extends Dependencies>
|
export async function makeDependencies<const T extends Dependencies>
|
||||||
(conf: ValidDependencyConfig) {
|
(conf: ValidDependencyConfig) {
|
||||||
//Until there are more optional dependencies, just check if the logger exists
|
|
||||||
//SIDE EFFECT
|
|
||||||
containerSubject = new CoreContainer();
|
containerSubject = new CoreContainer();
|
||||||
if(typeof conf === 'function') {
|
if(typeof conf === 'function') {
|
||||||
const excluded: string[] = [];
|
const excluded: string[] = [];
|
||||||
conf(dependencyBuilder(containerSubject, excluded));
|
conf(dependencyBuilder(containerSubject, excluded));
|
||||||
if(!excluded.includes('@sern/logger')) {
|
|
||||||
assert.ok(!containerSubject.getTokens()['@sern/logger'])
|
if(!excluded.includes('@sern/logger')
|
||||||
|
&& !containerSubject.getTokens()['@sern/logger']) {
|
||||||
insertLogger(containerSubject);
|
insertLogger(containerSubject);
|
||||||
}
|
}
|
||||||
|
|
||||||
containerSubject.ready();
|
containerSubject.ready();
|
||||||
} else {
|
} else {
|
||||||
composeRoot(containerSubject, conf);
|
composeRoot(containerSubject, conf);
|
||||||
|
|||||||
@@ -22,19 +22,18 @@ export class CoreContainer<T extends Partial<Dependencies>> extends Container<T,
|
|||||||
|
|
||||||
(this as Container<{}, {}>)
|
(this as Container<{}, {}>)
|
||||||
.add({ '@sern/errors': () => new DefaultServices.DefaultErrorHandling(),
|
.add({ '@sern/errors': () => new DefaultServices.DefaultErrorHandling(),
|
||||||
'@sern/emitter': () => new SernEmitter(),
|
'@sern/emitter': () => new SernEmitter,
|
||||||
'@sern/store': () => new ModuleStore() })
|
'@sern/store': () => new ModuleStore })
|
||||||
.add(ctx => {
|
.add(ctx => {
|
||||||
return {
|
return { '@sern/modules': () =>
|
||||||
'@sern/modules': () =>
|
new DefaultServices.DefaultModuleManager(ctx['@sern/store']) };
|
||||||
new DefaultServices.DefaultModuleManager(ctx['@sern/store']),
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
isReady() {
|
isReady() {
|
||||||
return this.ready$.closed;
|
return this.ready$.closed;
|
||||||
}
|
}
|
||||||
|
|
||||||
override async disposeAll() {
|
override async disposeAll() {
|
||||||
|
|
||||||
const otherDisposables = Object
|
const otherDisposables = Object
|
||||||
|
|||||||
@@ -1,14 +1,23 @@
|
|||||||
import { Result } from 'ts-results-es';
|
import { Result } from 'ts-results-es';
|
||||||
import { type Observable, from, mergeMap, ObservableInput } from 'rxjs';
|
import { type Observable, from, mergeMap, ObservableInput } from 'rxjs';
|
||||||
import { readdir, stat } from 'fs/promises';
|
import { readdir, stat } from 'fs/promises';
|
||||||
import { basename, extname, join, resolve, parse } from 'path';
|
import { basename, extname, join, resolve, parse, dirname } from 'path';
|
||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
import { createRequire } from 'node:module';
|
import { createRequire } from 'node:module';
|
||||||
import type { ImportPayload, Wrapper } from '../types/core';
|
import type { ImportPayload, Wrapper } from '../types/core';
|
||||||
import type { Module } from '../types/core-modules';
|
import type { Module } from '../types/core-modules';
|
||||||
|
import { existsSync } from 'fs';
|
||||||
|
|
||||||
|
export const shouldHandle = (path: string, fpath: string) => {
|
||||||
|
const file_name = fpath+extname(path);
|
||||||
|
let newPath = join(dirname(path), file_name)
|
||||||
|
.replace(/file:\\?/, "");
|
||||||
|
return { exists: existsSync(newPath),
|
||||||
|
path: 'file:///'+newPath };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export type ModuleResult<T> = Promise<ImportPayload<T>>;
|
export type ModuleResult<T> = Promise<ImportPayload<T>>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Import any module based on the absolute path.
|
* Import any module based on the absolute path.
|
||||||
* This can accept four types of exported modules
|
* This can accept four types of exported modules
|
||||||
@@ -66,14 +75,14 @@ const isSkippable = (filename: string) => {
|
|||||||
const validExtensions = ['.js', '.cjs', '.mts', '.mjs', '.cts', '.ts', ''];
|
const validExtensions = ['.js', '.cjs', '.mts', '.mjs', '.cts', '.ts', ''];
|
||||||
return filename[0] === '!' || !validExtensions.includes(extname(filename));
|
return filename[0] === '!' || !validExtensions.includes(extname(filename));
|
||||||
};
|
};
|
||||||
|
|
||||||
async function deriveFileInfo(dir: string, file: string) {
|
async function deriveFileInfo(dir: string, file: string) {
|
||||||
const fullPath = join(dir, file);
|
const fullPath = join(dir, file);
|
||||||
return {
|
return { fullPath,
|
||||||
fullPath,
|
|
||||||
fileStats: await stat(fullPath),
|
fileStats: await stat(fullPath),
|
||||||
base: basename(file),
|
base: basename(file) };
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function* readPaths(dir: string): AsyncGenerator<string> {
|
async function* readPaths(dir: string): AsyncGenerator<string> {
|
||||||
try {
|
try {
|
||||||
const files = await readdir(dir);
|
const files = await readdir(dir);
|
||||||
@@ -118,10 +127,9 @@ export function loadConfig(wrapper: Wrapper | 'file'): Wrapper {
|
|||||||
eventsPath = makePath('events');
|
eventsPath = makePath('events');
|
||||||
console.log('Events path is set to', eventsPath);
|
console.log('Events path is set to', eventsPath);
|
||||||
}
|
}
|
||||||
return {
|
|
||||||
defaultPrefix: config.defaultPrefix,
|
return { defaultPrefix: config.defaultPrefix,
|
||||||
commands: commandsPath,
|
commands: commandsPath,
|
||||||
events: eventsPath,
|
events: eventsPath };
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,18 +61,19 @@ export function discordEvent<T extends keyof ClientEvents>(mod: {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
function prepareClassPlugins(c: Module) {
|
function prepareClassPlugins(c: Module) {
|
||||||
const [onEvent, initPlugins] = partitionPlugins(c.plugins);
|
const [onEvent, initPlugins] = partitionPlugins(c.plugins);
|
||||||
c.plugins = initPlugins as InitPlugin[];
|
c.plugins = initPlugins as InitPlugin[];
|
||||||
c.onEvent = onEvent as ControlPlugin[];
|
c.onEvent = onEvent as ControlPlugin[];
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// Class modules:
|
|
||||||
// Can be refactored.
|
|
||||||
// Both implement singleton, could I make them inherit a singleton parent class?
|
|
||||||
/**
|
/**
|
||||||
* @Experimental
|
* @deprecated
|
||||||
* Will be refactored / changed in future
|
* Will be removed in future
|
||||||
*/
|
*/
|
||||||
export abstract class CommandExecutable<const Type extends CommandType = CommandType> {
|
export abstract class CommandExecutable<const Type extends CommandType = CommandType> {
|
||||||
abstract type: Type;
|
abstract type: Type;
|
||||||
@@ -92,8 +93,8 @@ export abstract class CommandExecutable<const Type extends CommandType = Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Experimental
|
* @deprecated
|
||||||
* Will be refactored in future
|
* Will be removed in future
|
||||||
*/
|
*/
|
||||||
export abstract class EventExecutable<Type extends EventType> {
|
export abstract class EventExecutable<Type extends EventType> {
|
||||||
abstract type: Type;
|
abstract type: Type;
|
||||||
|
|||||||
@@ -28,16 +28,15 @@ export function filterMapTo<V>(item: () => V): OperatorFunction<boolean, V> {
|
|||||||
return concatMap(shouldKeep => (shouldKeep ? of(item()) : EMPTY));
|
return concatMap(shouldKeep => (shouldKeep ? of(item()) : EMPTY));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface PluginExecutable {
|
||||||
|
execute: (...args: unknown[]) => PluginResult;
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* Calls any plugin with {args}.
|
* Calls any plugin with {args}.
|
||||||
* @param args if an array, its spread and plugin called.
|
* @param args if an array, its spread and plugin called.
|
||||||
*/
|
*/
|
||||||
export function callPlugin(args: unknown): OperatorFunction<
|
export function callPlugin(args: unknown): OperatorFunction<PluginExecutable, VoidResult>
|
||||||
{
|
{
|
||||||
execute: (...args: unknown[]) => PluginResult;
|
|
||||||
},
|
|
||||||
VoidResult
|
|
||||||
> {
|
|
||||||
return concatMap(async plugin => {
|
return concatMap(async plugin => {
|
||||||
if (Array.isArray(args)) {
|
if (Array.isArray(args)) {
|
||||||
return plugin.execute(...args);
|
return plugin.execute(...args);
|
||||||
@@ -79,8 +78,6 @@ export const filterTap = <K, R>(onErr: (e: R) => void): OperatorFunction<Result<
|
|||||||
}
|
}
|
||||||
onErr(result.error);
|
onErr(result.error);
|
||||||
return EMPTY
|
return EMPTY
|
||||||
|
}))
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
70
src/core/presences.ts
Normal file
70
src/core/presences.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import type { ActivitiesOptions } from "discord.js";
|
||||||
|
import type { IntoDependencies } from "../types/ioc";
|
||||||
|
import type { Emitter } from "./contracts/emitter";
|
||||||
|
|
||||||
|
type Status = 'online' | 'idle' | 'invisible' | 'dnd'
|
||||||
|
type PresenceReduce = (previous: Result) => Result;
|
||||||
|
|
||||||
|
export interface Result {
|
||||||
|
status?: Status;
|
||||||
|
afk?: boolean;
|
||||||
|
activities?: ActivitiesOptions[];
|
||||||
|
shardId?: number[];
|
||||||
|
repeat?: number | [Emitter, string];
|
||||||
|
onRepeat?: (previous: Result) => Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Config <T extends (keyof Dependencies)[]> =
|
||||||
|
{
|
||||||
|
inject?: [...T]
|
||||||
|
execute: (...v: IntoDependencies<T>) => Result;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A small wrapper to provide type inference.
|
||||||
|
* Create a Presence module which **MUST** be put in a file called presence.(language-extension)
|
||||||
|
* adjacent to the file where **Sern.init** is CALLED.
|
||||||
|
*/
|
||||||
|
export function module<T extends (keyof Dependencies)[]>
|
||||||
|
(conf: Config<T>) {
|
||||||
|
return conf;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a Presence body which can be either:
|
||||||
|
* - once, the presence is activated only once.
|
||||||
|
* - repeated, per cycle or event, the presence can be changed.
|
||||||
|
*/
|
||||||
|
export function of(root: Omit<Result, 'repeat' | 'onRepeat'>) {
|
||||||
|
return {
|
||||||
|
/**
|
||||||
|
* @example
|
||||||
|
* Presence
|
||||||
|
* .of({
|
||||||
|
* activities: [{ name: "deez nuts" }]
|
||||||
|
* }) //starts the presence with "deez nuts".
|
||||||
|
* .repeated(prev => {
|
||||||
|
* return {
|
||||||
|
* afk: true,
|
||||||
|
* activities: prev.activities?.map(s => ({ ...s, name: s.name+"s" }))
|
||||||
|
* };
|
||||||
|
* }, 10000)) //every 10 s, the callback sets the presence to the returned one.
|
||||||
|
*/
|
||||||
|
repeated: (onRepeat: PresenceReduce, repeat: number | [Emitter, string]) => {
|
||||||
|
return { repeat, onRepeat, ...root }
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* @example
|
||||||
|
* Presence
|
||||||
|
* .of({
|
||||||
|
* activities: [
|
||||||
|
* { name: "Chilling out" }
|
||||||
|
* ]
|
||||||
|
* })
|
||||||
|
* .once() // Sets the presence once, with what's provided in '.of()'
|
||||||
|
*/
|
||||||
|
once: () => root
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
import type { ReplyOptions } from "../../types/utility";
|
|
||||||
import type { Logging } from "../contracts";
|
|
||||||
|
|
||||||
export interface Response {
|
|
||||||
type: 'fail' | 'continue';
|
|
||||||
body?: ReplyOptions;
|
|
||||||
log?: { type: keyof Logging; message: unknown }
|
|
||||||
}
|
|
||||||
|
|
||||||
export const of = () => {
|
|
||||||
const payload = {
|
|
||||||
type: 'fail',
|
|
||||||
body: undefined,
|
|
||||||
log : undefined
|
|
||||||
} as Record<PropertyKey, unknown>
|
|
||||||
|
|
||||||
return {
|
|
||||||
/**
|
|
||||||
* @param {'fail' | 'continue'} p a status to determine if the error will
|
|
||||||
* terminate your application or continue. Warning and
|
|
||||||
*/
|
|
||||||
status: (p: 'fail' | 'continue') => {
|
|
||||||
payload.type = p;
|
|
||||||
return payload;
|
|
||||||
},
|
|
||||||
/**
|
|
||||||
* @param {keyof Logging} type Determine to log to logger[type].
|
|
||||||
* @param {T} message the message to log
|
|
||||||
*
|
|
||||||
* Log this error with the logger.
|
|
||||||
*/
|
|
||||||
log: <T=string>(type: keyof Logging, message: T) => {
|
|
||||||
payload.log = { type, message };
|
|
||||||
return payload;
|
|
||||||
},
|
|
||||||
reply: (bodyContent: ReplyOptions) => {
|
|
||||||
payload.body = bodyContent;
|
|
||||||
return payload;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -114,7 +114,7 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
|||||||
if ('interaction' in wrappable) {
|
if ('interaction' in wrappable) {
|
||||||
return new Context(Ok(wrappable));
|
return new Context(Ok(wrappable));
|
||||||
}
|
}
|
||||||
assert.ok(wrappable.isChatInputCommand());
|
assert.ok(wrappable.isChatInputCommand(), "Context created with bad interaction.");
|
||||||
return new Context(Err(wrappable));
|
return new Context(Err(wrappable));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import * as assert from 'node:assert';
|
|||||||
*/
|
*/
|
||||||
export abstract class CoreContext<M, I> {
|
export abstract class CoreContext<M, I> {
|
||||||
protected constructor(protected ctx: Either<M, I>) {
|
protected constructor(protected ctx: Either<M, I>) {
|
||||||
assert.ok(typeof ctx === 'object' && ctx != null);
|
assert.ok(typeof ctx === 'object' && ctx != null, "Context was nonobject or null");
|
||||||
}
|
}
|
||||||
get message(): M {
|
get message(): M {
|
||||||
return this.ctx.expect(SernError.MismatchEvent);
|
return this.ctx.expect(SernError.MismatchEvent);
|
||||||
|
|||||||
@@ -3,4 +3,3 @@ export * from './context';
|
|||||||
export * from './sern-emitter';
|
export * from './sern-emitter';
|
||||||
export * from './services';
|
export * from './services';
|
||||||
export * from './module-store';
|
export * from './module-store';
|
||||||
export * as CommandError from './command-error';
|
|
||||||
|
|||||||
@@ -44,7 +44,10 @@ export class DefaultModuleManager implements ModuleManager {
|
|||||||
const publishable = 0b000000110;
|
const publishable = 0b000000110;
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
Array.from(entries)
|
Array.from(entries)
|
||||||
.filter(([id]) => !(Number.parseInt(id.at(-1)!) & publishable))
|
.filter(([id]) => {
|
||||||
|
const last_entry = id.at(-1);
|
||||||
|
return last_entry == 'B' || !(publishable & Number.parseInt(last_entry!));
|
||||||
|
})
|
||||||
.map(([, path]) => Files.importModule<CommandModule>(path)),
|
.map(([, path]) => Files.importModule<CommandModule>(path)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ import {
|
|||||||
import { createResultResolver } from './event-utils';
|
import { createResultResolver } from './event-utils';
|
||||||
import { BaseInteraction, Message } from 'discord.js';
|
import { BaseInteraction, Message } from 'discord.js';
|
||||||
import { CommandType, Context } from '../core';
|
import { CommandType, Context } from '../core';
|
||||||
import type { AnyFunction, Args } from '../types/utility';
|
import type { Args } from '../types/utility';
|
||||||
|
import { inspect } from 'node:util'
|
||||||
import type { CommandModule, Module, Processed } from '../types/core-modules';
|
import type { CommandModule, Module, Processed } from '../types/core-modules';
|
||||||
|
|
||||||
//TODO: refactor dispatchers so that it implements a strategy for each different type of payload?
|
//TODO: refactor dispatchers so that it implements a strategy for each different type of payload?
|
||||||
@@ -75,11 +76,8 @@ export function createDispatcher(payload: {
|
|||||||
case CommandType.Both: {
|
case CommandType.Both: {
|
||||||
if (isAutocomplete(payload.event)) {
|
if (isAutocomplete(payload.event)) {
|
||||||
const option = treeSearch(payload.event, payload.module.options);
|
const option = treeSearch(payload.event, payload.module.options);
|
||||||
assert.ok(
|
assert.ok(option, SernError.NotSupportedInteraction + ` There is no autocomplete tag for ` + inspect(payload.module));
|
||||||
option,
|
const { command } = option;
|
||||||
Error(SernError.NotSupportedInteraction + ` There is no autocomplete tag for this option`),
|
|
||||||
);
|
|
||||||
const { command, name, parent } = option;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...payload,
|
...payload,
|
||||||
|
|||||||
@@ -21,17 +21,17 @@ import {
|
|||||||
handleError,
|
handleError,
|
||||||
SernError,
|
SernError,
|
||||||
VoidResult,
|
VoidResult,
|
||||||
useContainerRaw,
|
|
||||||
} from '../core/_internal';
|
} from '../core/_internal';
|
||||||
import { Emitter, ErrorHandling, Logging, ModuleManager } from '../core';
|
import { Emitter, ErrorHandling, Logging, ModuleManager } from '../core';
|
||||||
import { contextArgs, createDispatcher } from './dispatchers';
|
import { contextArgs, createDispatcher } from './dispatchers';
|
||||||
import { ObservableInput, pipe } from 'rxjs';
|
import { ObservableInput, pipe } from 'rxjs';
|
||||||
import { SernEmitter } from '../core';
|
import { SernEmitter } from '../core';
|
||||||
import { Err, Ok, Result } from 'ts-results-es';
|
import { Err, Ok, Result } from 'ts-results-es';
|
||||||
import type { AnyFunction, Awaitable } from '../types/utility';
|
import type { Awaitable } from '../types/utility';
|
||||||
import type { ControlPlugin } from '../types/core-plugin';
|
import type { ControlPlugin } from '../types/core-plugin';
|
||||||
import type { AnyModule, CommandModule, Module, Processed } from '../types/core-modules';
|
import type { AnyModule, CommandModule, Module, Processed } from '../types/core-modules';
|
||||||
import type { ImportPayload } from '../types/core';
|
import type { ImportPayload } from '../types/core';
|
||||||
|
import { disposeAll } from '../core/ioc/base';
|
||||||
|
|
||||||
function createGenericHandler<Source, Narrowed extends Source, Output>(
|
function createGenericHandler<Source, Narrowed extends Source, Output>(
|
||||||
source: Observable<Source>,
|
source: Observable<Source>,
|
||||||
@@ -71,14 +71,18 @@ export function createInteractionHandler<T extends Interaction>(
|
|||||||
return createGenericHandler<Interaction, T, Result<ReturnType<typeof createDispatcher>, void>>(
|
return createGenericHandler<Interaction, T, Result<ReturnType<typeof createDispatcher>, void>>(
|
||||||
source,
|
source,
|
||||||
async event => {
|
async event => {
|
||||||
const fullPath = mg.get(Id.reconstruct(event));
|
const possibleIds = Id.reconstruct(event);
|
||||||
if(!fullPath) {
|
let fullPaths= possibleIds
|
||||||
return Err.EMPTY
|
.map(id => mg.get(id))
|
||||||
|
.filter((id): id is string => id !== undefined);
|
||||||
|
|
||||||
|
if(fullPaths.length == 0) {
|
||||||
|
return Err.EMPTY;
|
||||||
}
|
}
|
||||||
|
const [ path ] = fullPaths;
|
||||||
return Files
|
return Files
|
||||||
.defaultModuleLoader<Processed<CommandModule>>(fullPath)
|
.defaultModuleLoader<Processed<CommandModule>>(path)
|
||||||
.then(payload =>
|
.then(payload => Ok(createDispatcher({
|
||||||
Ok(createDispatcher({
|
|
||||||
module: payload.module,
|
module: payload.module,
|
||||||
event,
|
event,
|
||||||
})));
|
})));
|
||||||
@@ -93,17 +97,18 @@ export function createMessageHandler(
|
|||||||
) {
|
) {
|
||||||
return createGenericHandler(source, async event => {
|
return createGenericHandler(source, async event => {
|
||||||
const [prefix, ...rest] = fmt(event.content, defaultPrefix);
|
const [prefix, ...rest] = fmt(event.content, defaultPrefix);
|
||||||
const fullPath = mg.get(`${prefix}_A1`);
|
let fullPath = mg.get(`${prefix}_T`);
|
||||||
|
|
||||||
if(!fullPath) {
|
if(!fullPath) {
|
||||||
return Err('Possibly undefined behavior: could not find a static id to resolve')
|
fullPath = mg.get(`${prefix}_B`);
|
||||||
|
if(!fullPath) {
|
||||||
|
return Err('Possibly undefined behavior: could not find a static id to resolve');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Files
|
return Files
|
||||||
.defaultModuleLoader<Processed<CommandModule>>(fullPath)
|
.defaultModuleLoader<Processed<CommandModule>>(fullPath)
|
||||||
.then((payload)=> {
|
.then(payload => {
|
||||||
const args = contextArgs(event, rest);
|
const args = contextArgs(event, rest);
|
||||||
return Ok({ args, ...payload });
|
return Ok({ args, ...payload });
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -172,6 +177,8 @@ export function executeModule(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A higher order function that
|
* A higher order function that
|
||||||
* - creates a stream of {@link VoidResult} { config.createStream }
|
* - creates a stream of {@link VoidResult} { config.createStream }
|
||||||
@@ -258,8 +265,5 @@ export const handleCrash = (err: ErrorHandling, log?: Logging) =>
|
|||||||
log?.info({
|
log?.info({
|
||||||
message: 'A stream closed or reached end of lifetime',
|
message: 'A stream closed or reached end of lifetime',
|
||||||
});
|
});
|
||||||
useContainerRaw()
|
disposeAll(log);
|
||||||
?.disposeAll()
|
}));
|
||||||
.then(() => log?.info({ message: 'Cleaning container and crashing' }));
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|||||||
@@ -28,6 +28,5 @@ export function interactionHandler([emitter, err, log, modules, client]: Depende
|
|||||||
filterTap(e => emitter.emit('warning', SernEmitter.warning(e))),
|
filterTap(e => emitter.emit('warning', SernEmitter.warning(e))),
|
||||||
makeModuleExecutor(module =>
|
makeModuleExecutor(module =>
|
||||||
emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure))),
|
emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure))),
|
||||||
mergeMap(payload => executeModule(emitter, log, err, payload)),
|
mergeMap(payload => executeModule(emitter, log, err, payload)));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,5 @@ export function messageHandler(
|
|||||||
makeModuleExecutor(module => {
|
makeModuleExecutor(module => {
|
||||||
emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure));
|
emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure));
|
||||||
}),
|
}),
|
||||||
mergeMap(payload => executeModule(emitter, log, err, payload)),
|
mergeMap(payload => executeModule(emitter, log, err, payload)));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
46
src/handlers/presence.ts
Normal file
46
src/handlers/presence.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import { concatMap, from, interval, of, map, scan, startWith, fromEvent, take } from "rxjs"
|
||||||
|
import { Files } from "../core/_internal";
|
||||||
|
import * as Presence from "../core/presences";
|
||||||
|
import { Services } from "../core/ioc";
|
||||||
|
import assert from "node:assert";
|
||||||
|
|
||||||
|
type SetPresence = (conf: Presence.Result) => Promise<unknown>
|
||||||
|
|
||||||
|
const parseConfig = async (conf: Promise<Presence.Result>) => {
|
||||||
|
return conf.then(s => {
|
||||||
|
if('repeat' in s) {
|
||||||
|
const { onRepeat, repeat } = s;
|
||||||
|
assert(repeat !== undefined, "repeat option is undefined");
|
||||||
|
assert(onRepeat !== undefined, "onRepeat callback is undefined, but repeat exists");
|
||||||
|
const src$ = typeof repeat === 'number'
|
||||||
|
? interval(repeat)
|
||||||
|
: fromEvent(...repeat);
|
||||||
|
return src$
|
||||||
|
.pipe(scan(onRepeat, s),
|
||||||
|
startWith(s));
|
||||||
|
}
|
||||||
|
//take 1?
|
||||||
|
return of(s).pipe(take(1));
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
export const presenceHandler = (path: string, setPresence: SetPresence) => {
|
||||||
|
interface PresenceModule {
|
||||||
|
module: Presence.Config<(keyof Dependencies)[]>
|
||||||
|
}
|
||||||
|
const presence = Files
|
||||||
|
.importModule<PresenceModule>(path)
|
||||||
|
.then(({ module }) => {
|
||||||
|
//fetch services with the order preserved, passing it to the execute fn
|
||||||
|
const fetchedServices = Services(...module.inject ?? []);
|
||||||
|
return async () => module.execute(...fetchedServices);
|
||||||
|
})
|
||||||
|
const module$ = from(presence);
|
||||||
|
return module$.pipe(
|
||||||
|
//compose:.
|
||||||
|
//call the execute function, passing that result into parseConfig.
|
||||||
|
//concatMap resolves the promise, and passes it to the next concatMap.
|
||||||
|
concatMap(fn => parseConfig(fn())),
|
||||||
|
// subscribe to the observable parseConfig yields, and set the presence.
|
||||||
|
concatMap(conf => conf.pipe(map(setPresence))));
|
||||||
|
}
|
||||||
@@ -25,8 +25,7 @@ export function startReadyEvent(
|
|||||||
|
|
||||||
const once = () => pipe(
|
const once = () => pipe(
|
||||||
first(),
|
first(),
|
||||||
ignoreElements()
|
ignoreElements())
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
function register<T extends Processed<AnyModule>>(
|
function register<T extends Processed<AnyModule>>(
|
||||||
@@ -40,8 +39,12 @@ function register<T extends Processed<AnyModule>>(
|
|||||||
validModuleType,
|
validModuleType,
|
||||||
`Found ${module.name} at ${fullPath}, which does not have a valid type`,
|
`Found ${module.name} at ${fullPath}, which does not have a valid type`,
|
||||||
);
|
);
|
||||||
if (module.type === CommandType.Both || module.type === CommandType.Text) {
|
if (module.type === CommandType.Both) {
|
||||||
module.alias?.forEach(a => manager.set(`${a}_A1`, fullPath));
|
module.alias?.forEach(a => manager.set(`${a}_B`, fullPath));
|
||||||
|
} else {
|
||||||
|
if(module.type === CommandType.Text){
|
||||||
|
module.alias?.forEach(a => manager.set(`${a}_T`, fullPath));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Result.wrap(() => manager.set(id, fullPath));
|
return Result.wrap(() => manager.set(id, fullPath));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,8 @@ export {
|
|||||||
CommandExecutable,
|
CommandExecutable,
|
||||||
} from './core/modules';
|
} from './core/modules';
|
||||||
|
|
||||||
|
export * as Presence from './core/presences'
|
||||||
|
|
||||||
export {
|
export {
|
||||||
useContainerRaw
|
useContainerRaw
|
||||||
} from './core/_internal'
|
} from './core/_internal'
|
||||||
export { controller } from './sern';
|
|
||||||
|
|||||||
28
src/sern.ts
28
src/sern.ts
@@ -1,4 +1,5 @@
|
|||||||
import { handleCrash } from './handlers/_internal';
|
import { handleCrash } from './handlers/_internal';
|
||||||
|
import callsites from 'callsites';
|
||||||
import { err, ok, Files } from './core/_internal';
|
import { err, ok, Files } from './core/_internal';
|
||||||
import { merge } from 'rxjs';
|
import { merge } from 'rxjs';
|
||||||
import { Services } from './core/ioc';
|
import { Services } from './core/ioc';
|
||||||
@@ -7,6 +8,8 @@ import { eventsHandler } from './handlers/user-defined-events';
|
|||||||
import { startReadyEvent } from './handlers/ready-event';
|
import { startReadyEvent } from './handlers/ready-event';
|
||||||
import { messageHandler } from './handlers/message-event';
|
import { messageHandler } from './handlers/message-event';
|
||||||
import { interactionHandler } from './handlers/interaction-event';
|
import { interactionHandler } from './handlers/interaction-event';
|
||||||
|
import { presenceHandler } from './handlers/presence';
|
||||||
|
import { Client } from 'discord.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
@@ -31,13 +34,21 @@ export function init(maybeWrapper: Wrapper | 'file') {
|
|||||||
if (wrapper.events !== undefined) {
|
if (wrapper.events !== undefined) {
|
||||||
eventsHandler(dependencies, Files.getFullPathTree(wrapper.events));
|
eventsHandler(dependencies, Files.getFullPathTree(wrapper.events));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const initCallsite = callsites()[1].getFileName();
|
||||||
|
const presencePath = Files.shouldHandle(initCallsite!, "presence");
|
||||||
//Ready event: load all modules and when finished, time should be taken and logged
|
//Ready event: load all modules and when finished, time should be taken and logged
|
||||||
startReadyEvent(dependencies, Files.getFullPathTree(wrapper.commands)).add(() => {
|
startReadyEvent(dependencies, Files.getFullPathTree(wrapper.commands))
|
||||||
|
.add(() => {
|
||||||
const time = ((performance.now() - startTime) / 1000).toFixed(2);
|
const time = ((performance.now() - startTime) / 1000).toFixed(2);
|
||||||
dependencies[0].emit('modulesLoaded');
|
dependencies[0].emit('modulesLoaded');
|
||||||
logger?.info({
|
logger?.info({ message: `sern: registered all modules in ${time} s`, });
|
||||||
message: `sern: registered all modules in ${time} s`,
|
if(presencePath.exists) {
|
||||||
});
|
const setPresence = async (p: any) => {
|
||||||
|
return (dependencies[4] as Client).user?.setPresence(p);
|
||||||
|
}
|
||||||
|
presenceHandler(presencePath.path, setPresence).subscribe();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const messages$ = messageHandler(dependencies, wrapper.defaultPrefix);
|
const messages$ = messageHandler(dependencies, wrapper.defaultPrefix);
|
||||||
@@ -56,11 +67,4 @@ function useDependencies() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @since 1.0.0
|
|
||||||
* The object passed into every plugin to control a command's behavior
|
|
||||||
*/
|
|
||||||
export const controller = {
|
|
||||||
next: ok,
|
|
||||||
stop: err,
|
|
||||||
};
|
|
||||||
|
|||||||
57
test/core/presence.test.ts
Normal file
57
test/core/presence.test.ts
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import { describe, expect, it, vi } from 'vitest';
|
||||||
|
import { Presence } from '../../src';
|
||||||
|
|
||||||
|
|
||||||
|
// Example test suite for the module function
|
||||||
|
describe('module function', () => {
|
||||||
|
it('should return a valid configuration', () => {
|
||||||
|
const config: Presence.Config<['dependency1', 'dependency2']> = Presence.module({
|
||||||
|
inject: ['dependency1', 'dependency2'],
|
||||||
|
execute: vi.fn(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(config).toBeDefined();
|
||||||
|
expect(config.inject).toEqual(['dependency1', 'dependency2']);
|
||||||
|
expect(typeof config.execute).toBe('function');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('of function', () => {
|
||||||
|
it('should return a valid presence configuration without repeat and onRepeat', () => {
|
||||||
|
const presenceConfig = Presence.of({
|
||||||
|
status: 'online',
|
||||||
|
afk: false,
|
||||||
|
activities: [{ name: 'Test Activity' }],
|
||||||
|
shardId: [1, 2, 3],
|
||||||
|
}).once();
|
||||||
|
|
||||||
|
expect(presenceConfig).toBeDefined();
|
||||||
|
//@ts-ignore Maybe fix?
|
||||||
|
expect(presenceConfig.repeat).toBeUndefined();
|
||||||
|
//@ts-ignore Maybe fix?
|
||||||
|
expect(presenceConfig.onRepeat).toBeUndefined();
|
||||||
|
expect(presenceConfig).toMatchObject({
|
||||||
|
status: 'online',
|
||||||
|
afk: false,
|
||||||
|
activities: [{ name: 'Test Activity' }],
|
||||||
|
shardId: [1, 2, 3],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a valid presence configuration with repeat and onRepeat', () => {
|
||||||
|
const onRepeatCallback = vi.fn();
|
||||||
|
const presenceConfig = Presence.of({
|
||||||
|
status: 'idle',
|
||||||
|
activities: [{ name: 'Another Test Activity' }],
|
||||||
|
}).repeated(onRepeatCallback, 5000);
|
||||||
|
|
||||||
|
expect(presenceConfig).toBeDefined();
|
||||||
|
expect(presenceConfig.repeat).toBe(5000);
|
||||||
|
expect(presenceConfig.onRepeat).toBe(onRepeatCallback);
|
||||||
|
expect(presenceConfig).toMatchObject({
|
||||||
|
status: 'idle',
|
||||||
|
activities: [{ name: 'Another Test Activity' }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})
|
||||||
@@ -2,7 +2,6 @@ import { describe, expect, it, vi } from 'vitest';
|
|||||||
import * as Id from '../../src/core/id';
|
import * as Id from '../../src/core/id';
|
||||||
import { faker } from '@faker-js/faker';
|
import { faker } from '@faker-js/faker';
|
||||||
import { CommandModule, CommandType, commandModule } from '../../src';
|
import { CommandModule, CommandType, commandModule } from '../../src';
|
||||||
import { CommandTypeDiscordApi } from '../../src/core/id';
|
|
||||||
|
|
||||||
function createRandomCommandModules() {
|
function createRandomCommandModules() {
|
||||||
const randomCommandType = [
|
const randomCommandType = [
|
||||||
@@ -41,32 +40,8 @@ describe('id resolution', () => {
|
|||||||
const metadata = modules.map(createMetadata);
|
const metadata = modules.map(createMetadata);
|
||||||
metadata.forEach((meta, idx) => {
|
metadata.forEach((meta, idx) => {
|
||||||
const associatedModule = modules[idx];
|
const associatedModule = modules[idx];
|
||||||
const am = (appBitField & associatedModule.type) !== 0 ? 'A' : 'C';
|
const uid = Id.create(associatedModule.name!, associatedModule.type!);
|
||||||
let uid = 0;
|
expect(meta.id).toBe(uid);
|
||||||
if (
|
|
||||||
associatedModule.type === CommandType.Both ||
|
|
||||||
associatedModule.type === CommandType.Modal
|
|
||||||
) {
|
|
||||||
uid = 1;
|
|
||||||
} else {
|
|
||||||
uid = CommandTypeDiscordApi[Math.log2(associatedModule.type)];
|
|
||||||
}
|
|
||||||
expect(meta.id).toBe(associatedModule.name + '_' + am + uid);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('maps commands type to discord components or application commands', () => {
|
|
||||||
expect(CommandTypeDiscordApi[Math.log2(CommandType.Text)]).toBe(1);
|
|
||||||
|
|
||||||
expect(CommandTypeDiscordApi[1]).toBe(1);
|
|
||||||
expect(CommandTypeDiscordApi[Math.log2(CommandType.CtxUser)]).toBe(2);
|
|
||||||
expect(CommandTypeDiscordApi[Math.log2(CommandType.CtxMsg)]).toBe(3);
|
|
||||||
expect(CommandTypeDiscordApi[Math.log2(CommandType.Button)]).toBe(2);
|
|
||||||
expect(CommandTypeDiscordApi[Math.log2(CommandType.StringSelect)]).toBe(3);
|
|
||||||
expect(CommandTypeDiscordApi[Math.log2(CommandType.UserSelect)]).toBe(5);
|
|
||||||
expect(CommandTypeDiscordApi[Math.log2(CommandType.RoleSelect)]).toBe(6);
|
|
||||||
expect(CommandTypeDiscordApi[Math.log2(CommandType.MentionableSelect)]).toBe(7);
|
|
||||||
expect(CommandTypeDiscordApi[Math.log2(CommandType.ChannelSelect)]).toBe(8);
|
|
||||||
expect(CommandTypeDiscordApi[6]).toBe(1);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -627,6 +627,7 @@ __metadata:
|
|||||||
"@types/node": ^18.15.11
|
"@types/node": ^18.15.11
|
||||||
"@typescript-eslint/eslint-plugin": 5.58.0
|
"@typescript-eslint/eslint-plugin": 5.58.0
|
||||||
"@typescript-eslint/parser": 5.59.1
|
"@typescript-eslint/parser": 5.59.1
|
||||||
|
callsites: ^3.1.0
|
||||||
discord.js: ^14.11.0
|
discord.js: ^14.11.0
|
||||||
esbuild: ^0.17.0
|
esbuild: ^0.17.0
|
||||||
eslint: 8.39.0
|
eslint: 8.39.0
|
||||||
@@ -1186,7 +1187,7 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"callsites@npm:^3.0.0":
|
"callsites@npm:^3.0.0, callsites@npm:^3.1.0":
|
||||||
version: 3.1.0
|
version: 3.1.0
|
||||||
resolution: "callsites@npm:3.1.0"
|
resolution: "callsites@npm:3.1.0"
|
||||||
checksum: 072d17b6abb459c2ba96598918b55868af677154bec7e73d222ef95a8fdb9bbf7dae96a8421085cdad8cd190d86653b5b6dc55a4484f2e5b2e27d5e0c3fc15b3
|
checksum: 072d17b6abb459c2ba96598918b55868af677154bec7e73d222ef95a8fdb9bbf7dae96a8421085cdad8cd190d86653b5b6dc55a4484f2e5b2e27d5e0c3fc15b3
|
||||||
|
|||||||
Reference in New Issue
Block a user