-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagination.js
149 lines (127 loc) · 3.63 KB
/
pagination.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ComponentType,
MessageFlags,
} = require("discord.js");
async function pagination(interaction, pages, time = 30 * 1000) {
try {
if (!interaction || !pages || !pages > 0)
throw new Error("[PAGINATION] Invalid args");
await interaction.deferReply();
if (pages.length === 1) {
return await interaction.editReply({
embeds: pages,
components: [],
withResponse: true,
});
}
var index = 0;
const first = new ButtonBuilder()
.setCustomId("pagefirst")
.setEmoji("⏮️")
.setStyle(ButtonStyle.Primary)
.setDisabled(true);
const prev = new ButtonBuilder()
.setCustomId("pageprev")
.setEmoji("◀️")
.setStyle(ButtonStyle.Primary)
.setDisabled(true);
const pageCount = new ButtonBuilder()
.setCustomId("pagecount")
.setLabel(`${index + 1}/${pages.length}`)
.setStyle(ButtonStyle.Secondary)
.setDisabled(true);
const next = new ButtonBuilder()
.setCustomId("pagenext")
.setEmoji("▶️")
.setStyle(ButtonStyle.Primary);
const last = new ButtonBuilder()
.setCustomId("pagelast")
.setEmoji("⏭️")
.setStyle(ButtonStyle.Primary);
const buttons = new ActionRowBuilder().addComponents([
first,
prev,
pageCount,
next,
last,
]);
const msg = await interaction.editReply({
embeds: [pages[index]],
components: [buttons],
withResponse: true,
});
const collector = await msg.createMessageComponentCollector({
ComponentType: ComponentType.Button,
time,
});
collector.on("collect", async (i) => {
if (i.user.id !== interaction.user.id)
return await i.reply({
content: `Only **${interaction.user.name}** can use these buttons!`,
flags: MessageFlags.Ephemeral,
});
await i.deferUpdate();
if (i.customId == `pagefirst`) {
index = 0;
pageCount.setLabel(`${index + 1}/${pages.length}`);
}
if (i.customId == `pageprev`) {
if (index > 0) index--;
pageCount.setLabel(`${index + 1}/${pages.length}`);
} else if (i.customId == `pagenext`) {
if (index < pages.length - 1) {
index++;
pageCount.setLabel(`${index + 1}/${pages.length}`);
}
} else if (i.customId == `pagelast`) {
index = pages.length - 1;
pageCount.setLabel(`${index + 1}/${pages.length}`);
}
if (index == 0) {
first.setDisabled(true);
prev.setDisabled(true);
} else {
first.setDisabled(false);
prev.setDisabled(false);
}
if (index == pages.length - 1) {
next.setDisabled(true);
last.setDisabled(true);
} else {
next.setDisabled(false);
last.setDisabled(false);
}
await msg
.edit({
embeds: [pages[index]],
components: [buttons],
})
.catch((err) => {});
collector.resetTimer();
});
collector.on("end", async () => {
await msg
.edit({
embeds: [pages[index]],
components: [],
})
.catch((err) => {});
});
return msg;
} catch (err) {
console.log(`[ERROR] ${err}`);
}
}
module.exports = pagination;
//このファイルをインポートするファイルでは、以下のようにすることで、ページを作れる
/*
const pagination = require("./pagination.js");
const embeds = [];
for (var i=0; i<4; i++) {
embeds.push(new EmbedBuilder().setDescription(`page ${i + 1}`));
}
await pagination(interaction, embeds);
*/