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 { Constants } = require('detritus-client');
|
||||||
|
const { InteractionCallbackTypes } = require('detritus-client/lib/constants');
|
||||||
const { paginator } = require('../../../../labscore/client');
|
const { paginator } = require('../../../../labscore/client');
|
||||||
|
|
||||||
const createEmbedMessage = (title, description) => ({
|
const createEmbedMessage = (title, description) => ({
|
||||||
|
@ -20,9 +21,18 @@ module.exports = {
|
||||||
createEmbedMessage("Page2", "eee"),
|
createEmbedMessage("Page2", "eee"),
|
||||||
createEmbedMessage("Page 3", "h")
|
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,
|
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.index = 0;
|
||||||
this.targetUser = data.targetUser || this.message.author.id;
|
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);
|
this.editOrReply = data.context.editOrReply.bind(data.context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,43 +2,31 @@ const InteractionPaginator = require("./InteractionPaginator");
|
||||||
const assert = require("assert");
|
const assert = require("assert");
|
||||||
|
|
||||||
const { Constants, Utils } = require('detritus-client')
|
const { Constants, Utils } = require('detritus-client')
|
||||||
const { Components, ComponentActionRow } = Utils
|
const { Components } = Utils
|
||||||
const { InteractionCallbackTypes } = Constants
|
const { InteractionCallbackTypes } = Constants
|
||||||
|
|
||||||
const allowedEvents = new Set([
|
const allowedEvents = new Set([
|
||||||
"MESSAGE_CREATE"
|
"MESSAGE_CREATE"
|
||||||
]);
|
]);
|
||||||
|
|
||||||
function deprecate(message) {
|
|
||||||
console.warn(`[detritus-pagination] Deprecation warning: ${message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ButtonEmoji = Object.freeze({
|
const ButtonEmoji = Object.freeze({
|
||||||
NEXT: '<:right:977871577758707782>',
|
NEXT: '<:right:977871577758707782>',
|
||||||
PREVIOUS: '<:left:977871577532211200>',
|
PREVIOUS: '<:left:977871577532211200>',
|
||||||
STOP: '<:ico_trash:929498022386221096>',
|
STOP: '<:ico_trash:929498022386221096>',
|
||||||
// NEXT: '<:next_page:977894273376727050>',
|
UNKNOWN: '<:ico_question:949420315677691934>'
|
||||||
// PREVIOUS: '<:previous_page:977894273443844167>'
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const { hasOwnProperty } = Object.prototype;
|
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();
|
const instances = new WeakSet();
|
||||||
|
|
||||||
module.exports = class Paginator {
|
module.exports = class Paginator {
|
||||||
constructor(client, data = {}) {
|
constructor(client, data = {}) {
|
||||||
if (instances.has(client)) {
|
if (instances.has(client)) {
|
||||||
deprecate("Avoid attaching multiple Paginators to the same client, as it can lead to memory leaks");
|
throw "Only attach one pagination client"
|
||||||
} else {
|
|
||||||
instances.add(client);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.ok(
|
assert.ok(hasOwnProperty.call(client, "gateway"), "Provided `client` has no `gateway` property. Use PaginationCluster.");
|
||||||
hasOwnProperty.call(client, "gateway"),
|
|
||||||
"Provided `client` has no `gateway` property. Consider using `require('detritus-pagination').PaginatorCluster` if you're using CommandClient."
|
|
||||||
);
|
|
||||||
|
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.maxTime = data.maxTime || 300000;
|
this.maxTime = data.maxTime || 300000;
|
||||||
|
@ -69,26 +57,24 @@ module.exports = class Paginator {
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleButtonEvent(context) {
|
async handleButtonEvent(context) {
|
||||||
// Get listener
|
|
||||||
let listener;
|
let listener;
|
||||||
for (const l of this.activeListeners) {
|
for (const l of this.activeListeners) {
|
||||||
if (!(l instanceof InteractionPaginator)) continue;
|
if (!(l instanceof InteractionPaginator)) continue;
|
||||||
if (!l.commandMessage) continue;
|
if (!l.commandMessage) continue;
|
||||||
|
|
||||||
if (l.isCommandMessage(context.message.id)) {
|
if (l.isCommandMessage(context.message.id)) {
|
||||||
listener = l
|
listener = l
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If person that interacted isnt the target, send a generic ping response and ignore it
|
||||||
if (!listener.isTarget(context.user.id)) {
|
if (!listener.isTarget(context.user.id)) {
|
||||||
await context.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE)
|
await context.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Respond
|
||||||
switch (context.customId) {
|
switch (context.customId) {
|
||||||
case "next":
|
case "next":
|
||||||
//await context.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE)
|
|
||||||
//listener.next();
|
|
||||||
await context.respond(InteractionCallbackTypes.UPDATE_MESSAGE, await listener.getNext())
|
await context.respond(InteractionCallbackTypes.UPDATE_MESSAGE, await listener.getNext())
|
||||||
break;
|
break;
|
||||||
case "previous":
|
case "previous":
|
||||||
|
@ -98,7 +84,13 @@ module.exports = class Paginator {
|
||||||
await context.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE)
|
await context.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE)
|
||||||
listener.stop();
|
listener.stop();
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
// Emit the button as an event
|
||||||
|
listener.emit("interaction", { context, listener });
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleMessageEvent(data, listener) {
|
async handleMessageEvent(data, listener) {
|
||||||
|
@ -119,30 +111,31 @@ module.exports = class Paginator {
|
||||||
}
|
}
|
||||||
|
|
||||||
async components(listener) {
|
async components(listener) {
|
||||||
|
|
||||||
const components = new Components({
|
const components = new Components({
|
||||||
timeout: this.expires,
|
timeout: this.expires,
|
||||||
run: this.handleButtonEvent.bind(this),
|
run: this.handleButtonEvent.bind(this),
|
||||||
});
|
});
|
||||||
|
|
||||||
components.createButton({
|
for(const b of this.buttons){
|
||||||
customId: "previous",
|
// If an object is provided, build button from that
|
||||||
disabled: 0,
|
if(typeof b == "object") {
|
||||||
style: 2,
|
components.createButton(Object.assign({
|
||||||
emoji: ButtonEmoji.PREVIOUS
|
customId: "custom",
|
||||||
});
|
disabled: 0,
|
||||||
components.createButton({
|
style: 2,
|
||||||
customId: "next",
|
emoji: ButtonEmoji.UNKNOWN
|
||||||
disabled: 0,
|
}, b));
|
||||||
style: 2,
|
} else {
|
||||||
emoji: ButtonEmoji.NEXT
|
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;
|
return components;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,6 +165,9 @@ module.exports = class Paginator {
|
||||||
instance.stop(true);
|
instance.stop(true);
|
||||||
}, data.maxTime || this.maxTime);
|
}, 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) {
|
if (instance.commandMessage === null && data.pages) {
|
||||||
await instance.init();
|
await instance.init();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue