-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathchunkMessgae.js
More file actions
114 lines (99 loc) · 3.42 KB
/
Copy pathchunkMessgae.js
File metadata and controls
114 lines (99 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
const PAGE_SIZE = 27; // projects per page
const PHASE1_LABEL = '`PHASE 1 Project`'; // inline code formatting
const PHASE2_LABEL = '`PHASE 2 Project`'; // inline code formatting
function createProjectEmbed(pageProjects, pageIndex, totalPages) {
const embed = new EmbedBuilder()
.setTitle(`📚 GSSoC Projects (Page ${pageIndex + 1}/${totalPages})`)
.setColor('#00AAFF')
.setDescription(
// pageProjects.map((p, i) => `${i + 1}. [${p["Project name"]}](${p["Project link"]})`).join('\n')
pageProjects
.map((p, i) => {
const label = p.keyword === 'phase1' ? ` ${PHASE1_LABEL}` : ` ${PHASE2_LABEL}`;
return `${i + 1 + pageIndex * PAGE_SIZE}. [${p["Project name"]}](${p["Project link"]})${label}`;
})
.join('\n')
);
return embed;
}
async function sendPaginatedProjects(interaction, projects) {
const pages = [];
for (let i = 0; i < projects.length; i += PAGE_SIZE) {
pages.push(projects.slice(i, i + PAGE_SIZE));
}
let currentPage = 0;
const totalPages = pages.length;
const embedMessage = await interaction.reply({
embeds: [createProjectEmbed(pages[currentPage], currentPage, totalPages)],
components: [
new ActionRowBuilder({
components: [
new ButtonBuilder()
.setCustomId('prev')
.setLabel('◀️ Previous')
.setStyle(ButtonStyle.Primary)
.setDisabled(true),
new ButtonBuilder()
.setCustomId('next')
.setLabel('Next ▶️')
.setStyle(ButtonStyle.Primary)
.setDisabled(totalPages === 1),
],
}),
],
ephemeral: false,
fetchReply: true,
});
const collector = embedMessage.createMessageComponentCollector({
time: 120000,
});
collector.on('collect', async (btnInt) => {
if (btnInt.user.id !== interaction.user.id) {
return btnInt.reply({ content: 'This is not your interaction!', ephemeral: true });
}
if (btnInt.customId === 'next') currentPage++;
if (btnInt.customId === 'prev') currentPage--;
currentPage = Math.max(0, Math.min(currentPage, totalPages - 1));
await btnInt.update({
embeds: [createProjectEmbed(pages[currentPage], currentPage, totalPages)],
components: [
new ActionRowBuilder({
components: [
new ButtonBuilder()
.setCustomId('prev')
.setLabel('◀️ Previous')
.setStyle(ButtonStyle.Primary)
.setDisabled(currentPage === 0),
new ButtonBuilder()
.setCustomId('next')
.setLabel('Next ▶️')
.setStyle(ButtonStyle.Primary)
.setDisabled(currentPage === totalPages - 1),
],
}),
],
});
});
collector.on('end', () => {
embedMessage.edit({
components: [
new ActionRowBuilder({
components: [
new ButtonBuilder()
.setCustomId('prev')
.setLabel('◀️ Previous')
.setStyle(ButtonStyle.Primary)
.setDisabled(true),
new ButtonBuilder()
.setCustomId('next')
.setLabel('Next ▶️')
.setStyle(ButtonStyle.Primary)
.setDisabled(true),
],
}),
],
});
});
}
module.exports = { sendPaginatedProjects };