Skip to content

Commit a069d5f

Browse files
favnakyranet
andauthoredJun 20, 2023
feat(interaction-handlers): add events for parse return states (#641)
* feat(interaction-handlers): add events for parse return states * Update src/lib/types/Events.ts Co-authored-by: Aura Román <kyradiscord@gmail.com> --------- Co-authored-by: Aura Román <kyradiscord@gmail.com>
1 parent 33103ef commit a069d5f

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed
 

‎src/lib/structures/InteractionHandlerStore.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Store } from '@sapphire/pieces';
2-
import { Result } from '@sapphire/result';
2+
import { Result, type Option } from '@sapphire/result';
33
import type { Interaction } from 'discord.js';
44
import { Events } from '../types/Events';
55
import { InteractionHandler, InteractionHandlerTypes, type InteractionHandlerOptions } from './InteractionHandler';
@@ -27,13 +27,27 @@ export class InteractionHandlerStore extends Store<InteractionHandler> {
2727
const result = await Result.fromAsync(() => handler.parse(interaction));
2828
result.match({
2929
ok: (option) => {
30-
// If the `parse` method returned a `Some` (whatever that `Some`'s value is, it should be handled)
31-
option.inspect((value) => {
32-
// Schedule the run of the handler method
33-
const promise = Result.fromAsync(() => handler.run(interaction, value)) //
34-
.then((res) => res.mapErr((error) => ({ handler, error })));
30+
// Emit an event to the user to let them know `parse` was successful
31+
this.container.client.emit(Events.InteractionHandlerParseSuccess, option, { interaction, handler });
3532

36-
promises.push(promise);
33+
option.match({
34+
// If the `parse` method returned a `Some` (whatever that `Some`'s value is, it should be handled)
35+
some: (value) => {
36+
// Emit an event to the user to let them know parse was successful and `some` was returned.
37+
this.container.client.emit(Events.InteractionHandlerParseSome, option as Option.Some<typeof value>, {
38+
interaction,
39+
handler,
40+
value
41+
});
42+
43+
// Schedule the run of the handler method
44+
const promise = Result.fromAsync(() => handler.run(interaction, value)) //
45+
.then((res) => res.mapErr((error) => ({ handler, error })));
46+
47+
promises.push(promise);
48+
},
49+
// Emit an event to the user to let them know parse was successful and `none` was returned.
50+
none: () => this.container.client.emit(Events.InteractionHandlerParseNone, option as Option.None, { interaction, handler })
3751
});
3852
},
3953
err: (error) => {

‎src/lib/types/Events.ts

+34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Piece, Store } from '@sapphire/pieces';
2+
import type { Option } from '@sapphire/result';
23
import {
34
Events as DJSEvents,
45
type AutocompleteInteraction,
@@ -246,6 +247,25 @@ export const Events = {
246247
PluginLoaded: 'pluginLoaded' as const,
247248

248249
// Interaction handlers
250+
/**
251+
* Emitted when the `parse` method of an interaction handler passes successfully (no errors are encountered)
252+
* Use the {@link option} parameter to determine if `some` or `none` was passed.
253+
* @param {Option.None | Option.Some<unknown>} option The {@link Option} from the `parse` method.
254+
* @param {InteractionHandlerParseSuccess} payload The contextual payload
255+
*/
256+
InteractionHandlerParseSuccess: 'interactionHandlerParseSuccess' as const,
257+
/**
258+
* Emitted when the `parse` method of an interaction handler passes successfully (no errors are encountered) and `some` is returned.
259+
* @param {Option.Some<unknown>} option The {@link Option.Some} from the `parse` method.
260+
* @param {InteractionHandlerParseSome} payload The contextual payload
261+
*/
262+
InteractionHandlerParseSome: 'interactionHandlerParseSome' as const,
263+
/**
264+
* Emitted when the `parse` method of an interaction handler passes successfully (no errors are encountered) and `none` is returned.
265+
* @param {Option.None} option The {@link Option.None} from the `parse` method.
266+
* @param {InteractionHandlerParseNone} payload The contextual payload
267+
*/
268+
InteractionHandlerParseNone: 'interactionHandlerParseNone' as const,
249269
/**
250270
* Emitted when the `parse` method of an interaction handler encounters an error.
251271
* @param {*} error The error that was encountered
@@ -551,6 +571,17 @@ export interface IInteractionHandlerPayload {
551571
handler: InteractionHandler;
552572
}
553573

574+
export interface InteractionHandlerParseSuccess extends IInteractionHandlerPayload {}
575+
576+
export interface InteractionHandlerParseSome<T = unknown> extends IInteractionHandlerPayload {
577+
/**
578+
* The value that was passed to the `some` function.
579+
*/
580+
value: T;
581+
}
582+
583+
export interface InteractionHandlerParseNone extends IInteractionHandlerPayload {}
584+
554585
export interface InteractionHandlerParseError extends IInteractionHandlerPayload {}
555586

556587
export interface InteractionHandlerError extends IInteractionHandlerPayload {}
@@ -597,6 +628,9 @@ declare module 'discord.js' {
597628

598629
[SapphireEvents.PluginLoaded]: [hook: PluginHook, name: string | undefined];
599630

631+
[SapphireEvents.InteractionHandlerParseSuccess]: [option: Option<unknown>, payload: InteractionHandlerParseSuccess];
632+
[SapphireEvents.InteractionHandlerParseSome]: [option: Option.Some<unknown>, payload: InteractionHandlerParseSome];
633+
[SapphireEvents.InteractionHandlerParseNone]: [option: Option.None, payload: InteractionHandlerParseNone];
600634
[SapphireEvents.InteractionHandlerParseError]: [error: unknown, payload: InteractionHandlerParseError];
601635
[SapphireEvents.InteractionHandlerError]: [error: unknown, payload: InteractionHandlerError];
602636

0 commit comments

Comments
 (0)
Please sign in to comment.