/[sudobot]/trunk/src/utils/Pagination.ts
ViewVC logotype

Diff of /trunk/src/utils/Pagination.ts

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 447 by rakin, Mon Jul 29 17:30:17 2024 UTC revision 543 by rakin, Mon Jul 29 17:30:44 2024 UTC
# Line 1  Line 1 
1    /**
2    * This file is part of SudoBot.
3    *
4    * Copyright (C) 2021-2022 OSN Inc.
5    *
6    * SudoBot is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Affero General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10    *
11    * SudoBot is distributed in the hope that it will be useful, but
12    * WITHOUT ANY WARRANTY; without even the implied warranty of
13    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    * GNU Affero General Public License for more details.
15    *
16    * You should have received a copy of the GNU Affero General Public License
17    * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18    */
19    
20  import DiscordClient from "../client/Client";  import DiscordClient from "../client/Client";
21  import MessageEmbed from "../client/MessageEmbed";  import MessageEmbed from "../client/MessageEmbed";
22  import { v4 as uuid } from 'uuid';  import { v4 as uuid } from 'uuid';
23  import { ButtonInteraction, InteractionCollector, InteractionReplyOptions, Message, MessageActionRow, MessageButton, MessageEditOptions, MessageOptions, ReplyMessageOptions } from "discord.js";  import { ButtonInteraction, InteractionCollector, InteractionReplyOptions, Message, MessageActionRow, MessageButton, MessageEditOptions, MessageOptions, ReplyMessageOptions } from "discord.js";
24    import { emoji } from "./Emoji";
25    
26  export interface EmbedBuilderOptions<T> {  export interface EmbedBuilderOptions<T> {
27      data: Array<T>;      data: Array<T>;
# Line 14  export interface PaginationOptions<T> { Line 34  export interface PaginationOptions<T> {
34      guild_id: string;      guild_id: string;
35      channel_id: string;      channel_id: string;
36      user_id?: string;      user_id?: string;
37        timeout?: number;
38      embedBuilder: (options: EmbedBuilderOptions<T>) => MessageEmbed;      embedBuilder: (options: EmbedBuilderOptions<T>) => MessageEmbed;
39      actionRowBuilder?: (options: { first: boolean, last: boolean, next: boolean, back: boolean }) => MessageActionRow<MessageButton>;      actionRowBuilder?: (options: { first: boolean, last: boolean, next: boolean, back: boolean }) => MessageActionRow<MessageButton>;
40  }  }
# Line 60  export default class Pagination<T> { Line 81  export default class Pagination<T> {
81    
82          options.embeds ??= [];          options.embeds ??= [];
83          options.embeds.push(this.getEmbed(page));          options.embeds.push(this.getEmbed(page));
84          console.log(options.embeds);          
85          options.components ??= [];          options.components ??= [];
86          options.components.push(this.getActionRow(actionRowOptionsDup));          options.components.push(this.getActionRow(actionRowOptionsDup));
87    
# Line 72  export default class Pagination<T> { Line 93  export default class Pagination<T> {
93              return this.options.actionRowBuilder({ first, last, next, back });              return this.options.actionRowBuilder({ first, last, next, back });
94          }          }
95    
96          const actionRow = new MessageActionRow<MessageButton>;          const actionRow = new MessageActionRow<MessageButton>();
97    
98          actionRow.addComponents(          actionRow.addComponents(
99              new MessageButton()              new MessageButton()
100                  .setCustomId(`pagination_first_${this.id}`)                  .setCustomId(`pagination_first_${this.id}`)
101                  .setStyle("PRIMARY")                  .setStyle("PRIMARY")
102                  .setLabel('⏪')                  .setDisabled(!first)
103                  .setDisabled(!first),                  .setEmoji(emoji('ArrowLeft')!),
104              new MessageButton()              new MessageButton()
105                  .setCustomId(`pagination_back_${this.id}`)                  .setCustomId(`pagination_back_${this.id}`)
106                  .setStyle("PRIMARY")                  .setStyle("PRIMARY")
107                  .setLabel('◀')                  .setDisabled(!back)
108                  .setDisabled(!back),                  .setEmoji(emoji('ChevronLeft')!),
109              new MessageButton()              new MessageButton()
110                  .setCustomId(`pagination_next_${this.id}`)                  .setCustomId(`pagination_next_${this.id}`)
111                  .setStyle("PRIMARY")                  .setStyle("PRIMARY")
112                  .setLabel('▶')                  .setDisabled(!next)
113                  .setDisabled(!next),                  .setEmoji(emoji('ChevronRight')!),
114              new MessageButton()              new MessageButton()
115                  .setCustomId(`pagination_last_${this.id}`)                  .setCustomId(`pagination_last_${this.id}`)
116                  .setStyle("PRIMARY")                  .setStyle("PRIMARY")
                 .setLabel('⏩')  
117                  .setDisabled(!last)                  .setDisabled(!last)
118                    .setEmoji(emoji('ArrowRight')!)
119          );          );
120    
121          return actionRow;          return actionRow;
# Line 107  export default class Pagination<T> { Line 128  export default class Pagination<T> {
128              interactionType: 'MESSAGE_COMPONENT',              interactionType: 'MESSAGE_COMPONENT',
129              componentType: 'BUTTON',              componentType: 'BUTTON',
130              message,              message,
131              time: 60_000,              time: this.options.timeout ?? 60_000,
132              filter: interaction => {              filter: interaction => {
133                  if (interaction.inGuild() && (!this.options.user_id || interaction.user.id === this.options.user_id)) {                  if (interaction.inGuild() && (!this.options.user_id || interaction.user.id === this.options.user_id)) {
134                      return true;                      return true;
135                  }                  }
136    
137                  if (interaction.isRepliable()) {                  if (interaction.isRepliable()) {
138                      interaction.reply({ content: 'That\'t not under your control or the button controls are expired', ephemeral: true });                      interaction.reply({ content: 'That\'s not under your control or the button controls are expired', ephemeral: true });
139                  }                  }
140    
141                  return false;                  return false;
# Line 126  export default class Pagination<T> { Line 147  export default class Pagination<T> {
147                  return;                  return;
148              }              }
149    
150                // await interaction.deferUpdate();
151                
152              const maxPage = Math.ceil(this.data.length / this.options.limit);              const maxPage = Math.ceil(this.data.length / this.options.limit);
153              const componentOptions = { first: true, last: true, next: true, back: true };              const componentOptions = { first: true, last: true, next: true, back: true };
154    
# Line 162  export default class Pagination<T> { Line 185  export default class Pagination<T> {
185          });          });
186    
187          collector.on("end", async () => {          collector.on("end", async () => {
188              await message.edit({ components: [this.getActionRow({ first: false, last: false, next: false, back: false })] });              const component = message.components[0]; // this.getActionRow({ first: false, last: false, next: false, back: false })
189    
190                for (const i in component.components) {
191                    component.components[i].disabled = true;
192                }
193    
194                await message.edit({ components: [component] });
195          });          });
196      }      }
 }  
197    }

Legend:
Removed from v.447  
changed lines
  Added in v.543

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26