mirror of
https://github.com/sern-handler/handler
synced 2026-07-05 14:09:39 +00:00
initplugins inject deps, inconspicuos
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { CommandType, EventType, PluginType } from './structures/enums';
|
||||
import type { Plugin, PluginResult, EventArgs, CommandArgs, InitArgs } from '../types/core-plugin';
|
||||
import type { ClientEvents } from 'discord.js';
|
||||
import { CommandType, PluginType } from './structures/enums';
|
||||
import type { Plugin, PluginResult, CommandArgs, InitArgs } from '../types/core-plugin';
|
||||
import { err, ok } from './functions';
|
||||
|
||||
export function makePlugin<V extends unknown[]>(
|
||||
@@ -31,27 +30,7 @@ export function CommandControlPlugin<I extends CommandType>(
|
||||
) {
|
||||
return makePlugin(PluginType.Control, execute);
|
||||
}
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
export function EventControlPlugin<I extends EventType>(
|
||||
execute: (...args: EventArgs<I>) => PluginResult,
|
||||
) {
|
||||
return makePlugin(PluginType.Control, execute);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
* @Experimental
|
||||
* A specialized function for creating control plugins with discord.js ClientEvents.
|
||||
* Will probably be moved one day!
|
||||
*/
|
||||
export function DiscordEventControlPlugin<T extends keyof ClientEvents>(
|
||||
name: T,
|
||||
execute: (...args: ClientEvents[T]) => PluginResult,
|
||||
) {
|
||||
return makePlugin(PluginType.Control, execute);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.0.0
|
||||
|
||||
@@ -46,37 +46,31 @@ export function treeSearch(
|
||||
while (_options.length > 0) {
|
||||
const cur = _options.pop()!;
|
||||
switch (cur.type) {
|
||||
case ApplicationCommandOptionType.Subcommand:
|
||||
{
|
||||
case ApplicationCommandOptionType.Subcommand: {
|
||||
subcommands.add(cur.name);
|
||||
for (const option of cur.options ?? []) _options.push(option);
|
||||
}
|
||||
break;
|
||||
case ApplicationCommandOptionType.SubcommandGroup:
|
||||
{
|
||||
} break;
|
||||
case ApplicationCommandOptionType.SubcommandGroup: {
|
||||
for (const command of cur.options ?? []) _options.push(command);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
if ('autocomplete' in cur && cur.autocomplete) {
|
||||
const choice = iAutocomplete.options.getFocused(true);
|
||||
assert( 'command' in cur, 'No `command` property found for option ' + cur.name);
|
||||
if (subcommands.size > 0) {
|
||||
const parent = iAutocomplete.options.getSubcommand();
|
||||
const parentAndOptionMatches =
|
||||
subcommands.has(parent) && cur.name === choice.name;
|
||||
if (parentAndOptionMatches) {
|
||||
return { ...cur, parent };
|
||||
}
|
||||
} else {
|
||||
if (cur.name === choice.name) {
|
||||
return { ...cur, parent: undefined };
|
||||
}
|
||||
} break;
|
||||
default: {
|
||||
if ('autocomplete' in cur && cur.autocomplete) {
|
||||
const choice = iAutocomplete.options.getFocused(true);
|
||||
assert( 'command' in cur, 'No `command` property found for option ' + cur.name);
|
||||
if (subcommands.size > 0) {
|
||||
const parent = iAutocomplete.options.getSubcommand();
|
||||
const parentAndOptionMatches =
|
||||
subcommands.has(parent) && cur.name === choice.name;
|
||||
if (parentAndOptionMatches) {
|
||||
return { ...cur, parent };
|
||||
}
|
||||
} else {
|
||||
if (cur.name === choice.name) {
|
||||
return { ...cur, parent: undefined };
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { ClientEvents } from 'discord.js';
|
||||
import { EventType } from '../core/structures/enums';
|
||||
import type { AnyEventPlugin, } from '../types/core-plugin';
|
||||
import type {
|
||||
InputCommand,
|
||||
InputEvent,
|
||||
@@ -21,18 +20,14 @@ export function commandModule(mod: InputCommand): Module {
|
||||
plugins,
|
||||
} as Module;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.0.0
|
||||
* The wrapper function to define event modules for sern
|
||||
* @param mod
|
||||
*/
|
||||
export function eventModule(mod: InputEvent): Module {
|
||||
const [onEvent, plugins] = partitionPlugins(mod.plugins);
|
||||
return {
|
||||
...mod,
|
||||
plugins,
|
||||
onEvent,
|
||||
} as Module;
|
||||
return mod as Module;
|
||||
}
|
||||
|
||||
/** Create event modules from discord.js client events,
|
||||
@@ -43,7 +38,6 @@ export function eventModule(mod: InputEvent): Module {
|
||||
*/
|
||||
export function discordEvent<T extends keyof ClientEvents>(mod: {
|
||||
name: T;
|
||||
plugins?: AnyEventPlugin[];
|
||||
execute: (...args: ClientEvents[T]) => Awaitable<unknown>;
|
||||
}) {
|
||||
return eventModule({ type: EventType.Discord, ...mod, });
|
||||
|
||||
@@ -24,25 +24,26 @@ function fmt(msg: string, prefix?: string): string[] {
|
||||
* Message and ChatInputCommandInteraction
|
||||
*/
|
||||
export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
||||
prefix: string|undefined;
|
||||
|
||||
get options() {
|
||||
return this.interaction.options;
|
||||
}
|
||||
|
||||
args() {
|
||||
return {
|
||||
message: <T = string[]>() => {
|
||||
args(type: 'message'|'interaction', parser?: Function) {
|
||||
switch(type) {
|
||||
case 'message': {
|
||||
const [, ...rest] = fmt(this.message.content, this.prefix);
|
||||
return rest as T;
|
||||
},
|
||||
interaction: () => this.interaction.options
|
||||
return rest;
|
||||
};
|
||||
case 'interaction': {
|
||||
return this.interaction.options;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
protected constructor(protected ctx: Result<Message, ChatInputCommandInteraction>, prefix?: string) {
|
||||
protected constructor(protected ctx: Result<Message, ChatInputCommandInteraction>,
|
||||
public prefix?: string) {
|
||||
super(ctx);
|
||||
this.prefix = prefix
|
||||
}
|
||||
|
||||
public get id(): Snowflake {
|
||||
@@ -52,9 +53,7 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
||||
}
|
||||
|
||||
public get channel() {
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.channel)
|
||||
.mapErr(i => i.channel));
|
||||
return safeUnwrap(this.ctx.map(m => m.channel).mapErr(i => i.channel));
|
||||
}
|
||||
|
||||
public get channelId(): Snowflake {
|
||||
|
||||
Reference in New Issue
Block a user