Skip to content

Commit

Permalink
feat(Interactions): Interaction Handlers, Application Commands and mu…
Browse files Browse the repository at this point in the history
…ch much more! (#293)

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Jeroen Claassens <support@favware.tech>
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
  • Loading branch information
4 people committed Jan 8, 2022
1 parent abe36ae commit 6bc7f1b
Show file tree
Hide file tree
Showing 72 changed files with 5,126 additions and 1,072 deletions.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,28 @@
"@commitlint/cli": "^16.0.1",
"@commitlint/config-conventional": "^16.0.0",
"@favware/npm-deprecate": "^1.0.4",
"@favware/rollup-type-bundler": "^1.0.6",
"@favware/rollup-type-bundler": "^1.0.7",
"@sapphire/eslint-config": "^4.0.8",
"@sapphire/prettier-config": "^1.2.7",
"@sapphire/ts-config": "^3.1.6",
"@types/jest": "^27.4.0",
"@types/node": "^17.0.4",
"@types/node": "^17.0.8",
"@types/ws": "^8.2.2",
"@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"cz-conventional-changelog": "^3.3.0",
"discord.js": "^13.5.0",
"discord.js": "^13.5.1",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"gen-esm-wrapper": "^1.1.3",
"husky": "^7.0.4",
"jest": "^27.4.5",
"jest-circus": "^27.4.5",
"lint-staged": "^12.1.4",
"jest": "^27.4.7",
"jest-circus": "^27.4.6",
"lint-staged": "^12.1.7",
"prettier": "^2.5.1",
"pretty-quick": "^3.1.3",
"rollup": "^2.62.0",
"rollup": "^2.63.0",
"rollup-plugin-version-injector": "^1.3.3",
"standard-version": "^9.3.2",
"ts-jest": "^27.1.2",
Expand Down
14 changes: 0 additions & 14 deletions src/errorListeners/CoreCommandError.ts

This file was deleted.

28 changes: 22 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import {
acquire,
getDefaultBehaviorWhenNotIdentical,
registries,
setDefaultBehaviorWhenNotIdentical
} from './lib/utils/application-commands/ApplicationCommandRegistries';
import type { ApplicationCommandRegistry } from './lib/utils/application-commands/ApplicationCommandRegistry';

const ApplicationCommandRegistries = {
acquire,
setDefaultBehaviorWhenNotIdentical,
getDefaultBehaviorWhenNotIdentical,
get registries(): ReadonlyMap<string, ApplicationCommandRegistry> {
return registries;
}
};

export {
AliasPiece,
AliasPieceOptions,
Expand Down Expand Up @@ -31,12 +48,16 @@ export * from './lib/structures/ArgumentStore';
export * from './lib/structures/Command';
export * from './lib/structures/CommandStore';
export * from './lib/structures/ExtendedArgument';
export * from './lib/structures/InteractionHandler';
export * from './lib/structures/InteractionHandlerStore';
export * from './lib/structures/Listener';
export * from './lib/structures/ListenerStore';
export * from './lib/structures/Precondition';
export * from './lib/structures/PreconditionStore';
export * from './lib/types/Enums';
export * from './lib/types/Events';
export { ApplicationCommandRegistries };
export { ApplicationCommandRegistry, ApplicationCommandRegistryRegisterOptions } from './lib/utils/application-commands/ApplicationCommandRegistry';
export * from './lib/utils/logger/ILogger';
export * from './lib/utils/logger/Logger';
export * from './lib/utils/preconditions/conditions/IPreconditionCondition';
Expand All @@ -47,12 +68,7 @@ export * from './lib/utils/preconditions/containers/UserPermissionsPrecondition'
export * from './lib/utils/preconditions/IPreconditionContainer';
export * from './lib/utils/preconditions/PreconditionContainerArray';
export * from './lib/utils/preconditions/PreconditionContainerSingle';
export * as CorePreconditions from './preconditions';
/**
* @deprecated. Please use `CorePreconditions.ClientPermissions`. `ClientPermissionsCorePrecondition` will be removed in v3.0.0
*/
export { CorePrecondition as ClientPermissionsCorePrecondition } from './preconditions/ClientPermissions';
export type { CooldownContext } from './preconditions/Cooldown';
export * as CorePreconditions from './preconditions/index';

/**
* The [@sapphire/framework](https://github.com/sapphiredev/framework) version that you are currently using.
Expand Down
32 changes: 29 additions & 3 deletions src/lib/SapphireClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import type { Plugin } from './plugins/Plugin';
import { PluginManager } from './plugins/PluginManager';
import { ArgumentStore } from './structures/ArgumentStore';
import { CommandStore } from './structures/CommandStore';
import { InteractionHandlerStore } from './structures/InteractionHandlerStore';
import { ListenerStore } from './structures/ListenerStore';
import { PreconditionStore } from './structures/PreconditionStore';
import { BucketScope, PluginHook } from './types/Enums';
import { Events } from './types/Events';
import { acquire } from './utils/application-commands/ApplicationCommandRegistries';
import { ILogger, LogLevel } from './utils/logger/ILogger';
import { Logger } from './utils/logger/Logger';

container.applicationCommandRegistries = { acquire };

/**
* A valid prefix in Sapphire.
* * `string`: a single prefix, e.g. `'!'`.
Expand Down Expand Up @@ -103,12 +107,19 @@ export interface SapphireClientOptions {
enableLoaderTraceLoggings?: boolean;

/**
* If Sapphire should load our pre-included error event listeners that log any encountered errors to the {@link SapphireClient.logger} instance
* If Sapphire should load the pre-included error event listeners that log any encountered errors to the {@link SapphireClient.logger} instance
* @since 1.0.0
* @default true
*/
loadDefaultErrorListeners?: boolean;

/**
* If Sapphire should load the pre-included message command listeners that are used to process incoming messages for commands.
* @since 3.0.0
* @default false
*/
loadMessageCommandListeners?: boolean;

/**
* Controls whether the bot will automatically appear to be typing when a command is accepted.
* @default false
Expand Down Expand Up @@ -180,7 +191,7 @@ export class SapphireClient<Ready extends boolean = boolean> extends Client<Read
public id: Snowflake | null = null;

/**
* The method to be overriden by the developer.
* The method to be overridden by the developer.
* @since 1.0.0
* @return A string for a single prefix, an array of strings for matching multiple, or null for no match (mention prefix only).
* @example
Expand Down Expand Up @@ -260,12 +271,23 @@ export class SapphireClient<Ready extends boolean = boolean> extends Client<Read
}

this.id = options.id ?? null;

this.stores
.register(new ArgumentStore().registerPath(join(__dirname, '..', 'arguments'))) //
.register(new CommandStore())
.register(new InteractionHandlerStore())
.register(new ListenerStore().registerPath(join(__dirname, '..', 'listeners')))
.register(new PreconditionStore().registerPath(join(__dirname, '..', 'preconditions')));
if (options.loadDefaultErrorListeners !== false) this.stores.get('listeners').registerPath(join(__dirname, '..', 'errorListeners'));

const optionalListenersPath = join(__dirname, '..', 'optional-listeners');

if (options.loadDefaultErrorListeners !== false) {
this.stores.get('listeners').registerPath(join(optionalListenersPath, 'error-listeners'));
}

if (options.loadMessageCommandListeners === true) {
this.stores.get('listeners').registerPath(join(optionalListenersPath, 'message-command-listeners'));
}

for (const plugin of SapphireClient.plugins.values(PluginHook.PostInitialization)) {
plugin.hook.call(this, options);
Expand Down Expand Up @@ -341,11 +363,15 @@ declare module '@sapphire/pieces' {
client: SapphireClient;
logger: ILogger;
stores: StoreRegistry;
applicationCommandRegistries: {
acquire: typeof acquire;
};
}

interface StoreRegistryEntries {
arguments: ArgumentStore;
commands: CommandStore;
'interaction-handlers': InteractionHandlerStore;
listeners: ListenerStore;
preconditions: PreconditionStore;
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib/errors/Identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@ export const enum Identifiers {
PreconditionClientPermissionsNoPermissions = 'preconditionClientPermissionsNoPermissions',
PreconditionUserPermissions = 'preconditionUserPermissions',
PreconditionUserPermissionsNoPermissions = 'preconditionUserPermissionsNoPermissions',
PreconditionThreadOnly = 'preconditionThreadOnly'
PreconditionThreadOnly = 'preconditionThreadOnly',

PreconditionMissingMessageHandler = 'preconditionMissingMessageHandler',
PreconditionMissingChatInputHandler = 'preconditionMissingChatInputHandler',
PreconditionMissingContextMenuHandler = 'preconditionMissingContextMenuHandler'
}
8 changes: 4 additions & 4 deletions src/lib/parsers/Args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ArgumentError } from '../errors/ArgumentError';
import { Identifiers } from '../errors/Identifiers';
import { UserError } from '../errors/UserError';
import type { Argument, IArgument } from '../structures/Argument';
import type { Command } from '../structures/Command';
import type { MessageCommand } from '../structures/Command';
import { isSome, maybe, Maybe } from './Maybe';
import { Err, err, isErr, isOk, ok, Ok, Result } from './Result';

Expand All @@ -35,12 +35,12 @@ export class Args {
/**
* The command that is being run.
*/
public readonly command: Command;
public readonly command: MessageCommand;

/**
* The context of the command being run.
*/
public readonly commandContext: Command.RunContext;
public readonly commandContext: MessageCommand.RunContext;

/**
* The internal Lexure parser.
Expand All @@ -54,7 +54,7 @@ export class Args {
*/
private readonly states: Lexure.ArgsState[] = [];

public constructor(message: Message, command: Command, parser: Lexure.Args, context: Command.RunContext) {
public constructor(message: Message, command: MessageCommand, parser: Lexure.Args, context: MessageCommand.RunContext) {
this.message = message;
this.command = command;
this.parser = parser;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/parsers/Maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ export function isMaybe<T>(x: unknown): x is Maybe<T>;
export function isMaybe<T>(x: Maybe<T> | unknown): x is Maybe<T> {
return typeof x === 'object' && x !== null && typeof Reflect.get(x, 'exists') === 'boolean';
}

export type UnwrapMaybeValue<T extends Maybe<unknown>> = T extends Some<infer V> ? V : never;
6 changes: 3 additions & 3 deletions src/lib/structures/Argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ArgumentError } from '../errors/ArgumentError';
import type { UserError } from '../errors/UserError';
import { Args } from '../parsers/Args';
import type { Result } from '../parsers/Result';
import type { Command } from './Command';
import type { MessageCommand } from './Command';

/**
* Defines a synchronous result of an {@link Argument}, check {@link Argument.AsyncResult} for the asynchronous version.
Expand Down Expand Up @@ -115,8 +115,8 @@ export interface ArgumentContext<T = unknown> extends Record<PropertyKey, unknow
argument: IArgument<T>;
args: Args;
message: Message;
command: Command;
commandContext: Command.RunContext;
command: MessageCommand;
commandContext: MessageCommand.RunContext;
minimum?: number;
maximum?: number;
inclusive?: boolean;
Expand Down

0 comments on commit 6bc7f1b

Please sign in to comment.