From 8d04ec8720a96ccb190e9ef6b65fafe938ac4e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikuro=E3=81=95=E3=81=84=E3=81=AA?= Date: Sat, 14 Jan 2023 14:51:07 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E3=83=98=E3=83=AB=E3=83=97=E3=82=B3?= =?UTF-8?q?=E3=83=9E=E3=83=B3=E3=83=89=E3=81=AE=E3=83=9C=E3=82=BF=E3=83=B3?= =?UTF-8?q?=E3=82=92=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89=E7=99=BA=E8=A1=8C?= =?UTF-8?q?=E8=80=85=E3=81=97=E3=81=8B=E6=8A=BC=E3=81=9B=E3=81=AA=E3=81=84?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3=20(#666)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add ReplyPagesOptions * Specify usersCanPaginate --- src/adaptor/proxy/command.ts | 98 +++++++++++++++----------- src/service/command/command-message.ts | 18 ++++- src/service/command/help.ts | 4 +- 3 files changed, 75 insertions(+), 45 deletions(-) diff --git a/src/adaptor/proxy/command.ts b/src/adaptor/proxy/command.ts index 6fd806e0..30a11c01 100644 --- a/src/adaptor/proxy/command.ts +++ b/src/adaptor/proxy/command.ts @@ -15,6 +15,7 @@ import type { import { Schema, makeError } from '../../model/command-schema.js'; import type { EmbedPage } from '../../model/embed-message.js'; import type { RawMessage } from './middleware.js'; +import type { ReplyPagesOptions } from '../../service/command/command-message.js'; import type { Snowflake } from '../../model/id.js'; import { convertEmbed } from '../embed-convert.js'; import { parseStrings } from './command/schema.js'; @@ -132,50 +133,61 @@ const CONTROLS_DISABLED: APIActionRowComponent = const pagesFooter = (currentPage: number, pagesLength: number) => `ページ ${currentPage + 1}/${pagesLength}`; -const replyPages = (message: RawMessage) => async (pages: EmbedPage[]) => { - if (pages.length === 0) { - throw new Error('pages must not be empty array'); - } +const replyPages = + (message: RawMessage) => + async (pages: EmbedPage[], options?: ReplyPagesOptions) => { + if (pages.length === 0) { + throw new Error('pages must not be empty array'); + } + + const generatePage = (index: number) => + convertEmbed(pages[index]).setFooter({ + text: pagesFooter(index, pages.length) + }); + + const paginated = await message.reply({ + embeds: [generatePage(0)], + components: [CONTROLS] + }); - const generatePage = (index: number) => - convertEmbed(pages[index]).setFooter({ - text: pagesFooter(index, pages.length) + const collector = paginated.createMessageComponentCollector({ + time: ONE_MINUTE_MS }); - const paginated = await message.reply({ - embeds: [generatePage(0)], - components: [CONTROLS] - }); - - const collector = paginated.createMessageComponentCollector({ - time: ONE_MINUTE_MS - }); - - let currentPage = 0; - collector.on('collect', async (interaction) => { - switch (interaction.customId) { - case 'prev': - if (0 < currentPage) { - currentPage -= 1; - } else { - currentPage = pages.length - 1; - } - break; - case 'next': - if (currentPage < pages.length - 1) { - currentPage += 1; - } else { - currentPage = 0; - } - break; - default: + const isLimitedToPaginate = options?.usersCanPaginate !== undefined; + + let currentPage = 0; + collector.on('collect', async (interaction) => { + if ( + isLimitedToPaginate && + !options?.usersCanPaginate?.includes(interaction.user.id as Snowflake) + ) { return; - } - await interaction.update({ embeds: [generatePage(currentPage)] }); - }); - collector.on('end', async () => { - if (paginated.editable) { - await paginated.edit({ components: [CONTROLS_DISABLED] }); - } - }); -}; + } + + switch (interaction.customId) { + case 'prev': + if (0 < currentPage) { + currentPage -= 1; + } else { + currentPage = pages.length - 1; + } + break; + case 'next': + if (currentPage < pages.length - 1) { + currentPage += 1; + } else { + currentPage = 0; + } + break; + default: + return; + } + await interaction.update({ embeds: [generatePage(currentPage)] }); + }); + collector.on('end', async () => { + if (paginated.editable) { + await paginated.edit({ components: [CONTROLS_DISABLED] }); + } + }); + }; diff --git a/src/service/command/command-message.ts b/src/service/command/command-message.ts index 0c9910d0..97b1367a 100644 --- a/src/service/command/command-message.ts +++ b/src/service/command/command-message.ts @@ -3,6 +3,22 @@ import type { ParsedSchema, Schema } from '../../model/command-schema.js'; import type { Snowflake } from '../../model/id.js'; +/** + * `CommandMessage.replyPages` のオプション。 + * + * @export + * @interface ReplyPagesOptions + */ +export interface ReplyPagesOptions { + /** + * ページネーションのボタンを操作できるユーザの ID のリスト。 + * + * @type {readonly Snowflake[]} + * @memberof ReplyPagesOptions + */ + readonly usersCanPaginate?: readonly Snowflake[]; +} + /** * コマンド形式のメッセージの抽象。 * @@ -73,7 +89,7 @@ export interface CommandMessage { * @param pages * @memberof CommandMessage */ - replyPages(pages: EmbedPage[]): Promise; + replyPages(pages: EmbedPage[], options?: ReplyPagesOptions): Promise; /** * このメッセージに `emoji` の絵文字でリアクションする。 diff --git a/src/service/command/help.ts b/src/service/command/help.ts index 735d573e..c1027d05 100644 --- a/src/service/command/help.ts +++ b/src/service/command/help.ts @@ -29,7 +29,9 @@ export class HelpCommand implements CommandResponder { const pages: EmbedPage[] = helpAndSchema.map((helpScheme) => this.buildField(helpScheme) ); - await message.replyPages(pages); + await message.replyPages(pages, { + usersCanPaginate: [message.senderId] + }); } private buildField({