oops deleted something important

This commit is contained in:
Emily 2021-08-30 19:39:34 +10:00
commit fc7cb02784
4 changed files with 239 additions and 0 deletions

19
LICENSE Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2020 XzFirzal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

41
README.md Normal file
View File

@ -0,0 +1,41 @@
# discord-paginator.js
A lightweight package to paginate discord message for discord bots with ease!
# How to use? easy!
# Install using NPM
```properties
npm install discord-paginator.js
```
# Example
```js
const BasePaginator = require('discord-paginator.js')
const pages = ['foo', 'bar'] //return foo at the first page, and bar at the second page
const Paginator = new BasePaginator({
pages: pages, //the pages
timeout: 120000, //the timeout for the reaction collector ended (in ms)
page: 'Page {current}/{total}', //Show the page counter to the message
filter: (reaction, user) => user.id == message.author.id //to filter the reaction collector
})
Paginator.spawn(message.channel) //to spawn the paginator to specific text channel
```
# Explanation
```js
const Paginator = new BasePaginator({
pages: Array<String || Discord.MessageEmbed>, //The pages, can contain string or message embed
remove: String, //The emoji to despawn the paginator
reset: String, //The emoji to reset the paginator to first page
reaction: [String, String], //The emoji to move previous or next page
removeReaction: Boolean, //Remove the user reaction when used
removeAtEnd: Boolean, //Remove the reactions when the reaction collector is ended
pageCount: String, //Show the page counter to the message
timeout: Number, //The timeout for the reaction collector ended (in ms)
filter: Function //An additional filter for the paginator
})
await Paginator.spawn(Discord.TextChannel) //Spawn the paginator to specific text channel
```

124
index.js Normal file
View File

@ -0,0 +1,124 @@
let djs
try {
djs = require('discord.js')
} catch {
throw new Error('Paginator: ' + 'Please install discord.js package manually')
}
const defaultOpts = {
pages: [],
remove: '🗑️',
reset: '🔁',
reaction: ['⬅️', '➡️'],
removeReaction: true,
removeAtEnd: true,
timeout: 60000,
filter: (reaction, user) => true
}
function none() {}
class Paginator {
constructor(opts = defaultOpts) {
Object.assign(this, defaultOpts)
Object.assign(this, opts)
}
async spawn(channel) {
const opts = {
remove: this.remove,
reset: this.reset,
reaction: this.reaction,
removeReaction: this.removeReaction,
removeAtEnd: this.removeAtEnd,
timeout: this.timeout,
filter: this.filter
}
const pages = [...this.pages]
let page = 0
if (!pages.length) throw new Error('Paginator: ' + 'Empty pages')
let message = await channel.send(pages[page])
if (!Array.isArray(opts.reaction) || opts.reaction.length < 2) throw new Error('Paginator: ' + 'Must be two reactions given in \'reaction\' options')
if (typeof opts.filter != 'function') throw new Error('Paginator: ' + 'Expecting a function in \'filter\' options')
if (opts.timeout < 1000 || opts.timeout > 259200000) throw new Error('Paginator: ' + 'Spawner timeout must be between 1 second and 3 days')
const reactions = {}
if (typeof opts.reset == 'string' && pages.length > 1) {
reactions.reset = await message.react(opts.reset).catch(none)
if (!reactions.reset) return
}
if (pages.length > 1) {
reactions.left = await message.react(opts.reaction[0]).catch(none)
if (!reactions.left) return
reactions.right = await message.react(opts.reaction[1]).catch(none)
}
if (typeof opts.remove == 'string') {
reactions.remove = await message.react(opts.remove).catch(none)
if (!reactions.remove) return
}
async function filter(reaction, user) {
const bool = [false, await opts.filter(reaction, user)]
if (reactions.reset) bool[0] = reaction.emoji.toString() == reactions.reset.emoji.toString()
if (!bool[0] && reactions.remove) bool[0] = reaction.emoji.toString() == reactions.remove.emoji.toString()
if (!bool[0]) bool[0] = reaction.emoji.toString() == reactions.left.emoji.toString() || reaction.emoji.toString() == reactions.right.emoji.toString()
return bool.every(condition => condition)
}
const collector = message.createReactionCollector(filter, { time: opts.timeout })
collector.on('collect', async (reaction, user) => {
if (reactions.reset && reaction.emoji.toString() == reactions.reset.emoji.toString()) {
page = 0
await message.edit(pages[page])
} else if (reactions.remove && reaction.emoji.toString() == reactions.remove.emoji.toString()) {
collector.stop('123M0V3')
return
} else if (reaction.emoji.toString() == reactions.left.emoji.toString()) {
if (pages[page - 1]) {
page--
await message.edit(pages[page])
}
} else if (reaction.emoji.toString() == reactions.right.emoji.toString()) {
if (pages[page + 1]) {
page++
await message.edit(pages[page])
}
}
reaction.users.remove(user.id).catch(none)
})
collector.on('end', async (collected, reason) => {
if (opts.removeAtEnd && reason != '123M0V3') {
for (const reaction of Object.values(reactions)) {
await reaction.remove().catch(none)
}
}
if (reason == '123M0V3') await message.delete()
})
}
}
module.exports = Paginator

55
package.json Normal file
View File

@ -0,0 +1,55 @@
{
"_from": "github:mudkipscience/discord-paginator.js",
"_id": "discord-paginator.js@1.0.2",
"_inBundle": false,
"_integrity": "",
"_location": "/discord-paginator.js",
"_phantomChildren": {},
"_requested": {
"type": "git",
"raw": "discord-paginator.js@github:mudkipscience/discord-paginator.js",
"name": "discord-paginator.js",
"escapedName": "discord-paginator.js",
"rawSpec": "github:mudkipscience/discord-paginator.js",
"saveSpec": "github:mudkipscience/discord-paginator.js",
"fetchSpec": null,
"gitCommittish": null
},
"_requiredBy": [
"/"
],
"_resolved": "github:mudkipscience/discord-paginator.js#c0969fb34119fd0f5a9af9c75bc6fa39f680993c",
"_spec": "discord-paginator.js@github:mudkipscience/discord-paginator.js",
"_where": "/home/container",
"author": {
"name": "Azusfin#9887"
},
"bugs": {
"url": "https://github.com/XzFirzal/discord-paginator.js/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "A lightweight package to paginate discord message for discord bots",
"homepage": "https://github.com/XzFirzal/discord-paginator.js",
"keywords": [
"paginate",
"discord",
"discord.js",
"menus",
"discord-menu",
"discord-pagination",
"menus",
"pagination"
],
"license": "MIT",
"main": "index.js",
"name": "discord-paginator.js",
"repository": {
"type": "git",
"url": "git+https://github.com/XzFirzal/discord-paginator.js.git"
},
"scripts": {
"test": "node ."
},
"version": "1.0.2"
}