mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-08 22:23:03 -04:00
[core](paginator) custom button system
This commit is contained in:
parent
72828b408f
commit
6526b79c86
3 changed files with 46 additions and 41 deletions
|
@ -1,4 +1,5 @@
|
|||
const { Constants } = require('detritus-client');
|
||||
const { InteractionCallbackTypes } = require('detritus-client/lib/constants');
|
||||
const { paginator } = require('../../../../labscore/client');
|
||||
|
||||
const createEmbedMessage = (title, description) => ({
|
||||
|
@ -20,9 +21,18 @@ module.exports = {
|
|||
createEmbedMessage("Page2", "eee"),
|
||||
createEmbedMessage("Page 3", "h")
|
||||
]
|
||||
const paging = await paginator.createPaginator({
|
||||
const buttons = ["previous","next", {customId: "german", emoji: `<:german:856501761857683486>`, style: 4}]
|
||||
const page = await paginator.createPaginator({
|
||||
context,
|
||||
pages
|
||||
pages,
|
||||
buttons
|
||||
});
|
||||
|
||||
// Custom button example, event only triggers for
|
||||
page.on("interaction", async (e) => {
|
||||
if(e.context.customId == "german"){
|
||||
await e.context.respond(InteractionCallbackTypes.UPDATE_MESSAGE, {"content": "german button activated"})
|
||||
}
|
||||
})
|
||||
},
|
||||
};
|
|
@ -11,7 +11,6 @@ module.exports = class BasePaginator extends EventEmitter {
|
|||
this.index = 0;
|
||||
this.targetUser = data.targetUser || this.message.author.id;
|
||||
|
||||
// TODO: use editOrReply, kill old paginator if it exists
|
||||
this.editOrReply = data.context.editOrReply.bind(data.context);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,43 +2,31 @@ const InteractionPaginator = require("./InteractionPaginator");
|
|||
const assert = require("assert");
|
||||
|
||||
const { Constants, Utils } = require('detritus-client')
|
||||
const { Components, ComponentActionRow } = Utils
|
||||
const { Components } = Utils
|
||||
const { InteractionCallbackTypes } = Constants
|
||||
|
||||
const allowedEvents = new Set([
|
||||
"MESSAGE_CREATE"
|
||||
]);
|
||||
|
||||
function deprecate(message) {
|
||||
console.warn(`[detritus-pagination] Deprecation warning: ${message}`);
|
||||
}
|
||||
|
||||
const ButtonEmoji = Object.freeze({
|
||||
NEXT: '<:right:977871577758707782>',
|
||||
PREVIOUS: '<:left:977871577532211200>',
|
||||
STOP: '<:ico_trash:929498022386221096>',
|
||||
// NEXT: '<:next_page:977894273376727050>',
|
||||
// PREVIOUS: '<:previous_page:977894273443844167>'
|
||||
UNKNOWN: '<:ico_question:949420315677691934>'
|
||||
})
|
||||
|
||||
const { hasOwnProperty } = Object.prototype;
|
||||
|
||||
// Keep track of created instances in a WeakSet to prevent memory leaks
|
||||
// We do this to notify the user when a Paginator is attached to the same client
|
||||
const instances = new WeakSet();
|
||||
|
||||
module.exports = class Paginator {
|
||||
constructor(client, data = {}) {
|
||||
if (instances.has(client)) {
|
||||
deprecate("Avoid attaching multiple Paginators to the same client, as it can lead to memory leaks");
|
||||
} else {
|
||||
instances.add(client);
|
||||
throw "Only attach one pagination client"
|
||||
}
|
||||
|
||||
assert.ok(
|
||||
hasOwnProperty.call(client, "gateway"),
|
||||
"Provided `client` has no `gateway` property. Consider using `require('detritus-pagination').PaginatorCluster` if you're using CommandClient."
|
||||
);
|
||||
assert.ok(hasOwnProperty.call(client, "gateway"), "Provided `client` has no `gateway` property. Use PaginationCluster.");
|
||||
|
||||
this.client = client;
|
||||
this.maxTime = data.maxTime || 300000;
|
||||
|
@ -69,26 +57,24 @@ module.exports = class Paginator {
|
|||
}
|
||||
|
||||
async handleButtonEvent(context) {
|
||||
// Get listener
|
||||
let listener;
|
||||
for (const l of this.activeListeners) {
|
||||
if (!(l instanceof InteractionPaginator)) continue;
|
||||
if (!l.commandMessage) continue;
|
||||
|
||||
if (l.isCommandMessage(context.message.id)) {
|
||||
listener = l
|
||||
}
|
||||
}
|
||||
|
||||
// If person that interacted isnt the target, send a generic ping response and ignore it
|
||||
if (!listener.isTarget(context.user.id)) {
|
||||
await context.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE)
|
||||
return;
|
||||
}
|
||||
|
||||
// Respond
|
||||
switch (context.customId) {
|
||||
case "next":
|
||||
//await context.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE)
|
||||
//listener.next();
|
||||
await context.respond(InteractionCallbackTypes.UPDATE_MESSAGE, await listener.getNext())
|
||||
break;
|
||||
case "previous":
|
||||
|
@ -98,7 +84,13 @@ module.exports = class Paginator {
|
|||
await context.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE)
|
||||
listener.stop();
|
||||
break;
|
||||
default:
|
||||
// Emit the button as an event
|
||||
listener.emit("interaction", { context, listener });
|
||||
break;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
async handleMessageEvent(data, listener) {
|
||||
|
@ -119,30 +111,31 @@ module.exports = class Paginator {
|
|||
}
|
||||
|
||||
async components(listener) {
|
||||
|
||||
const components = new Components({
|
||||
timeout: this.expires,
|
||||
run: this.handleButtonEvent.bind(this),
|
||||
});
|
||||
|
||||
components.createButton({
|
||||
customId: "previous",
|
||||
disabled: 0,
|
||||
style: 2,
|
||||
emoji: ButtonEmoji.PREVIOUS
|
||||
});
|
||||
components.createButton({
|
||||
customId: "next",
|
||||
disabled: 0,
|
||||
style: 2,
|
||||
emoji: ButtonEmoji.NEXT
|
||||
});
|
||||
for(const b of this.buttons){
|
||||
// If an object is provided, build button from that
|
||||
if(typeof b == "object") {
|
||||
components.createButton(Object.assign({
|
||||
customId: "custom",
|
||||
disabled: 0,
|
||||
style: 2,
|
||||
emoji: ButtonEmoji.UNKNOWN
|
||||
}, b));
|
||||
} else {
|
||||
components.createButton({
|
||||
customId: b,
|
||||
disabled: 0,
|
||||
style: 2,
|
||||
emoji: ButtonEmoji[b.toUpperCase()]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//components.createButton({
|
||||
// customId: "stop",
|
||||
// disabled: 0,
|
||||
// style: 2,
|
||||
// emoji: ButtonEmoji.STOP
|
||||
//});
|
||||
return components;
|
||||
}
|
||||
|
||||
|
@ -172,6 +165,9 @@ module.exports = class Paginator {
|
|||
instance.stop(true);
|
||||
}, data.maxTime || this.maxTime);
|
||||
|
||||
// Edit below to change default button set
|
||||
this.buttons = typeof data.buttons !== "object" ? ["previous", "next"] : data.buttons;
|
||||
|
||||
if (instance.commandMessage === null && data.pages) {
|
||||
await instance.init();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue