mrmBot-Matrix/utils/pagination/pagination.js

188 lines
6.2 KiB
JavaScript
Raw Normal View History

import InteractionCollector from "./awaitinteractions.js";
import { ComponentInteraction } from "oceanic.js";
2019-09-13 20:02:41 +00:00
2022-03-31 19:53:22 +00:00
export default async (client, info, pages, timeout = 120000) => {
const options = info.type === "classic" ? {
messageReference: {
channelID: info.message.channelID,
2022-03-31 19:53:22 +00:00
messageID: info.message.id,
guildID: info.message.guildID,
failIfNotExists: false
},
allowedMentions: {
repliedUser: false
}
2022-03-31 19:53:22 +00:00
} : {};
2021-05-17 15:17:43 +00:00
let page = 0;
const components = {
components: [{
type: 1,
components: [
{
type: 2,
label: "Back",
emoji: {
id: null,
name: "◀"
},
style: 1,
customID: "back"
},
{
type: 2,
label: "Forward",
emoji: {
id: null,
name: "▶"
},
style: 1,
customID: "forward"
},
{
type: 2,
label: "Jump",
emoji: {
id: null,
name: "🔢"
},
style: 1,
customID: "jump"
},
{
type: 2,
label: "Delete",
emoji: {
id: null,
name: "🗑"
},
style: 4,
customID: "delete"
}
]
}]
};
2022-03-31 19:53:22 +00:00
let currentPage;
if (info.type === "classic") {
currentPage = await client.rest.channels.createMessage(info.message.channelID, Object.assign(pages[page], options, pages.length > 1 ? components : {}));
2022-03-31 19:53:22 +00:00
} else {
await info.interaction[info.interaction.acknowledged ? "editOriginal" : "createMessage"](Object.assign(pages[page], pages.length > 1 ? components : {}));
currentPage = await info.interaction.getOriginal();
2022-03-31 19:53:22 +00:00
}
if (pages.length > 1) {
const interactionCollector = new InteractionCollector(client, currentPage, ComponentInteraction, timeout);
interactionCollector.on("interaction", async (interaction) => {
2022-03-31 19:53:22 +00:00
if ((interaction.member ?? interaction.user).id === info.author.id) {
switch (interaction.data.customID) {
case "back":
await interaction.deferUpdate();
page = page > 0 ? --page : pages.length - 1;
currentPage = await currentPage.edit(Object.assign(pages[page], options));
interactionCollector.extend();
break;
case "forward":
await interaction.deferUpdate();
page = page + 1 < pages.length ? ++page : 0;
currentPage = await currentPage.edit(Object.assign(pages[page], options));
interactionCollector.extend();
break;
case "jump":
await interaction.deferUpdate();
var newComponents = JSON.parse(JSON.stringify(components));
for (const index of newComponents.components[0].components.keys()) {
newComponents.components[0].components[index].disabled = true;
}
currentPage = await currentPage.edit(newComponents);
interactionCollector.extend();
var jumpComponents = {
components: [{
type: 1,
components: [{
type: 3,
customID: "seekDropdown",
placeholder: "Page Number",
options: []
}]
}]
};
for (let i = 0; i < pages.length && i < 25; i++) {
const payload = {
label: i + 1,
value: i
};
jumpComponents.components[0].components[0].options[i] = payload;
}
2022-03-31 19:53:22 +00:00
var promise;
if (info.type === "classic") {
promise = client.rest.channels.createMessage(info.message.channelID, Object.assign({ content: "What page do you want to jump to?" }, {
2022-03-31 19:53:22 +00:00
messageReference: {
channelID: currentPage.channelID,
2022-03-31 19:53:22 +00:00
messageID: currentPage.id,
guildID: currentPage.guildID,
2022-03-31 19:53:22 +00:00
failIfNotExists: false
},
allowedMentions: {
repliedUser: false
}
}, jumpComponents));
} else {
promise = info.interaction.createFollowup(Object.assign({ content: "What page do you want to jump to?" }, jumpComponents));
}
promise.then(askMessage => {
const dropdownCollector = new InteractionCollector(client, askMessage, ComponentInteraction, timeout);
let ended = false;
dropdownCollector.on("interaction", async (response) => {
if (response.data.customID !== "seekDropdown") return;
2022-03-15 23:47:23 +00:00
try {
await askMessage.delete();
2022-03-15 23:47:23 +00:00
} catch {
// no-op
}
page = Number(response.data.values[0]);
currentPage = await currentPage.edit(Object.assign(pages[page], options, components));
ended = true;
dropdownCollector.stop();
});
dropdownCollector.once("end", async () => {
if (ended) return;
2022-03-15 23:47:23 +00:00
try {
await askMessage.delete();
2022-03-15 23:47:23 +00:00
} catch {
// no-op
}
currentPage = await currentPage.edit(Object.assign(pages[page], options, components));
});
}).catch(error => {
throw error;
});
break;
case "delete":
await interaction.deferUpdate();
2022-03-31 19:53:22 +00:00
interactionCollector.emit("end", true);
try {
await currentPage.delete();
} catch {
// no-op
}
return;
default:
break;
}
}
});
2022-03-31 19:53:22 +00:00
interactionCollector.once("end", async (deleted = false) => {
interactionCollector.removeAllListeners("interaction");
2022-03-31 19:53:22 +00:00
if (!deleted) {
for (const index of components.components[0].components.keys()) {
components.components[0].components[index].disabled = true;
}
try {
2022-03-31 19:53:22 +00:00
await currentPage.edit(components);
} catch {
// no-op
2022-03-31 19:53:22 +00:00
}
}
});
}
2019-09-13 20:02:41 +00:00
};