Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

401 add pagination helper #527

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Adding paragraph input to pagination and pages limit
sagarpatel211 committed Jun 19, 2024
commit 440e698615f0bfe4b6b4bfb60dd6770f2010df47
53 changes: 51 additions & 2 deletions src/commandDetails/miscellaneous/pagination-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { container } from '@sapphire/framework';
import { CodeyCommandDetails, getUserFromMessage } from '../../codeyCommand';
import { EmbedBuilder } from 'discord.js';
import { PaginationBuilder } from '../../utils/pagination';
import { PaginationBuilder, PaginationBuilderFromText } from '../../utils/pagination';

Check warning on line 4 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

'PaginationBuilder' is defined but never used
import { SapphireMessageExecuteType, SapphireMessageResponse } from '../../codeyCommand';
import { Colors } from 'discord.js';

@@ -41,7 +41,7 @@
},
];

const embeds: EmbedBuilder[] = embedData.map((data) =>

Check warning on line 44 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

'embeds' is assigned a value but never used
new EmbedBuilder()
.setColor(Colors.Blue)
.setTitle(data.title)
@@ -50,7 +50,56 @@
);

try {
await PaginationBuilder(message, author, embeds, 10000);
//await PaginationBuilder(message, author, embeds);

// await PaginationBuilderFromText(
// message,
// author,
// `Lorem Ipsum is simply dummy text of the printing and typesetting industry. \

Check failure on line 58 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

Delete `·`
// Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make \

Check failure on line 59 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

Delete `·`
// a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, \

Check failure on line 60 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

Delete `·`
// remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \

Check failure on line 61 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

Delete `·`
// of Lorem Ipsum. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, \

Check failure on line 62 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

Delete `·`
// consectetur, from a \

Check failure on line 63 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

Delete `·`
// Lorem \

Check failure on line 64 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

Delete `·`
// Ipsum passage, and going through the cites of the word in classical literature, \

Check failure on line 65 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

Delete `·`
// discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and \

Check failure on line 66 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

Delete `·`
// 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, \

Check failure on line 67 in src/commandDetails/miscellaneous/pagination-test.ts

GitHub Actions / build (16.x)

Delete `·`
// written in 45 BC. This book is a treatise on the theory of ethics, \
// very popular \
// during the Renaissance. The first \
// line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.`,
// );

// await PaginationBuilderFromText(message, author, "") // no content test case

// too much content test case, spaces are necessary after slash!!
await PaginationBuilderFromText(message, author, `Lorem Ipsum is simply dummy text of the printing and typesetting industry. \
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make \
a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions \
of Lorem Ipsum. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, \
consectetur, from a \
Lorem \
Ipsum passage, and going through the cites of the word in classical literature, \
discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and \
1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, \
written in 45 BC. This book is a treatise on the theory of ethics, \
very popular \
during the Renaissance. The first \
line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.`);
} catch (error) {
await message.reply('Error or timeout occurred during navigation.');
}
72 changes: 70 additions & 2 deletions src/utils/pagination.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,31 @@ import {
CacheType,
} from 'discord.js';

const COLLECTOR_TIMEOUT = 120000;
const COLLECTOR_TIMEOUT = 300000;
const MAX_CHARS_PER_PAGE = 2048;
const MAX_PAGES = 25;
const getRandomColor = (): number => Math.floor(Math.random() * 16777215);

const textToPages = (text: string, maxChars: number): string[] => {
const pages: string[] = [];
let currentPage = '';
let charCount = 0;

for (let i = 0; i < text.length; i++) {
currentPage += text[i];
charCount++;
if (text[i] === '\n' || charCount >= maxChars) {
pages.push(currentPage.trim());
currentPage = '';
charCount = 0;
}
}

if (currentPage.trim()) {
pages.push(currentPage.trim());
}
return pages;
};

export const PaginationBuilder = async (
originalMessage: Message<boolean> | ChatInputCommandInteraction<CacheType>,
@@ -25,6 +49,16 @@ export const PaginationBuilder = async (
});
return;
}
if (embedPages.length > MAX_PAGES) {
await originalMessage.reply({
embeds: [
new EmbedBuilder().setColor(0xff0000).setDescription(
`Too much content to display. Limit is ${MAX_PAGES} pages. \nCurrent content produces ${embedPages.length} pages.`,
),
],
});
return;
}
let currentPage = 0;
const firstButton = new ButtonBuilder()
.setCustomId('first')
@@ -111,13 +145,47 @@ export const PaginationBuilder = async (
});

collector.on('end', async () => {
firstButton.setDisabled(true);
previousButton.setDisabled(true);
nextButton.setDisabled(true);
lastButton.setDisabled(true);
pageCount.setDisabled(true);

await message.edit({
components: [],
components: [actionRow],
});

setTimeout(async () => {
await message.edit({
components: [],
});
}, 3000);
});

return message;
} catch (error) {
return undefined;
}
};

export const PaginationBuilderFromText = async (
originalMessage: Message<boolean> | ChatInputCommandInteraction<CacheType>,
author: string,
text: string,
textPageSize: number = MAX_CHARS_PER_PAGE,
timeout: number = COLLECTOR_TIMEOUT,
): Promise<Message<boolean> | undefined> => {
try {
const textPages = textToPages(text, textPageSize);
const embedPages = textPages.map((text, index) =>
new EmbedBuilder()
.setColor(getRandomColor())
.setTitle('Page ' + (index + 1))
.setDescription(text),
);

return PaginationBuilder(originalMessage, author, embedPages, timeout);
} catch (error) {
return undefined;
}
};
Loading