adds hacky support for paginator cloning

This commit is contained in:
bignutty 2024-10-17 18:21:29 +02:00
parent b368717d87
commit a815c7db08
3 changed files with 38 additions and 4 deletions

View file

@ -48,7 +48,8 @@ async function quoraPaginator(context, pages, refMappings, currentRef) {
"previous",
"next",
"search"
]
],
disableCloning: true
});
paging.on("interaction", async ({ context: ctx, listener }) => {

View file

@ -1,20 +1,25 @@
const EventEmitter = require("eventemitter3");
const { Context } = require("detritus-client/lib/command");
const { editOrReply } = require("#utils/message");
const { MessageFlags } = require("detritus-client/lib/constants");
module.exports = class BasePaginator extends EventEmitter {
constructor(client, data) {
super();
this.client = client;
this.data = data;
this.message = BasePaginator.asMessage(data.context);
this.commandMessage = data.commandMessage || null;
this.pages = data.pages;
this.index = 0;
this.index = data.index || 0;
this.ephemeral = data.ephemeral || false;
this.context = data.context;
this.targetUser = data.targetUser || this.message?.author?.id || data.context?.user?.id;
this.disableCloning = data.disableCloning || false;
this.isInteractionPaginator = false;
this.editOrReply;
if(data.context.editOrReply) this.editOrReply = editOrReply.bind(data.context);
@ -31,7 +36,7 @@ module.exports = class BasePaginator extends EventEmitter {
get isShared() {
return this.commandMessage instanceof Map;
}
isCommandMessage(messageId) {
if (!this.commandMessage) return false;
@ -68,6 +73,12 @@ module.exports = class BasePaginator extends EventEmitter {
if(!msg.message_reference) msg.reference = true
if(!msg.allowedMentions) msg.allowedMentions = {parse: [], repliedUser: false}
if(this.ephemeral){
msg.flags = MessageFlags.EPHEMERAL
this.commandMessage = await this.context.createMessage(msg);
return this.commandMessage;
}
return this.commandMessage = await this.editOrReply(this.context, msg);
}

View file

@ -1,5 +1,6 @@
const { COMPONENT_BUTTON_ICONS } = require("#constants");
const { icon } = require("#utils/markdown");
const { MessageFlags } = require("detritus-client/lib/constants");
const InteractionPaginator = require("./InteractionPaginator");
const assert = require("assert");
@ -63,7 +64,28 @@ module.exports = class Paginator {
// 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)
// If ephemeral cloning is disabled ignore the interaction
if(listener.disableCloning) return await context.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE);
await context.respond(InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE, { flags: MessageFlags.EPHEMERAL });
// (this is very hacky but the best i can think of right now)
// clones the current paginator and attaches a new one to that response
// i have plans to fix this with pagination v2 whenever i get around to working on it
let newPaginator = Object.assign(Object.create(Object.getPrototypeOf(listener)), listener);
newPaginator.context = context;
newPaginator.targetUser = context.user.id;
newPaginator.ephemeral = true;
if(context.customId == "next") await newPaginator.getNext()
else if(context.customId == "previous") await newPaginator.getPrevious()
this.activeListeners.push(newPaginator);
await newPaginator.init();
return;
}