Skip to content

Commit

Permalink
feat(PaginatedMessage): add support for per-page actions (#587)
Browse files Browse the repository at this point in the history
* feat(PaginatedMessage): add support for per-page actions

* fix(LazyPaginatedMessage): remove @returns line

* refactor: improve docs and allow including default actions with `setPageActions`

* docs: improve typedoc

* fix(PaginatedMessage): properly add page actions

* fix(PaginatedMessage): remove default actions from `setPageActions`

* fix(PaginatedMessage): properly defer interactions

* fix(PaginatedMessage): use previously resolved components in `updateCurrentPage`

* fix(PaginatedMessage): revert message edit change in `handleCollect`

* feat(PaginatedMessage): apply new components to updated page

* fix(PaginatedMessage): add `pageActions` to `clone`

---------

Co-authored-by: Jeroen Claassens <support@favware.tech>
  • Loading branch information
killbasa and favna committed May 4, 2023
1 parent db13068 commit b917236
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 79 deletions.
@@ -1,6 +1,7 @@
import { isFunction } from '@sapphire/utilities';
import { EmbedBuilder } from 'discord.js';
import { EmbedBuilder, Message, User } from 'discord.js';
import { MessageBuilder } from '../builders/MessageBuilder';
import type { AnyInteractableInteraction } from '../utility-types';
import { PaginatedMessage } from './PaginatedMessage';
import type { PaginatedMessagePage } from './PaginatedMessageTypes';

Expand All @@ -11,18 +12,24 @@ export class LazyPaginatedMessage extends PaginatedMessage {
/**
* Only resolves the page corresponding with the handler's current index.
*/
public override async resolvePagesOnRun(): Promise<void> {
await this.resolvePage(this.index);
public override async resolvePagesOnRun(messageOrInteraction: Message | AnyInteractableInteraction, target: User): Promise<void> {
await this.resolvePage(messageOrInteraction, target, this.index);
}

/**
* Resolves the page corresponding with the given index. This also resolves the index's before and after the given index.
* @param messageOrInteraction The message or interaction that triggered this {@link LazyPaginatedMessage}.
* @param target The user who will be able to interact with the buttons of this {@link LazyPaginatedMessage}.
* @param index The index to resolve. Defaults to handler's current index.
*/
public override async resolvePage(index: number): Promise<PaginatedMessagePage> {
const promises = [super.resolvePage(index)];
if (this.hasPage(index - 1)) promises.push(super.resolvePage(index - 1));
if (this.hasPage(index + 1)) promises.push(super.resolvePage(index + 1));
public override async resolvePage(
messageOrInteraction: Message | AnyInteractableInteraction,
target: User,
index: number
): Promise<PaginatedMessagePage> {
const promises = [super.resolvePage(messageOrInteraction, target, index)];
if (this.hasPage(index - 1)) promises.push(super.resolvePage(messageOrInteraction, target, index - 1));
if (this.hasPage(index + 1)) promises.push(super.resolvePage(messageOrInteraction, target, index + 1));

const [result] = await Promise.all(promises);
return result;
Expand Down

0 comments on commit b917236

Please sign in to comment.