@wolfstar/http-framework 6.2.2 → 6.3.0-next-20261007181329

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,85 @@ import { JSONEncodable } from "@discordjs/util";
11
11
  import { webcrypto } from "node:crypto";
12
12
  import { ListenOptions as ListenOptions$1 } from "node:net";
13
13
  import { ChokidarOptions } from "chokidar";
14
+ //#region src/lib/errors/UserError.d.ts
15
+ /**
16
+ * The UserError class to be thrown and emitted by the pieces of the framework.
17
+ *
18
+ * @remarks
19
+ * This class is the root of every framework-thrown error, allowing consumers to distinguish
20
+ * errors that are meant to be shown to the user from unexpected, internal ones.
21
+ *
22
+ * When a command, an autocomplete handler, or an interaction handler throws a `UserError` (or any subclass), the
23
+ * client emits the matching `*Denied` event ({@link Events.ChatInputCommandDenied}, {@link Events.ContextMenuCommandDenied},
24
+ * {@link Events.AutocompleteDenied}, or {@link Events.InteractionHandlerDenied}) instead of the `*Error` one, so listeners
25
+ * can reply to the user without reporting an expected failure as a bug. Use {@link isUserError} to apply the same check.
26
+ *
27
+ * @property name This will be `'UserError'` and can be used to distinguish the type of error when any error gets thrown.
28
+ * @since 3.2.0
29
+ * @example
30
+ * ```typescript
31
+ * throw new UserError({
32
+ * identifier: Identifiers.ArgumentIntegerTooSmall,
33
+ * message: 'The number you provided is too small.',
34
+ * context: { received: 2, minimum: 3 }
35
+ * });
36
+ * ```
37
+ */
38
+ export declare class UserError extends Error {
39
+ /**
40
+ * An identifier, useful to localize emitted errors.
41
+ * @since 3.2.0
42
+ */
43
+ readonly identifier: string;
44
+ /**
45
+ * User-provided context.
46
+ * @since 3.2.0
47
+ */
48
+ readonly context: unknown;
49
+ /**
50
+ * Constructs an UserError.
51
+ * @param options The UserError options
52
+ */
53
+ constructor(options: UserError.Options);
54
+ get name(): string;
55
+ }
56
+ export declare namespace UserError {
57
+ /**
58
+ * The options for {@link UserError}.
59
+ * @since 3.2.0
60
+ */
61
+ interface Options {
62
+ /**
63
+ * The identifier for this error.
64
+ * @since 3.2.0
65
+ */
66
+ identifier: string;
67
+ /**
68
+ * The message to be passed to the Error constructor.
69
+ * @since 3.2.0
70
+ */
71
+ message?: string;
72
+ /**
73
+ * The extra context to provide more information about this error.
74
+ * @since 3.2.0
75
+ * @default null
76
+ */
77
+ context?: unknown;
78
+ }
79
+ }
80
+ /**
81
+ * Checks whether a value is a {@link UserError}, which is how the client tells an expected failure from an unexpected
82
+ * one.
83
+ *
84
+ * @remarks `instanceof` is tried first. A bundle that inlines the framework, or a second installed copy of it, has its
85
+ * own `UserError` class that `instanceof` does not recognise, so any `Error` with a string `identifier` and a `context`
86
+ * property is accepted too.
87
+ * @param error The value to check.
88
+ * @returns Whether the value is a `UserError`.
89
+ * @since 6.3.0
90
+ */
91
+ export declare function isUserError(error: unknown): error is UserError;
92
+ //#endregion
14
93
  //#region src/lib/utils/internals.d.ts
15
94
  type Void = void;
16
95
  //#endregion
@@ -366,7 +445,7 @@ export declare class CommandStoreRouter {
366
445
  * @param interaction - The interaction object.
367
446
  * @returns The command associated with the interaction, or null if not found.
368
447
  */
369
- get(interaction: APIApplicationCommandInteraction): Command<import("@sapphire/pieces").PieceOptions> | null;
448
+ get(interaction: APIApplicationCommandInteraction): Command<Command.Options> | null;
370
449
  /**
371
450
  * Gets the chat input command with the specified name.
372
451
  *
@@ -1578,6 +1657,389 @@ export declare class CommandRouter<Options extends Command.Options = Command.Opt
1578
1657
  routeContextMenuInteraction(data: APIContextMenuInteractionData): string | null;
1579
1658
  }
1580
1659
  //#endregion
1660
+ //#region src/lib/structures/InteractionHandler.d.ts
1661
+ export declare abstract class InteractionHandler<Options extends InteractionHandler.Options = InteractionHandler.Options> extends Piece$1<Options, 'interaction-handlers'> {
1662
+ constructor(context: InteractionHandler.LoaderContext, options?: Options);
1663
+ abstract run(interaction: InteractionHandler.Interaction, customIdValue: unknown): Awaited<unknown>;
1664
+ }
1665
+ export declare namespace InteractionHandler {
1666
+ type ButtonInteraction = Interactions.MessageComponentButton;
1667
+ type SelectMenuInteraction = Interactions.MessageComponentSelectMenu;
1668
+ type ModalInteraction = Interactions.ModalSubmit;
1669
+ type MessageComponentInteraction = Interactions.MessageComponent;
1670
+ type ContextMenuInteraction = Interactions.ContextMenuCommand;
1671
+ type ApplicationCommandInteraction = Interactions.ApplicationCommand;
1672
+ type Interaction = Interactions.MessageComponent;
1673
+ type InteractionData = Interaction['data'];
1674
+ /** @deprecated Use {@linkcode LoaderContext} instead. */
1675
+ type Context = LoaderContext;
1676
+ type LoaderContext = Piece$1.LoaderContext<'interaction-handlers'>;
1677
+ type JSON = Piece$1.JSON;
1678
+ type LocationJSON = Piece$1.LocationJSON;
1679
+ interface Options extends Piece$1.Options {
1680
+ /**
1681
+ * The checks to run, in order, before the handler, after the global preconditions. An entry is the name of a
1682
+ * `Precondition` piece (which needs an `interactionHandlerRun` method to apply), that name with a `context`, a
1683
+ * function, or an array of entries (see `Command.Options.preconditions`). The first denial stops the interaction: the handler is not run and the error is
1684
+ * emitted as `interactionHandlerDenied`.
1685
+ *
1686
+ * @since 6.3.0
1687
+ * @default []
1688
+ */
1689
+ preconditions?: PreconditionArrayResolvable<PreconditionFunction<Interaction, InteractionHandler>>;
1690
+ }
1691
+ }
1692
+ //#endregion
1693
+ //#region src/lib/errors/PreconditionError.d.ts
1694
+ /**
1695
+ * Errors thrown by preconditions, that is, any check that runs before a command or an interaction
1696
+ * handler is executed, such as the guild restrictions applied by
1697
+ * {@link RestrictGuildIds}.
1698
+ *
1699
+ * @remarks Being a {@link UserError}, it is emitted as a `*Denied` event when thrown from a command, an autocomplete
1700
+ * handler, or an interaction handler, and not as `commandError`. The framework has no precondition store: the
1701
+ * decorators of `@wolfstar/decorators` are what throws it.
1702
+ *
1703
+ * @property name This will be `'PreconditionError'` and can be used to distinguish the type of error when any error gets thrown.
1704
+ * @since 3.2.0
1705
+ * @example
1706
+ * ```typescript
1707
+ * throw new PreconditionError({
1708
+ * precondition: 'GuildIds',
1709
+ * identifier: Identifiers.PreconditionGuildIds,
1710
+ * message: 'This command can only be used in specific guilds.',
1711
+ * context: { guildIds: ['737141877803057244'] }
1712
+ * });
1713
+ * ```
1714
+ */
1715
+ export declare class PreconditionError extends UserError {
1716
+ /**
1717
+ * The name of the precondition that caused the error.
1718
+ * @since 3.2.0
1719
+ */
1720
+ readonly precondition: string;
1721
+ constructor(options: PreconditionError.Options);
1722
+ get name(): string;
1723
+ }
1724
+ export declare namespace PreconditionError {
1725
+ /**
1726
+ * The options for {@link PreconditionError}.
1727
+ * @since 3.2.0
1728
+ */
1729
+ interface Options extends Omit<UserError.Options, 'identifier'> {
1730
+ /**
1731
+ * The name of the precondition that caused the error.
1732
+ * @since 3.2.0
1733
+ */
1734
+ precondition: string;
1735
+ /**
1736
+ * The identifier.
1737
+ * @since 3.2.0
1738
+ * @default precondition
1739
+ */
1740
+ identifier?: string;
1741
+ }
1742
+ }
1743
+ //#endregion
1744
+ //#region src/lib/structures/Precondition.d.ts
1745
+ /**
1746
+ * A check that runs before a command method does, loaded from the `preconditions` directory into the
1747
+ * `preconditions` store.
1748
+ *
1749
+ * @remarks A precondition with a `position` is global: it runs, in ascending order of `position`, before the
1750
+ * preconditions of every chat input and context menu command, and of every autocomplete and interaction handler that
1751
+ * it has a method for. Any other one runs only for the commands that list its
1752
+ * name in the `preconditions` option. The method it needs depends on the command, and a precondition that is asked to
1753
+ * run for a kind of command it has no method for denies the command with
1754
+ * {@linkcode Identifiers.PreconditionMissingChatInputHandler} or
1755
+ * {@linkcode Identifiers.PreconditionMissingContextMenuHandler}.
1756
+ * @since 6.3.0
1757
+ * @example
1758
+ * ```typescript
1759
+ * import { Precondition } from '@wolfstar/http-framework';
1760
+ *
1761
+ * export class UserPrecondition extends Precondition {
1762
+ * public override chatInputRun(interaction: Precondition.ChatInputInteraction) {
1763
+ * return interaction.user.id === process.env.OWNER_ID ? this.ok() : this.error({ message: 'Only the owner can use this command.' });
1764
+ * }
1765
+ * }
1766
+ * ```
1767
+ */
1768
+ export declare class Precondition<Options extends Precondition.Options = Precondition.Options> extends Piece$1<Options, 'preconditions'> {
1769
+ /**
1770
+ * The position of the precondition in the list of global preconditions, or `null` when it is not global.
1771
+ * @since 6.3.0
1772
+ */
1773
+ readonly position: number | null;
1774
+ constructor(context: Precondition.LoaderContext, options?: Options);
1775
+ /**
1776
+ * Checks a chat input command.
1777
+ *
1778
+ * @param interaction The interaction of the command.
1779
+ * @param command The command that is about to run.
1780
+ * @param context The context of the check: the `context` given in the `preconditions` option of the command.
1781
+ */
1782
+ chatInputRun?(interaction: Precondition.ChatInputInteraction, command: Command, context: Precondition.Context): Precondition.Result;
1783
+ /**
1784
+ * Checks a user or message context menu command.
1785
+ *
1786
+ * @param interaction The interaction of the command.
1787
+ * @param command The command that is about to run.
1788
+ * @param context The context of the check: the `context` given in the `preconditions` option of the command.
1789
+ */
1790
+ contextMenuRun?(interaction: Precondition.ContextMenuInteraction, command: Command, context: Precondition.Context): Precondition.Result;
1791
+ /**
1792
+ * Checks the autocomplete of a command. Unlike `chatInputRun` and `contextMenuRun`, a precondition without this
1793
+ * method is skipped, not denied, so a check written for the commands does not break their autocomplete.
1794
+ *
1795
+ * @param interaction The autocomplete interaction.
1796
+ * @param command The command whose autocomplete is about to run.
1797
+ * @param context The context of the check: the `context` given in the `preconditions` option of the command.
1798
+ * @since 6.3.0
1799
+ */
1800
+ autocompleteRun?(interaction: Precondition.AutocompleteInteraction, command: Command, context: Precondition.Context): Precondition.Result;
1801
+ /**
1802
+ * Checks an interaction handler (a button, a select menu, or a modal). A precondition without this method is
1803
+ * skipped, not denied.
1804
+ *
1805
+ * @param interaction The interaction of the component or modal.
1806
+ * @param handler The interaction handler that is about to run.
1807
+ * @param context The context of the check: the `context` given in the `preconditions` option of the handler.
1808
+ * @since 6.3.0
1809
+ */
1810
+ interactionHandlerRun?(interaction: Precondition.InteractionHandlerInteraction, handler: InteractionHandler, context: Precondition.Context): Precondition.Result;
1811
+ /**
1812
+ * Lets the command run.
1813
+ * @since 6.3.0
1814
+ */
1815
+ ok(): Precondition.Result;
1816
+ /**
1817
+ * Denies the command with a {@link PreconditionError} whose `precondition` is the name of this piece.
1818
+ *
1819
+ * @param options The message, identifier and context of the error.
1820
+ * @since 6.3.0
1821
+ */
1822
+ error(options?: Omit<PreconditionError.Options, 'precondition'>): Precondition.Result;
1823
+ }
1824
+ /**
1825
+ * A {@link Precondition} that has to implement the check for every kind of command.
1826
+ *
1827
+ * @since 6.3.0
1828
+ */
1829
+ export declare abstract class AllFlowsPrecondition extends Precondition {
1830
+ abstract chatInputRun(interaction: Precondition.ChatInputInteraction, command: Command, context: Precondition.Context): Precondition.Result;
1831
+ abstract contextMenuRun(interaction: Precondition.ContextMenuInteraction, command: Command, context: Precondition.Context): Precondition.Result;
1832
+ }
1833
+ export interface PreconditionOptions extends Piece$1.Options {
1834
+ /**
1835
+ * The position of the precondition in the list of global preconditions. When it is `null`, the precondition is not
1836
+ * global and only runs for the commands that list it.
1837
+ * @since 6.3.0
1838
+ * @default null
1839
+ */
1840
+ position?: number | null;
1841
+ }
1842
+ export declare namespace Precondition {
1843
+ type Options = PreconditionOptions;
1844
+ type LoaderContext = Piece$1.LoaderContext<'preconditions'>;
1845
+ type Context = PreconditionContext;
1846
+ type Result = PreconditionResult;
1847
+ type ChatInputInteraction = Command.ChatInputInteraction;
1848
+ type ContextMenuInteraction = Command.ContextMenuInteraction;
1849
+ type AutocompleteInteraction = Command.AutocompleteInteraction;
1850
+ type InteractionHandlerInteraction = InteractionHandler.Interaction;
1851
+ }
1852
+ export declare namespace AllFlowsPrecondition {
1853
+ type Options = PreconditionOptions;
1854
+ type LoaderContext = Piece$1.LoaderContext<'preconditions'>;
1855
+ type Context = PreconditionContext;
1856
+ type Result = PreconditionResult;
1857
+ }
1858
+ //#endregion
1859
+ //#region src/lib/utils/preconditions.d.ts
1860
+ /**
1861
+ * What a precondition returns: `Result.ok()` to let the command run, or a `Result.err(error)` with a
1862
+ * {@link UserError} (usually a `PreconditionError`) to deny it.
1863
+ *
1864
+ * @since 6.3.0
1865
+ */
1866
+ export type PreconditionResult = Awaitable<Result<unknown, UserError>>;
1867
+ /**
1868
+ * The data a precondition is given besides the interaction and the command.
1869
+ *
1870
+ * @since 6.3.0
1871
+ */
1872
+ export type PreconditionContext = Record<PropertyKey, unknown>;
1873
+ /**
1874
+ * What a precondition is run for, which decides the method it is asked for: `chatInputRun`, `contextMenuRun`,
1875
+ * `autocompleteRun`, or `interactionHandlerRun`.
1876
+ *
1877
+ * @remarks A piece with no method for a chat input or context menu command denies it. For `autocomplete` and
1878
+ * `interactionHandler` it is skipped instead, since a check written for the commands says nothing about their
1879
+ * autocomplete or about the components of the bot.
1880
+ * @since 6.3.0
1881
+ */
1882
+ export type PreconditionKind = 'chatInput' | 'contextMenu' | 'autocomplete' | 'interactionHandler';
1883
+ /**
1884
+ * The piece a precondition guards: the command, or the interaction handler.
1885
+ *
1886
+ * @since 6.3.0
1887
+ */
1888
+ export type PreconditionTarget = Command | InteractionHandler;
1889
+ /**
1890
+ * The interactions a precondition can be run for.
1891
+ *
1892
+ * @since 6.3.0
1893
+ */
1894
+ export type PreconditionInteraction = Command.ApplicationCommandInteraction | Command.AutocompleteInteraction | InteractionHandler.Interaction;
1895
+ /**
1896
+ * A precondition written as a function, for a check that does not need a piece of its own. In the `preconditions` option
1897
+ * of a command it is used for chat input and context menu commands, not for their autocomplete. In the option of an
1898
+ * interaction handler it is used for the handler.
1899
+ *
1900
+ * @since 6.3.0
1901
+ * @example
1902
+ * ```typescript
1903
+ * import { err, ok } from '@sapphire/result';
1904
+ * import { Command, PreconditionError, type PreconditionFunction } from '@wolfstar/http-framework';
1905
+ *
1906
+ * const OwnerOnly: PreconditionFunction = (interaction) =>
1907
+ * interaction.user.id === process.env.OWNER_ID
1908
+ * ? ok()
1909
+ * : err(new PreconditionError({ precondition: 'OwnerOnly', message: 'Only the owner can use this command.' }));
1910
+ *
1911
+ * export class UserCommand extends Command {
1912
+ * public constructor(context: Command.LoaderContext, options: Command.Options) {
1913
+ * super(context, { ...options, preconditions: [OwnerOnly] });
1914
+ * }
1915
+ * }
1916
+ * ```
1917
+ */
1918
+ export type PreconditionFunction<Interaction = Command.ApplicationCommandInteraction, Target = Command> = (interaction: Interaction, target: Target) => PreconditionResult;
1919
+ /**
1920
+ * The name of a {@link Precondition} piece, with the context to give it.
1921
+ *
1922
+ * @since 6.3.0
1923
+ */
1924
+ export interface PreconditionDetails {
1925
+ /**
1926
+ * The name of the precondition in the `preconditions` store.
1927
+ * @since 6.3.0
1928
+ */
1929
+ name: string;
1930
+ /**
1931
+ * The context to give to the precondition.
1932
+ * @since 6.3.0
1933
+ * @default {}
1934
+ */
1935
+ context?: PreconditionContext;
1936
+ }
1937
+ /**
1938
+ * An entry of the `preconditions` option of a command: the name of a {@link Precondition} piece, that name with a
1939
+ * context, or a {@link PreconditionFunction}.
1940
+ *
1941
+ * @since 6.3.0
1942
+ */
1943
+ export type PreconditionResolvable<Function = PreconditionFunction> = string | PreconditionDetails | Function;
1944
+ /**
1945
+ * How the entries of an array of preconditions are run.
1946
+ *
1947
+ * @since 6.3.0
1948
+ */
1949
+ export declare enum PreconditionRunMode {
1950
+ /**
1951
+ * The entries are run one after the other. This is the default, and it stops as soon as the result is known.
1952
+ * @since 6.3.0
1953
+ */
1954
+ Sequential = 0,
1955
+ /**
1956
+ * Every entry is started at once and awaited with `Promise.all`, then the results are read. It is faster when the
1957
+ * checks are slow, but none of them is skipped once another one has settled the result.
1958
+ * @since 6.3.0
1959
+ */
1960
+ Parallel = 1
1961
+ }
1962
+ /**
1963
+ * How the results of the entries of an array of preconditions are combined.
1964
+ *
1965
+ * @since 6.3.0
1966
+ */
1967
+ export declare enum PreconditionRunCondition {
1968
+ /**
1969
+ * Every entry has to pass: the first `Err` is the result.
1970
+ * @since 6.3.0
1971
+ */
1972
+ And = 0,
1973
+ /**
1974
+ * One entry has to pass: the first `Ok` is the result, and when none does, the last `Err`.
1975
+ * @since 6.3.0
1976
+ */
1977
+ Or = 1
1978
+ }
1979
+ /**
1980
+ * An array of preconditions with the {@link PreconditionRunMode} to run it with.
1981
+ *
1982
+ * @since 6.3.0
1983
+ */
1984
+ export interface PreconditionArrayDetails<Function = PreconditionFunction> {
1985
+ /**
1986
+ * The entries of the array.
1987
+ * @since 6.3.0
1988
+ */
1989
+ entries: readonly PreconditionEntryResolvable<Function>[];
1990
+ /**
1991
+ * The mode to run the array and the arrays nested in it with.
1992
+ * @since 6.3.0
1993
+ */
1994
+ mode: PreconditionRunMode;
1995
+ }
1996
+ /**
1997
+ * An array of preconditions: a list of entries, or a list with a {@link PreconditionRunMode}.
1998
+ *
1999
+ * @since 6.3.0
2000
+ */
2001
+ export type PreconditionArrayResolvable<Function = PreconditionFunction> = readonly PreconditionEntryResolvable<Function>[] | PreconditionArrayDetails<Function>;
2002
+ /**
2003
+ * An entry of an array of preconditions: a single precondition, or a nested array.
2004
+ *
2005
+ * @since 6.3.0
2006
+ */
2007
+ export type PreconditionEntryResolvable<Function = PreconditionFunction> = PreconditionResolvable<Function> | PreconditionArrayResolvable<Function>;
2008
+ /**
2009
+ * Runs a {@link Precondition} piece for the given kind of interaction.
2010
+ *
2011
+ * @param precondition The precondition to run.
2012
+ * @param kind What is being run.
2013
+ * @param interaction The interaction being handled.
2014
+ * @param target The command or interaction handler being run.
2015
+ * @param context The context of the check.
2016
+ * @returns The result of the precondition. A chat input or context menu command is denied when the piece has no method
2017
+ * for it, an autocomplete or an interaction handler is let through.
2018
+ * @since 6.3.0
2019
+ */
2020
+ export declare function runPrecondition(precondition: Precondition, kind: PreconditionKind, interaction: PreconditionInteraction, target: PreconditionTarget, context?: PreconditionContext): PreconditionResult;
2021
+ /**
2022
+ * Runs the preconditions of a command or an interaction handler: first the global ones of the `preconditions` store,
2023
+ * then the entries of its `preconditions` option, stopping at the first denial.
2024
+ *
2025
+ * @remarks
2026
+ * - The `preconditions` option is an array that runs with {@link PreconditionRunCondition.And}: every entry has to pass.
2027
+ * An array nested in it runs with `Or`, one nested in that with `And` again, and so on, so
2028
+ * `['Connect', ['Moderator', ['DJ', 'SongAuthor']]]` is `Connect && (Moderator || (DJ && SongAuthor))`.
2029
+ * - The {@link PreconditionRunMode} is `Sequential`, or the `mode` of the array written as `{ entries, mode }`, which the
2030
+ * arrays nested in it inherit.
2031
+ * - An entry that names a precondition missing from the store denies the interaction with
2032
+ * {@linkcode Identifiers.PreconditionUnavailable}.
2033
+ * - An entry that does not apply to the interaction, such as a function for an autocomplete or a piece with no method for
2034
+ * it, is left out of the result. An array in which no entry applies is left out as well.
2035
+ * @param target The command or interaction handler being run.
2036
+ * @param kind What is being run.
2037
+ * @param interaction The interaction being handled.
2038
+ * @returns The first `Err`, or `Result.ok()` when every precondition lets it run.
2039
+ * @since 6.3.0
2040
+ */
2041
+ export declare function runPreconditions(target: PreconditionTarget, kind: PreconditionKind, interaction: PreconditionInteraction): Promise<Result<unknown, UserError>>;
2042
+ //#endregion
1581
2043
  //#region src/lib/structures/Command.d.ts
1582
2044
  /**
1583
2045
  * The options a command receives, by the full path of the command (`'ping'`, `'stickyroles add'`,
@@ -1691,29 +2153,20 @@ export declare namespace Command {
1691
2153
  type LoaderContext = Piece$1.LoaderContext<'commands'>;
1692
2154
  type JSON = Piece$1.JSON;
1693
2155
  type LocationJSON = Piece$1.LocationJSON;
1694
- type Options = Piece$1.Options;
1695
- }
1696
- //#endregion
1697
- //#region src/lib/structures/InteractionHandler.d.ts
1698
- export declare abstract class InteractionHandler<Options extends InteractionHandler.Options = InteractionHandler.Options> extends Piece$1<Options, 'interaction-handlers'> {
1699
- constructor(context: InteractionHandler.LoaderContext, options?: Options);
1700
- abstract run(interaction: InteractionHandler.Interaction, customIdValue: unknown): Awaited<unknown>;
1701
- }
1702
- export declare namespace InteractionHandler {
1703
- type ButtonInteraction = Interactions.MessageComponentButton;
1704
- type SelectMenuInteraction = Interactions.MessageComponentSelectMenu;
1705
- type ModalInteraction = Interactions.ModalSubmit;
1706
- type MessageComponentInteraction = Interactions.MessageComponent;
1707
- type ContextMenuInteraction = Interactions.ContextMenuCommand;
1708
- type ApplicationCommandInteraction = Interactions.ApplicationCommand;
1709
- type Interaction = Interactions.MessageComponent;
1710
- type InteractionData = Interaction['data'];
1711
- /** @deprecated Use {@linkcode LoaderContext} instead. */
1712
- type Context = LoaderContext;
1713
- type LoaderContext = Piece$1.LoaderContext<'interaction-handlers'>;
1714
- type JSON = Piece$1.JSON;
1715
- type LocationJSON = Piece$1.LocationJSON;
1716
- type Options = Piece$1.Options;
2156
+ interface Options extends Piece$1.Options {
2157
+ /**
2158
+ * The checks to run, in order, before a chat input or context menu method of the command, after the global
2159
+ * preconditions. An entry is the name of a `Precondition` piece, that name with a `context`, a function, or an
2160
+ * array of entries: the list is an `And`, a nested array an `Or`, and one nested in that an `And` again. The list
2161
+ * can also be `{ entries, mode }` to run in parallel. The first denial stops the command: the method is not run
2162
+ * and the error is emitted as `chatInputCommandDenied` or `contextMenuCommandDenied`. The autocomplete of the
2163
+ * command only runs the pieces named here that have an `autocompleteRun` method, not the functions.
2164
+ *
2165
+ * @since 6.3.0
2166
+ * @default []
2167
+ */
2168
+ preconditions?: PreconditionArrayResolvable;
2169
+ }
1717
2170
  }
1718
2171
  //#endregion
1719
2172
  //#region src/lib/types/Enums.d.ts
@@ -1761,19 +2214,29 @@ export declare enum Events {
1761
2214
  CommandNameMissing = "commandNameMissing",
1762
2215
  CommandNameUnknown = "commandNameUnknown",
1763
2216
  CommandMethodUnknown = "commandMethodUnknown",
2217
+ PreChatInputCommandRun = "preChatInputCommandRun",
2218
+ PreContextMenuCommandRun = "preContextMenuCommandRun",
2219
+ ChatInputCommandAccepted = "chatInputCommandAccepted",
2220
+ ContextMenuCommandAccepted = "contextMenuCommandAccepted",
1764
2221
  CommandRun = "commandRun",
1765
2222
  CommandSuccess = "commandSuccess",
1766
2223
  CommandError = "commandError",
2224
+ ChatInputCommandDenied = "chatInputCommandDenied",
2225
+ ContextMenuCommandDenied = "contextMenuCommandDenied",
1767
2226
  CommandFinish = "commandFinish",
2227
+ AutocompleteAccepted = "autocompleteAccepted",
1768
2228
  AutocompleteRun = "autocompleteRun",
1769
2229
  AutocompleteSuccess = "autocompleteSuccess",
1770
2230
  AutocompleteError = "autocompleteError",
2231
+ AutocompleteDenied = "autocompleteDenied",
1771
2232
  AutocompleteFinish = "autocompleteFinish",
1772
2233
  InteractionHandlerNameInvalid = "interactionHandlerNameInvalid",
1773
2234
  InteractionHandlerNameUnknown = "interactionHandlerNameUnknown",
2235
+ InteractionHandlerAccepted = "interactionHandlerAccepted",
1774
2236
  InteractionHandlerRun = "interactionHandlerRun",
1775
2237
  InteractionHandlerSuccess = "interactionHandlerSuccess",
1776
2238
  InteractionHandlerError = "interactionHandlerError",
2239
+ InteractionHandlerDenied = "interactionHandlerDenied",
1777
2240
  InteractionHandlerFinish = "interactionHandlerFinish",
1778
2241
  HmrStart = "hmrStart",
1779
2242
  HmrStop = "hmrStop",
@@ -1800,19 +2263,98 @@ export interface ClientEvents {
1800
2263
  commandNameMissing: [interaction: APIApplicationCommandAutocompleteInteraction, response: ServerResponse];
1801
2264
  commandNameUnknown: [interaction: APIApplicationCommandInteraction | APIApplicationCommandAutocompleteInteraction, response: ServerResponse];
1802
2265
  commandMethodUnknown: [context: ClientEventCommandContext];
2266
+ /**
2267
+ * Emitted for a chat input command before its preconditions run, then followed by either
2268
+ * {@link ClientEvents.chatInputCommandDenied} or {@link ClientEvents.chatInputCommandAccepted}.
2269
+ *
2270
+ * @since 6.3.0
2271
+ */
2272
+ preChatInputCommandRun: [context: ClientEventCommandContext];
2273
+ /**
2274
+ * Emitted for a user or message context menu command before its preconditions run, then followed by either
2275
+ * {@link ClientEvents.contextMenuCommandDenied} or {@link ClientEvents.contextMenuCommandAccepted}.
2276
+ *
2277
+ * @since 6.3.0
2278
+ */
2279
+ preContextMenuCommandRun: [context: ClientEventCommandContext];
2280
+ /**
2281
+ * Emitted when a chat input command passes every precondition, right before {@link ClientEvents.commandRun}.
2282
+ *
2283
+ * @since 6.3.0
2284
+ */
2285
+ chatInputCommandAccepted: [context: ClientEventCommandContext];
2286
+ /**
2287
+ * Emitted when a context menu command passes every precondition, right before {@link ClientEvents.commandRun}.
2288
+ *
2289
+ * @since 6.3.0
2290
+ */
2291
+ contextMenuCommandAccepted: [context: ClientEventCommandContext];
2292
+ /**
2293
+ * Emitted right before the method of a command runs. Since 6.3.0 that is after its preconditions passed, so a
2294
+ * denied command does not emit it.
2295
+ */
1803
2296
  commandRun: [context: ClientEventCommandContext];
1804
2297
  commandSuccess: [context: ClientEventCommandContext, value: unknown];
2298
+ /**
2299
+ * Emitted when a command throws something that is not a {@link UserError}. A {@link UserError} is emitted as
2300
+ * {@link ClientEvents.chatInputCommandDenied} or {@link ClientEvents.contextMenuCommandDenied} instead.
2301
+ *
2302
+ * @remarks Since 6.3.0 a thrown {@link UserError} (such as the `PreconditionError` of `@wolfstar/decorators`) no
2303
+ * longer reaches this event.
2304
+ */
1805
2305
  commandError: [error: unknown, context: ClientEventCommandContext];
2306
+ /**
2307
+ * Emitted when a chat input command throws a {@link UserError}, an expected failure that is meant to be shown to the
2308
+ * user. The `error` event is still emitted, and the HTTP response is produced as for any other thrown error
2309
+ * (`httpReplyOnError`).
2310
+ *
2311
+ * @since 6.3.0
2312
+ */
2313
+ chatInputCommandDenied: [error: UserError, context: ClientEventCommandContext];
2314
+ /**
2315
+ * Emitted when a user or message context menu command throws a {@link UserError}. See
2316
+ * {@link ClientEvents.chatInputCommandDenied}.
2317
+ *
2318
+ * @since 6.3.0
2319
+ */
2320
+ contextMenuCommandDenied: [error: UserError, context: ClientEventCommandContext];
1806
2321
  commandFinish: [context: ClientEventCommandContext];
2322
+ /**
2323
+ * Emitted when an autocomplete interaction passes every precondition, right before
2324
+ * {@link ClientEvents.autocompleteRun}.
2325
+ *
2326
+ * @since 6.3.0
2327
+ */
2328
+ autocompleteAccepted: [context: ClientEventAutocompleteContext];
1807
2329
  autocompleteRun: [context: ClientEventAutocompleteContext];
1808
2330
  autocompleteSuccess: [context: ClientEventAutocompleteContext, value: unknown];
1809
2331
  autocompleteError: [error: unknown, context: ClientEventAutocompleteContext];
2332
+ /**
2333
+ * Emitted when an autocomplete handler throws a {@link UserError}, instead of {@link ClientEvents.autocompleteError}.
2334
+ *
2335
+ * @since 6.3.0
2336
+ */
2337
+ autocompleteDenied: [error: UserError, context: ClientEventAutocompleteContext];
1810
2338
  autocompleteFinish: [context: ClientEventAutocompleteContext];
1811
2339
  interactionHandlerNameInvalid: [interaction: APIMessageComponentInteraction | APIModalSubmitInteraction, response: ServerResponse];
1812
2340
  interactionHandlerNameUnknown: [interaction: APIMessageComponentInteraction | APIModalSubmitInteraction, response: ServerResponse];
2341
+ /**
2342
+ * Emitted when an interaction handler passes every precondition, right before
2343
+ * {@link ClientEvents.interactionHandlerRun}.
2344
+ *
2345
+ * @since 6.3.0
2346
+ */
2347
+ interactionHandlerAccepted: [context: ClientEventInteractionHandlerContext];
1813
2348
  interactionHandlerRun: [context: ClientEventInteractionHandlerContext];
1814
2349
  interactionHandlerSuccess: [context: ClientEventInteractionHandlerContext, value: unknown];
1815
2350
  interactionHandlerError: [error: unknown, context: ClientEventInteractionHandlerContext];
2351
+ /**
2352
+ * Emitted when an interaction handler throws a {@link UserError}, instead of
2353
+ * {@link ClientEvents.interactionHandlerError}.
2354
+ *
2355
+ * @since 6.3.0
2356
+ */
2357
+ interactionHandlerDenied: [error: UserError, context: ClientEventInteractionHandlerContext];
1816
2358
  interactionHandlerFinish: [context: ClientEventInteractionHandlerContext];
1817
2359
  /**
1818
2360
  * Emitted when the Hot Module Reloader starts watching, with the list of watched store paths.
@@ -2158,6 +2700,7 @@ export declare class PluginManager {
2158
2700
  //#endregion
2159
2701
  //#region src/lib/structures/InteractionHandlerStore.d.ts
2160
2702
  export declare class InteractionHandlerStore extends Store$1<InteractionHandler, 'interaction-handlers'> {
2703
+ #private;
2161
2704
  constructor();
2162
2705
  runHandler(response: ServerResponse, interaction: APIMessageComponentInteraction | APIModalSubmitInteraction): Promise<ServerResponse>;
2163
2706
  }
@@ -2195,6 +2738,27 @@ export declare class ListenerStore extends Store$1<Listener, 'listeners'> {
2195
2738
  constructor();
2196
2739
  }
2197
2740
  //#endregion
2741
+ //#region src/lib/structures/PreconditionStore.d.ts
2742
+ export declare class PreconditionStore extends Store$1<Precondition, 'preconditions'> {
2743
+ #private;
2744
+ constructor();
2745
+ /**
2746
+ * Runs the global preconditions, the ones with a `position`, in ascending order of it, stopping at the first one
2747
+ * that denies the interaction.
2748
+ *
2749
+ * @since 6.3.0
2750
+ * @param kind What is being run.
2751
+ * @param interaction The interaction being handled.
2752
+ * @param target The command or interaction handler being run.
2753
+ * @param context The context of the checks.
2754
+ * @returns The first `Err`, or `Result.ok()` when every global precondition lets the command run.
2755
+ */
2756
+ run(kind: PreconditionKind, interaction: PreconditionInteraction, target: PreconditionTarget, context?: PreconditionContext): Promise<Result<unknown, UserError>>;
2757
+ set(key: string, value: Precondition): this;
2758
+ delete(key: string): boolean;
2759
+ clear(): void;
2760
+ }
2761
+ //#endregion
2198
2762
  //#region src/lib/utils/logger/ILogger.d.ts
2199
2763
  /**
2200
2764
  * The available log levels, ordered from the most verbose to the least. A {@link ILogger} only writes the entries
@@ -2485,6 +3049,7 @@ declare module '@sapphire/pieces' {
2485
3049
  commands: CommandStore;
2486
3050
  'interaction-handlers': InteractionHandlerStore;
2487
3051
  listeners: ListenerStore;
3052
+ preconditions: PreconditionStore;
2488
3053
  }
2489
3054
  interface Container {
2490
3055
  client: Client;
@@ -2879,72 +3444,14 @@ export declare class StringIdParser implements IIdParser {
2879
3444
  run(customId: string): IdParserRead | null;
2880
3445
  }
2881
3446
  //#endregion
2882
- //#region src/lib/errors/UserError.d.ts
2883
- /**
2884
- * The UserError class to be thrown and emitted by the pieces of the framework.
2885
- *
2886
- * @remarks
2887
- * This class is the root of every framework-thrown error, allowing consumers to distinguish
2888
- * errors that are meant to be shown to the user from unexpected, internal ones.
2889
- *
2890
- * @property name This will be `'UserError'` and can be used to distinguish the type of error when any error gets thrown.
2891
- * @since 3.2.0
2892
- * @example
2893
- * ```typescript
2894
- * throw new UserError({
2895
- * identifier: Identifiers.ArgumentIntegerTooSmall,
2896
- * message: 'The number you provided is too small.',
2897
- * context: { received: 2, minimum: 3 }
2898
- * });
2899
- * ```
2900
- */
2901
- export declare class UserError extends Error {
2902
- /**
2903
- * An identifier, useful to localize emitted errors.
2904
- * @since 3.2.0
2905
- */
2906
- readonly identifier: string;
2907
- /**
2908
- * User-provided context.
2909
- * @since 3.2.0
2910
- */
2911
- readonly context: unknown;
2912
- /**
2913
- * Constructs an UserError.
2914
- * @param options The UserError options
2915
- */
2916
- constructor(options: UserError.Options);
2917
- get name(): string;
2918
- }
2919
- export declare namespace UserError {
2920
- /**
2921
- * The options for {@link UserError}.
2922
- * @since 3.2.0
2923
- */
2924
- interface Options {
2925
- /**
2926
- * The identifier for this error.
2927
- * @since 3.2.0
2928
- */
2929
- identifier: string;
2930
- /**
2931
- * The message to be passed to the Error constructor.
2932
- * @since 3.2.0
2933
- */
2934
- message?: string;
2935
- /**
2936
- * The extra context to provide more information about this error.
2937
- * @since 3.2.0
2938
- * @default null
2939
- */
2940
- context?: unknown;
2941
- }
2942
- }
2943
- //#endregion
2944
3447
  //#region src/lib/errors/ArgumentError.d.ts
2945
3448
  /**
2946
3449
  * Errors thrown while resolving the options (arguments) of an interaction.
2947
3450
  *
3451
+ * @remarks The framework never throws this error by itself: Discord validates the type and the bounds of the options
3452
+ * before sending the interaction. It is meant to be thrown from a command that rejects a value, and, being a
3453
+ * {@link UserError}, it is emitted as a `*Denied` event.
3454
+ *
2948
3455
  * @property name This will be `'ArgumentError'` and can be used to distinguish the type of error when any error gets thrown.
2949
3456
  * @since 3.2.0
2950
3457
  * @example
@@ -3085,6 +3592,8 @@ export declare enum Identifiers {
3085
3592
  PreconditionClientPermissionsNoPermissions = "preconditionClientPermissionsNoPermissions",
3086
3593
  PreconditionCooldown = "preconditionCooldown",
3087
3594
  PreconditionGuildIds = "preconditionGuildIds",
3595
+ PreconditionMissingChatInputHandler = "preconditionMissingChatInputHandler",
3596
+ PreconditionMissingContextMenuHandler = "preconditionMissingContextMenuHandler",
3088
3597
  PreconditionNSFW = "preconditionNsfw",
3089
3598
  PreconditionRunIn = "preconditionRunIn",
3090
3599
  PreconditionUnavailable = "preconditionUnavailable",
@@ -3096,53 +3605,6 @@ export declare enum Identifiers {
3096
3605
  ChatInputRouterSubcommandLinkInvalid = "chatInputRouterSubcommandLinkInvalid"
3097
3606
  }
3098
3607
  //#endregion
3099
- //#region src/lib/errors/PreconditionError.d.ts
3100
- /**
3101
- * Errors thrown by preconditions, that is, any check that runs before a command or an interaction
3102
- * handler is executed, such as the guild restrictions applied by
3103
- * {@link RestrictGuildIds}.
3104
- *
3105
- * @property name This will be `'PreconditionError'` and can be used to distinguish the type of error when any error gets thrown.
3106
- * @since 3.2.0
3107
- * @example
3108
- * ```typescript
3109
- * throw new PreconditionError({
3110
- * precondition: 'GuildIds',
3111
- * identifier: Identifiers.PreconditionGuildIds,
3112
- * message: 'This command can only be used in specific guilds.',
3113
- * context: { guildIds: ['737141877803057244'] }
3114
- * });
3115
- * ```
3116
- */
3117
- export declare class PreconditionError extends UserError {
3118
- /**
3119
- * The name of the precondition that caused the error.
3120
- * @since 3.2.0
3121
- */
3122
- readonly precondition: string;
3123
- constructor(options: PreconditionError.Options);
3124
- get name(): string;
3125
- }
3126
- export declare namespace PreconditionError {
3127
- /**
3128
- * The options for {@link PreconditionError}.
3129
- * @since 3.2.0
3130
- */
3131
- interface Options extends Omit<UserError.Options, 'identifier'> {
3132
- /**
3133
- * The name of the precondition that caused the error.
3134
- * @since 3.2.0
3135
- */
3136
- precondition: string;
3137
- /**
3138
- * The identifier.
3139
- * @since 3.2.0
3140
- * @default precondition
3141
- */
3142
- identifier?: string;
3143
- }
3144
- }
3145
- //#endregion
3146
3608
  //#region src/lib/structures/CommandLoaderStrategy.d.ts
3147
3609
  /**
3148
3610
  * Represents a strategy for loading and unloading commands.
@@ -3158,7 +3620,7 @@ export declare class CommandLoaderStrategy extends LoaderStrategy<Command> {
3158
3620
  * @param piece - The command being loaded.
3159
3621
  * @returns The loaded command.
3160
3622
  */
3161
- onLoad(store: CommandStore, piece: Command): Command<import("@sapphire/pieces").PieceOptions>;
3623
+ onLoad(store: CommandStore, piece: Command): Command<Command.Options>;
3162
3624
  /**
3163
3625
  * Called when a command is unloaded.
3164
3626
  *
@@ -3171,7 +3633,7 @@ export declare class CommandLoaderStrategy extends LoaderStrategy<Command> {
3171
3633
  * @param piece - The command being unloaded.
3172
3634
  * @returns The unloaded command.
3173
3635
  */
3174
- onUnload(store: CommandStore, piece: Command): Command<import("@sapphire/pieces").PieceOptions>;
3636
+ onUnload(store: CommandStore, piece: Command): Command<Command.Options>;
3175
3637
  }
3176
3638
  //#endregion
3177
3639
  //#region src/lib/structures/ListenerLoaderStrategy.d.ts