Skip to content

Commit

Permalink
Added unref callback return to register component methods on MessageI…
Browse files Browse the repository at this point in the history
…nteractionContext

* registerComponent
* registerComponentFrom
* registerWildcardComponent
  • Loading branch information
sudojunior authored Nov 24, 2021
1 parent 9cdd970 commit b7a4169
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/structures/interfaces/messageInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,14 @@ export class MessageInteractionContext {
* @param callback The callback to use on interaction
* @param expiration The expiration time of the callback in milliseconds. Use null for no expiration (Although, in this case, global components might be more consistent).
* @param onExpired A function to be called when the component expires.
* @returns A function to unregister the component.
*/
registerComponent(
custom_id: string,
callback: ComponentRegisterCallback,
expiration: number = 1000 * 60 * 15,
onExpired?: () => void
) {
): ComponentUnregisterCallback {
if (this.expired) throw new Error('This interaction has expired');
if (!this.initiallyResponded || this.deferred)
throw new Error('You must send a message before registering components');
Expand All @@ -280,6 +281,8 @@ export class MessageInteractionContext {
expires: expiration != null ? this.invokedAt + expiration : undefined,
onExpired
});

return () => this.unregisterComponent(custom_id);
}

/**
Expand All @@ -290,14 +293,15 @@ export class MessageInteractionContext {
* @param callback The callback to use on interaction
* @param expiration The expiration time of the callback in milliseconds. Use null for no expiration (Although, in this case, global components might be more consistent).
* @param onExpired A function to be called when the component expires.
* @returns A function to unregister the component.
*/
registerComponentFrom(
message_id: string,
custom_id: string,
callback: ComponentRegisterCallback,
expiration: number = 1000 * 60 * 15,
onExpired?: () => void
) {
): ComponentUnregisterCallback {
if (this.expired) throw new Error('This interaction has expired');
if (!this.initiallyResponded || this.deferred)
throw new Error('You must send a message before registering components');
Expand All @@ -307,6 +311,8 @@ export class MessageInteractionContext {
expires: expiration != null ? this.invokedAt + expiration : undefined,
onExpired
});

return () => this.unregisterComponent(custom_id, message_id);
}

/**
Expand All @@ -329,13 +335,14 @@ export class MessageInteractionContext {
* @param callback The callback to use on interaction
* @param expiration The expiration time of the callback in milliseconds. Use null for no expiration (Although, in this case, global components might be more consistent).
* @param onExpired A function to be called when the component expires.
* @returns A function to unregister the component
*/
registerWildcardComponent(
message_id: string,
callback: ComponentRegisterCallback,
expiration: number = 1000 * 60 * 15,
onExpired?: () => void
) {
): ComponentUnregisterCallback {
if (this.expired) throw new Error('This interaction has expired');
if (!this.initiallyResponded || this.deferred)
throw new Error('You must send a message before registering components');
Expand All @@ -345,6 +352,8 @@ export class MessageInteractionContext {
expires: expiration != null ? this.invokedAt + expiration : undefined,
onExpired
});

return () => this.unregisterWildcardComponent(message_id);
}

/**
Expand All @@ -361,6 +370,9 @@ export class MessageInteractionContext {
}
}

/** A function to unregister a component callback, returns the boolean result from the method called. */
export type ComponentUnregisterCallback = () => boolean;

/** The options for {@link MessageInteractionContext#edit}. */
export interface EditMessageOptions {
/** The message content. */
Expand Down

0 comments on commit b7a4169

Please sign in to comment.