2021-08-19 14:19:14 +00:00
|
|
|
import InteractionCollector from "./awaitinteractions.js";
|
2022-09-24 03:25:16 +00:00
|
|
|
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" ? {
|
2021-05-11 16:12:01 +00:00
|
|
|
messageReference: {
|
2022-09-24 03:25:16 +00:00
|
|
|
channelID: info.message.channelID,
|
2022-03-31 19:53:22 +00:00
|
|
|
messageID: info.message.id,
|
2022-09-24 03:25:16 +00:00
|
|
|
guildID: info.message.guildID,
|
2021-05-11 16:12:01 +00:00
|
|
|
failIfNotExists: false
|
|
|
|
},
|
|
|
|
allowedMentions: {
|
|
|
|
repliedUser: false
|
|
|
|
}
|
2022-03-31 19:53:22 +00:00
|
|
|
} : {};
|
2021-05-17 15:17:43 +00:00
|
|
|
let page = 0;
|
2021-08-12 23:45:17 +00:00
|
|
|
const components = {
|
|
|
|
components: [{
|
|
|
|
type: 1,
|
|
|
|
components: [
|
|
|
|
{
|
|
|
|
type: 2,
|
|
|
|
label: "Back",
|
|
|
|
emoji: {
|
|
|
|
id: null,
|
|
|
|
name: "◀"
|
|
|
|
},
|
|
|
|
style: 1,
|
2022-09-24 03:25:16 +00:00
|
|
|
customID: "back"
|
2021-08-12 23:45:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 2,
|
|
|
|
label: "Forward",
|
|
|
|
emoji: {
|
|
|
|
id: null,
|
|
|
|
name: "▶"
|
|
|
|
},
|
|
|
|
style: 1,
|
2022-09-24 03:25:16 +00:00
|
|
|
customID: "forward"
|
2021-08-12 23:45:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 2,
|
|
|
|
label: "Jump",
|
|
|
|
emoji: {
|
|
|
|
id: null,
|
|
|
|
name: "🔢"
|
|
|
|
},
|
|
|
|
style: 1,
|
2022-09-24 03:25:16 +00:00
|
|
|
customID: "jump"
|
2021-08-12 23:45:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 2,
|
|
|
|
label: "Delete",
|
|
|
|
emoji: {
|
|
|
|
id: null,
|
|
|
|
name: "🗑"
|
|
|
|
},
|
|
|
|
style: 4,
|
2022-09-24 03:25:16 +00:00
|
|
|
customID: "delete"
|
2021-08-12 23:45:17 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}]
|
|
|
|
};
|
2022-03-31 19:53:22 +00:00
|
|
|
let currentPage;
|
|
|
|
if (info.type === "classic") {
|
2022-09-24 03:25:16 +00:00
|
|
|
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 {
|
2022-10-30 05:17:36 +00:00
|
|
|
currentPage = await info.interaction[info.interaction.acknowledged ? "editOriginal" : "createMessage"](Object.assign(pages[page], pages.length > 1 ? components : {}));
|
|
|
|
if (!currentPage) currentPage = await info.interaction.getOriginal();
|
2022-03-31 19:53:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 23:45:17 +00:00
|
|
|
if (pages.length > 1) {
|
2022-02-02 17:01:33 +00:00
|
|
|
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) {
|
2022-09-24 03:25:16 +00:00
|
|
|
switch (interaction.data.customID) {
|
2021-08-12 23:45:17 +00:00
|
|
|
case "back":
|
2022-02-02 17:01:33 +00:00
|
|
|
await interaction.deferUpdate();
|
2021-08-06 17:12:59 +00:00
|
|
|
page = page > 0 ? --page : pages.length - 1;
|
2021-11-16 20:40:17 +00:00
|
|
|
currentPage = await currentPage.edit(Object.assign(pages[page], options));
|
2022-02-06 07:12:04 +00:00
|
|
|
interactionCollector.extend();
|
2021-08-06 17:12:59 +00:00
|
|
|
break;
|
2021-08-12 23:45:17 +00:00
|
|
|
case "forward":
|
2022-02-02 17:01:33 +00:00
|
|
|
await interaction.deferUpdate();
|
2021-08-12 23:45:17 +00:00
|
|
|
page = page + 1 < pages.length ? ++page : 0;
|
2021-11-16 20:40:17 +00:00
|
|
|
currentPage = await currentPage.edit(Object.assign(pages[page], options));
|
2022-02-06 07:12:04 +00:00
|
|
|
interactionCollector.extend();
|
2021-08-12 23:45:17 +00:00
|
|
|
break;
|
|
|
|
case "jump":
|
2022-02-02 17:01:33 +00:00
|
|
|
await interaction.deferUpdate();
|
2022-03-10 20:24:03 +00:00
|
|
|
var newComponents = JSON.parse(JSON.stringify(components));
|
2022-01-15 05:26:38 +00:00
|
|
|
for (const index of newComponents.components[0].components.keys()) {
|
|
|
|
newComponents.components[0].components[index].disabled = true;
|
|
|
|
}
|
|
|
|
currentPage = await currentPage.edit(newComponents);
|
2022-02-06 07:12:04 +00:00
|
|
|
interactionCollector.extend();
|
2022-03-10 20:24:03 +00:00
|
|
|
var jumpComponents = {
|
2022-02-02 17:01:33 +00:00
|
|
|
components: [{
|
|
|
|
type: 1,
|
|
|
|
components: [{
|
|
|
|
type: 3,
|
2022-09-24 03:25:16 +00:00
|
|
|
customID: "seekDropdown",
|
2022-02-02 17:01:33 +00:00
|
|
|
placeholder: "Page Number",
|
|
|
|
options: []
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
};
|
2022-03-10 20:24:03 +00:00
|
|
|
for (let i = 0; i < pages.length && i < 25; i++) {
|
2022-02-02 17:01:33 +00:00
|
|
|
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") {
|
2022-09-24 03:25:16 +00:00
|
|
|
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: {
|
2022-09-24 03:25:16 +00:00
|
|
|
channelID: currentPage.channelID,
|
2022-03-31 19:53:22 +00:00
|
|
|
messageID: currentPage.id,
|
2022-09-24 03:25:16 +00:00
|
|
|
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 => {
|
2022-02-02 17:01:33 +00:00
|
|
|
const dropdownCollector = new InteractionCollector(client, askMessage, ComponentInteraction, timeout);
|
|
|
|
let ended = false;
|
|
|
|
dropdownCollector.on("interaction", async (response) => {
|
2022-09-24 03:25:16 +00:00
|
|
|
if (response.data.customID !== "seekDropdown") return;
|
2022-03-15 23:47:23 +00:00
|
|
|
try {
|
2022-09-24 03:25:16 +00:00
|
|
|
await askMessage.delete();
|
2022-03-15 23:47:23 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2022-02-02 17:01:33 +00:00
|
|
|
page = Number(response.data.values[0]);
|
|
|
|
currentPage = await currentPage.edit(Object.assign(pages[page], options, components));
|
|
|
|
ended = true;
|
|
|
|
dropdownCollector.stop();
|
2021-08-06 17:12:59 +00:00
|
|
|
});
|
2022-02-02 17:01:33 +00:00
|
|
|
dropdownCollector.once("end", async () => {
|
|
|
|
if (ended) return;
|
2022-03-15 23:47:23 +00:00
|
|
|
try {
|
2022-09-24 03:25:16 +00:00
|
|
|
await askMessage.delete();
|
2022-03-15 23:47:23 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2022-01-15 05:26:38 +00:00
|
|
|
currentPage = await currentPage.edit(Object.assign(pages[page], options, components));
|
2021-08-06 17:12:59 +00:00
|
|
|
});
|
|
|
|
}).catch(error => {
|
|
|
|
throw error;
|
2019-10-24 18:54:23 +00:00
|
|
|
});
|
2021-08-06 17:12:59 +00:00
|
|
|
break;
|
2021-08-12 23:45:17 +00:00
|
|
|
case "delete":
|
2022-02-02 17:01:33 +00:00
|
|
|
await interaction.deferUpdate();
|
2022-03-31 19:53:22 +00:00
|
|
|
interactionCollector.emit("end", true);
|
2022-09-24 03:25:16 +00:00
|
|
|
try {
|
|
|
|
await currentPage.delete();
|
|
|
|
} catch {
|
|
|
|
// no-op
|
|
|
|
}
|
2021-08-06 17:12:59 +00:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-10-24 18:54:23 +00:00
|
|
|
}
|
2021-08-06 17:12:59 +00:00
|
|
|
});
|
2022-03-31 19:53:22 +00:00
|
|
|
interactionCollector.once("end", async (deleted = false) => {
|
2021-08-12 23:45:17 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-09-24 03:25:16 +00:00
|
|
|
try {
|
2022-03-31 19:53:22 +00:00
|
|
|
await currentPage.edit(components);
|
2022-09-24 03:25:16 +00:00
|
|
|
} catch {
|
|
|
|
// no-op
|
2022-03-31 19:53:22 +00:00
|
|
|
}
|
2022-01-15 05:26:38 +00:00
|
|
|
}
|
2021-08-06 17:12:59 +00:00
|
|
|
});
|
|
|
|
}
|
2019-09-13 20:02:41 +00:00
|
|
|
};
|