2021-08-19 14:19:14 +00:00
|
|
|
import MessageCollector from "./awaitmessages.js";
|
|
|
|
import InteractionCollector from "./awaitinteractions.js";
|
|
|
|
import fetch from "node-fetch";
|
2019-09-13 20:02:41 +00:00
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
export default async (client, message, pages, timeout = 120000) => {
|
2021-03-19 20:10:15 +00:00
|
|
|
const manageMessages = message.channel.guild && message.channel.permissionsOf(client.user.id).has("manageMessages") ? true : false;
|
2021-08-12 23:45:17 +00:00
|
|
|
const options = {
|
2021-05-11 16:12:01 +00:00
|
|
|
messageReference: {
|
|
|
|
channelID: message.channel.id,
|
|
|
|
messageID: message.id,
|
|
|
|
guildID: message.channel.guild ? message.channel.guild.id : undefined,
|
|
|
|
failIfNotExists: false
|
|
|
|
},
|
|
|
|
allowedMentions: {
|
|
|
|
repliedUser: false
|
|
|
|
}
|
2021-08-12 23:45:17 +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,
|
|
|
|
custom_id: "back"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 2,
|
|
|
|
label: "Forward",
|
|
|
|
emoji: {
|
|
|
|
id: null,
|
|
|
|
name: "▶"
|
|
|
|
},
|
|
|
|
style: 1,
|
|
|
|
custom_id: "forward"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 2,
|
|
|
|
label: "Jump",
|
|
|
|
emoji: {
|
|
|
|
id: null,
|
|
|
|
name: "🔢"
|
|
|
|
},
|
|
|
|
style: 1,
|
|
|
|
custom_id: "jump"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 2,
|
|
|
|
label: "Delete",
|
|
|
|
emoji: {
|
|
|
|
id: null,
|
|
|
|
name: "🗑"
|
|
|
|
},
|
|
|
|
style: 4,
|
|
|
|
custom_id: "delete"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
const ackOptions = {
|
|
|
|
method: "POST",
|
|
|
|
body: "{\"type\":6}",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
2021-08-06 17:12:59 +00:00
|
|
|
}
|
2021-08-12 23:45:17 +00:00
|
|
|
};
|
2021-08-13 05:09:30 +00:00
|
|
|
let currentPage = await client.createMessage(message.channel.id, Object.assign(pages[page], options, pages.length > 1 ? components : {}));
|
2021-08-12 23:45:17 +00:00
|
|
|
if (pages.length > 1) {
|
2021-10-23 18:58:19 +00:00
|
|
|
const interactionCollector = new InteractionCollector(client, currentPage, timeout);
|
2021-08-14 21:15:21 +00:00
|
|
|
interactionCollector.on("interaction", async (interaction, id, token, member) => {
|
2021-08-14 02:34:44 +00:00
|
|
|
if (member === message.author.id) {
|
2021-08-12 23:45:17 +00:00
|
|
|
switch (interaction) {
|
|
|
|
case "back":
|
2022-01-15 05:26:38 +00:00
|
|
|
await fetch(`https://discord.com/api/v9/interactions/${id}/${token}/callback`, ackOptions);
|
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));
|
2021-08-06 17:12:59 +00:00
|
|
|
break;
|
2021-08-12 23:45:17 +00:00
|
|
|
case "forward":
|
2022-01-15 05:26:38 +00:00
|
|
|
await fetch(`https://discord.com/api/v9/interactions/${id}/${token}/callback`, ackOptions);
|
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));
|
2021-08-12 23:45:17 +00:00
|
|
|
break;
|
|
|
|
case "jump":
|
2022-01-15 05:26:38 +00:00
|
|
|
await fetch(`https://discord.com/api/v9/interactions/${id}/${token}/callback`, ackOptions);
|
|
|
|
const 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);
|
2021-08-06 17:12:59 +00:00
|
|
|
client.createMessage(message.channel.id, Object.assign({ content: "What page do you want to jump to?" }, {
|
|
|
|
messageReference: {
|
|
|
|
channelID: currentPage.channel.id,
|
|
|
|
messageID: currentPage.id,
|
|
|
|
guildID: currentPage.channel.guild ? currentPage.channel.guild.id : undefined,
|
|
|
|
failIfNotExists: false
|
|
|
|
},
|
|
|
|
allowedMentions: {
|
|
|
|
repliedUser: false
|
|
|
|
}
|
|
|
|
})).then(askMessage => {
|
|
|
|
const messageCollector = new MessageCollector(client, askMessage.channel, (response) => response.author.id === message.author.id && !isNaN(response.content) && Number(response.content) <= pages.length && Number(response.content) > 0, {
|
|
|
|
time: timeout,
|
|
|
|
maxMatches: 1
|
|
|
|
});
|
|
|
|
return messageCollector.on("message", async (response) => {
|
2021-12-13 21:46:08 +00:00
|
|
|
if (await client.getMessage(askMessage.channel.id, askMessage.id).catch(() => undefined)) await askMessage.delete();
|
2021-08-06 17:12:59 +00:00
|
|
|
if (manageMessages) await response.delete();
|
|
|
|
page = Number(response.content) - 1;
|
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-01-15 05:26:38 +00:00
|
|
|
await fetch(`https://discord.com/api/v9/interactions/${id}/${token}/callback`, ackOptions);
|
2021-08-12 23:45:17 +00:00
|
|
|
interactionCollector.emit("end");
|
2021-08-06 17:12:59 +00:00
|
|
|
if (await client.getMessage(currentPage.channel.id, currentPage.id).catch(() => undefined)) await currentPage.delete();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-10-24 18:54:23 +00:00
|
|
|
}
|
2021-08-06 17:12:59 +00:00
|
|
|
});
|
2022-01-15 05:26:38 +00:00
|
|
|
interactionCollector.once("end", async () => {
|
2021-08-12 23:45:17 +00:00
|
|
|
interactionCollector.removeAllListeners("interaction");
|
2022-01-15 05:26:38 +00:00
|
|
|
if (await client.getMessage(currentPage.channel.id, currentPage.id).catch(() => undefined)) {
|
|
|
|
for (const index of components.components[0].components.keys()) {
|
|
|
|
components.components[0].components[index].disabled = true;
|
|
|
|
}
|
|
|
|
await currentPage.edit(components);
|
|
|
|
}
|
2021-08-06 17:12:59 +00:00
|
|
|
});
|
|
|
|
}
|
2019-09-13 20:02:41 +00:00
|
|
|
};
|