Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wildcard components #188

Merged
merged 7 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,14 @@ export class SlashCreator extends (EventEmitter as any as new () => TypedEventEm

const componentCallbackKey = `${ctx.message.id}-${ctx.customID}`;
const globalCallbackKey = `global-${ctx.customID}`;
const wildcardCallbackKey = `${ctx.message.id}-*`;

if (this._componentCallbacks.has(componentCallbackKey))
return this._componentCallbacks.get(componentCallbackKey)!.callback(ctx);
if (this._componentCallbacks.has(globalCallbackKey))
return this._componentCallbacks.get(globalCallbackKey)!.callback(ctx);
if (this._componentCallbacks.has(wildcardCallbackKey))
return this._componentCallbacks.get(wildcardCallbackKey)!.callback(ctx);
break;
} else
return respond({
Expand Down
33 changes: 31 additions & 2 deletions src/structures/interfaces/messageInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,6 @@ export class MessageInteractionContext {
onExpired?: () => void
) {
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');

this.creator._componentCallbacks.set(`${message_id}-${custom_id}`, {
callback,
Expand All @@ -321,6 +319,37 @@ export class MessageInteractionContext {
}
return this.creator._componentCallbacks.delete(`${message_id}-${custom_id}`);
}

/**
* Registers a wildcard component callback on a message.
* This unregisters automatically when the context expires.
* @param message_id The message ID of the component to register
* @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.
*/
registerWildcardComponent(
message_id: string,
callback: ComponentRegisterCallback,
expiration: number = 1000 * 60 * 15,
onExpired?: () => void
) {
if (this.expired) throw new Error('This interaction has expired');

this.creator._componentCallbacks.set(`${message_id}-*`, {
callback,
expires: expiration != null ? this.invokedAt + expiration : undefined,
onExpired
});
}

/**
* Unregisters a component callback.
* @param message_id The message ID of the component to unregister, defaults to the invoking message ID.
*/
unregisterWildcardComponent(message_id: string) {
return this.creator._componentCallbacks.delete(`${message_id}-*`);
}
}

/** The options for {@link MessageInteractionContext#edit}. */
Expand Down