Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2
📃 A lightweight Pagination module for Necord
License
necordjs/pagination
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Certainly! Pagination is a useful technique employed in user interfaces to present large amounts of information in a structured andmanageable way. When dealing with substantial volumes of data, such as search results, articles, or product listings, presenting it all atonce can overwhelm users and lead to a poor user experience. Pagination allows you to divide the information into smaller, organized chunks,enhancing user engagement and ease of navigation. This module allows you to create a pagination with a few lines of code.
Node.js 18.0.0 or newer is required.
$ npm i @necord/pagination necord discord.js$ yarn add @necord/pagination necord discord.js$ pnpm add @necord/pagination necord discord.js
Once the installation process is complete, we can import theNecordPaginationModule with yourNecordModule into the rootAppModule:
import{NecordModule}from'necord';import{Module}from'@nestjs/common';import{NecordPaginationModule}from'@necord/pagination';import{AppService}from'./app.service';@Module({imports:[NecordModule.forRoot({token:'DISCORD_BOT_TOKEN',intents:['Guilds','GuildMessages','DirectMessages']}),NecordPaginationModule.forRoot({// Change your buttons appearancebuttons:{},// Add buttons for skip to first and last pageallowSkip:true,// Add buttons for search pageallowTraversal:true,// Define the buttons position (start / end)buttonsPosition:'end'})],providers:[AppService]})exportclassAppModule{}
Then, we can inject thePaginationService into our service and register a pagination handler:
import{OnModuleInit,Injectable}from'@nestjs/common';import{NecordPaginationService,PageBuilder}from'@necord/pagination';import{Context,SlashCommand,SlashCommandContext}from'necord';@Injectable()exportclassAppServiceimplementsOnModuleInit{publicconstructor(privatereadonlypaginationService:NecordPaginationService){}publiconModuleInit():void{returnthis.paginationService.register(builder=>builder// Required, need for search your builder.setCustomId('test')// First way to set pages.setPages([newPageBuilder().setContent('Page 1'),newPageBuilder().setContent('Page 2'),newPageBuilder().setContent('Page 3'),newPageBuilder().setContent('Page 4'),newPageBuilder().setContent('Page 5')])// Second way, you can manually set pages using `setPages` method.setPagesFactory(page=>newPageBuilder().setContent(`Page${page}`))// Optional, only if you want to use pages factory.setMaxPages(5));} @SlashCommand({name:'pagination',description:'Test pagination'})publicasynconPagination(@Context()[interaction]:SlashCommandContext){constpagination=this.paginationService.get('test');constpage=awaitpagination.build();returninteraction.reply(page);}}
Congratulations! You have successfully created your first pagination!Just usepagination command and you will see your pagination!
You can also register pagination's dynamically inside your command handlers instead ofonModuleInit hook.
import{Injectable}from"@nestjs/common";import{NecordPaginationService,PageBuilder}from"@necord/pagination";import{Context,SlashCommand,SlashCommandContext}from"necord";@Injectable()exportclassAppService{publicconstructor(privatereadonlypaginationService:NecordPaginationService){} @SlashCommand({name:"dynamic-pagination",description:"Register pagination inside command"})publicasynconDynamicPagination(@Context()[interaction]:SlashCommandContext){constpagination=this.paginationService.create(builder=>builder.setCustomId(`dynamic-${interaction.user.id}`).setFilter(i=>i.user.id===interaction.user.id).setPages([newPageBuilder().setContent("Dynamic Page 1"),newPageBuilder().setContent("Dynamic Page 2")]));constpage=awaitpagination.build();returninteraction.reply(page);}}
⚠️ You should ensure thatsetCustomIdis unique per user/session if you register dynamically to avoid collisions.
You can view a working examplehere.
- Author -Alexey Filippov
- Twitter -@SocketSomeone
About
📃 A lightweight Pagination module for Necord
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.
