1 |
import DiscordClient from "../client/Client"; |
import DiscordClient from "../client/Client"; |
2 |
import MessageEmbed from "../client/MessageEmbed"; |
import MessageEmbed from "../client/MessageEmbed"; |
3 |
import { v4 as uuid } from 'uuid'; |
import { v4 as uuid } from 'uuid'; |
4 |
import { ButtonInteraction, InteractionCollector, InteractionReplyOptions, Message, MessageActionRow, MessageButton, MessageEditOptions, MessageOptions, ReplyMessageOptions, TextChannel } from "discord.js"; |
import { ButtonInteraction, InteractionCollector, InteractionReplyOptions, Message, MessageActionRow, MessageButton, MessageEditOptions, MessageOptions, ReplyMessageOptions } from "discord.js"; |
5 |
|
import { emoji } from "./Emoji"; |
6 |
|
|
7 |
export interface EmbedBuilderOptions<T> { |
export interface EmbedBuilderOptions<T> { |
8 |
data: Array<T>; |
data: Array<T>; |
15 |
guild_id: string; |
guild_id: string; |
16 |
channel_id: string; |
channel_id: string; |
17 |
user_id?: string; |
user_id?: string; |
18 |
|
timeout?: number; |
19 |
embedBuilder: (options: EmbedBuilderOptions<T>) => MessageEmbed; |
embedBuilder: (options: EmbedBuilderOptions<T>) => MessageEmbed; |
20 |
actionRowBuilder?: (options: { first: boolean, last: boolean, next: boolean, back: boolean }) => MessageActionRow<MessageButton>; |
actionRowBuilder?: (options: { first: boolean, last: boolean, next: boolean, back: boolean }) => MessageActionRow<MessageButton>; |
21 |
} |
} |
62 |
|
|
63 |
options.embeds ??= []; |
options.embeds ??= []; |
64 |
options.embeds.push(this.getEmbed(page)); |
options.embeds.push(this.getEmbed(page)); |
65 |
console.log(options.embeds); |
|
66 |
options.components ??= []; |
options.components ??= []; |
67 |
options.components.push(this.getActionRow(actionRowOptionsDup)); |
options.components.push(this.getActionRow(actionRowOptionsDup)); |
68 |
|
|
74 |
return this.options.actionRowBuilder({ first, last, next, back }); |
return this.options.actionRowBuilder({ first, last, next, back }); |
75 |
} |
} |
76 |
|
|
77 |
const actionRow = new MessageActionRow<MessageButton>; |
const actionRow = new MessageActionRow<MessageButton>(); |
78 |
|
|
79 |
actionRow.addComponents( |
actionRow.addComponents( |
80 |
new MessageButton() |
new MessageButton() |
81 |
.setCustomId(`pagination_first_${this.id}`) |
.setCustomId(`pagination_first_${this.id}`) |
82 |
.setStyle("PRIMARY") |
.setStyle("PRIMARY") |
83 |
.setLabel('⏪') |
.setDisabled(!first) |
84 |
.setDisabled(!first), |
.setEmoji(emoji('ChevronLeft')!), |
85 |
new MessageButton() |
new MessageButton() |
86 |
.setCustomId(`pagination_back_${this.id}`) |
.setCustomId(`pagination_back_${this.id}`) |
87 |
.setStyle("PRIMARY") |
.setStyle("PRIMARY") |
88 |
.setLabel('◀') |
.setDisabled(!back) |
89 |
.setDisabled(!back), |
.setEmoji(emoji('ArrowLeft')!), |
90 |
new MessageButton() |
new MessageButton() |
91 |
.setCustomId(`pagination_next_${this.id}`) |
.setCustomId(`pagination_next_${this.id}`) |
92 |
.setStyle("PRIMARY") |
.setStyle("PRIMARY") |
93 |
.setLabel('▶') |
.setDisabled(!next) |
94 |
.setDisabled(!next), |
.setEmoji(emoji('ArrowRight')!), |
95 |
new MessageButton() |
new MessageButton() |
96 |
.setCustomId(`pagination_last_${this.id}`) |
.setCustomId(`pagination_last_${this.id}`) |
97 |
.setStyle("PRIMARY") |
.setStyle("PRIMARY") |
|
.setLabel('⏩') |
|
98 |
.setDisabled(!last) |
.setDisabled(!last) |
99 |
|
.setEmoji(emoji('ChevronRight')!) |
100 |
); |
); |
101 |
|
|
102 |
return actionRow; |
return actionRow; |
109 |
interactionType: 'MESSAGE_COMPONENT', |
interactionType: 'MESSAGE_COMPONENT', |
110 |
componentType: 'BUTTON', |
componentType: 'BUTTON', |
111 |
message, |
message, |
112 |
time: 60_000, |
time: this.options.timeout ?? 60_000, |
113 |
filter: interaction => { |
filter: interaction => { |
114 |
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)) { |
115 |
return true; |
return true; |
128 |
return; |
return; |
129 |
} |
} |
130 |
|
|
131 |
|
// await interaction.deferUpdate(); |
132 |
|
|
133 |
const maxPage = Math.ceil(this.data.length / this.options.limit); |
const maxPage = Math.ceil(this.data.length / this.options.limit); |
134 |
const componentOptions = { first: true, last: true, next: true, back: true }; |
const componentOptions = { first: true, last: true, next: true, back: true }; |
135 |
|
|
166 |
}); |
}); |
167 |
|
|
168 |
collector.on("end", async () => { |
collector.on("end", async () => { |
169 |
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 }) |
170 |
|
|
171 |
|
for (const i in component.components) { |
172 |
|
component.components[i].disabled = true; |
173 |
|
} |
174 |
|
|
175 |
|
await message.edit({ components: [component] }); |
176 |
}); |
}); |
177 |
} |
} |
178 |
} |
} |