mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-08 14:13:02 -04:00
[core] fully migrate to InteractionPaginator
This commit is contained in:
parent
b1cc10c6ef
commit
eb0ba140f3
15 changed files with 176 additions and 303 deletions
|
@ -22,11 +22,6 @@ const categories = {
|
|||
"dev": `${icon("analytics")} Development`
|
||||
}
|
||||
|
||||
const reactions = {
|
||||
previousPage: "⬅️",
|
||||
nextPage: "➡️"
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
name: 'help',
|
||||
label: 'command',
|
||||
|
@ -58,10 +53,9 @@ module.exports = {
|
|||
|
||||
pages = formatPaginationEmbeds(pages)
|
||||
const message = context.message
|
||||
const paging = await paginator.createReactionPaginator({
|
||||
const paging = await paginator.createPaginator({
|
||||
message,
|
||||
pages,
|
||||
reactions
|
||||
pages
|
||||
});
|
||||
}
|
||||
},
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
const { Constants } = require('detritus-client');
|
||||
const { paginator } = require('../../../../labscore/client');
|
||||
|
||||
const reactions = {
|
||||
previousPage: "⬅️",
|
||||
nextPage: "➡️"
|
||||
};
|
||||
|
||||
const createEmbedMessage = (title, description) => ({
|
||||
embeds: [{ title, description }]
|
||||
});
|
||||
|
@ -25,13 +20,11 @@ module.exports = {
|
|||
createEmbedMessage("Page2", "eee"),
|
||||
createEmbedMessage("Page 3", "h")
|
||||
]
|
||||
const paging = await paginator.createReactionPaginator({
|
||||
const paging = await paginator.createPaginator({
|
||||
// message is the message the user has sent
|
||||
message,
|
||||
// pages is an array of pages (will be passed as parameter in Message#edit)
|
||||
pages,
|
||||
// reactions is an object that includes `previousPage` and `nextPage` emojis (defined above)
|
||||
reactions
|
||||
pages
|
||||
});
|
||||
},
|
||||
};
|
|
@ -25,11 +25,6 @@ function createImageResultPage(context, result){
|
|||
return res;
|
||||
}
|
||||
|
||||
const reactions = {
|
||||
previousPage: "⬅️",
|
||||
nextPage: "➡️"
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
name: 'bingimage',
|
||||
label: 'query',
|
||||
|
@ -54,10 +49,9 @@ module.exports = {
|
|||
|
||||
pages = formatPaginationEmbeds(pages)
|
||||
const message = context.message
|
||||
const paging = await paginator.createReactionPaginator({
|
||||
const paging = await paginator.createPaginator({
|
||||
message,
|
||||
pages,
|
||||
reactions
|
||||
pages
|
||||
});
|
||||
}catch(e){
|
||||
console.log(e)
|
||||
|
|
|
@ -21,11 +21,6 @@ function createSearchResultPage(context, result){
|
|||
return res;
|
||||
}
|
||||
|
||||
const reactions = {
|
||||
previousPage: "⬅️",
|
||||
nextPage: "➡️"
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
name: 'bing',
|
||||
label: 'query',
|
||||
|
@ -50,10 +45,9 @@ module.exports = {
|
|||
|
||||
pages = formatPaginationEmbeds(pages)
|
||||
const message = context.message
|
||||
const paging = await paginator.createReactionPaginator({
|
||||
const paging = await paginator.createPaginator({
|
||||
message,
|
||||
pages,
|
||||
reactions
|
||||
pages
|
||||
});
|
||||
}catch(e){
|
||||
console.log(e)
|
||||
|
|
|
@ -25,11 +25,6 @@ function createImageResultPage(context, result){
|
|||
return res;
|
||||
}
|
||||
|
||||
const reactions = {
|
||||
previousPage: "⬅️",
|
||||
nextPage: "➡️"
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
name: 'image',
|
||||
label: 'query',
|
||||
|
@ -54,10 +49,9 @@ module.exports = {
|
|||
|
||||
pages = formatPaginationEmbeds(pages)
|
||||
const message = context.message
|
||||
const paging = await paginator.createReactionPaginator({
|
||||
const paging = await paginator.createPaginator({
|
||||
message,
|
||||
pages,
|
||||
reactions
|
||||
pages
|
||||
});
|
||||
}catch(e){
|
||||
console.log(e)
|
||||
|
|
|
@ -22,11 +22,6 @@ function createSearchResultPage(context, result){
|
|||
return res;
|
||||
}
|
||||
|
||||
const reactions = {
|
||||
previousPage: "⬅️",
|
||||
nextPage: "➡️"
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
name: 'google',
|
||||
label: 'query',
|
||||
|
@ -51,10 +46,9 @@ module.exports = {
|
|||
|
||||
pages = formatPaginationEmbeds(pages)
|
||||
const message = context.message
|
||||
const paging = await paginator.createReactionPaginator({
|
||||
const paging = await paginator.createPaginator({
|
||||
message,
|
||||
pages,
|
||||
reactions
|
||||
pages
|
||||
});
|
||||
}catch(e){
|
||||
console.log(e)
|
||||
|
|
|
@ -5,11 +5,6 @@ const { STATICS } = require('../../../labscore/utils/statics')
|
|||
const { lyrics } = require('../../../labscore/api');
|
||||
const { paginator } = require('../../../labscore/client');
|
||||
|
||||
const reactions = {
|
||||
previousPage: "⬅️",
|
||||
nextPage: "➡️"
|
||||
};
|
||||
|
||||
function createLyricsPage(context, search, fields){
|
||||
let em= createEmbed("default", context, {
|
||||
description: `**${search.body.song.title}**\n*Released ${search.body.song.release}*\n\n`,
|
||||
|
@ -70,10 +65,9 @@ module.exports = {
|
|||
const message = context.message
|
||||
|
||||
pages = formatPaginationEmbeds(pages)
|
||||
const paging = await paginator.createReactionPaginator({
|
||||
const paging = await paginator.createPaginator({
|
||||
message,
|
||||
pages,
|
||||
reactions
|
||||
pages
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -23,11 +23,6 @@ function createWolframPage(context, pod){
|
|||
return res;
|
||||
}
|
||||
|
||||
const reactions = {
|
||||
previousPage: "⬅️",
|
||||
nextPage: "➡️"
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
name: 'wolframalpha',
|
||||
label: 'query',
|
||||
|
@ -52,10 +47,9 @@ module.exports = {
|
|||
|
||||
pages = formatPaginationEmbeds(pages)
|
||||
const message = context.message
|
||||
const paging = await paginator.createReactionPaginator({
|
||||
const paging = await paginator.createPaginator({
|
||||
message,
|
||||
pages,
|
||||
reactions
|
||||
pages
|
||||
});
|
||||
}catch(e){
|
||||
console.log(e)
|
||||
|
|
|
@ -21,7 +21,7 @@ const cluster = new ClusterClient("", {
|
|||
|
||||
// Create this clusters paginator
|
||||
const paginator = new Paginator(cluster, {
|
||||
maxTime: 120000,
|
||||
maxTime: 300000,
|
||||
pageLoop: true,
|
||||
pageNumber: true
|
||||
});
|
||||
|
|
|
@ -11,9 +11,7 @@ module.exports = class BasePaginator extends EventEmitter {
|
|||
this.index = 0;
|
||||
this.targetUser = data.targetUser || this.message.author.id;
|
||||
|
||||
// Reference to reply function
|
||||
// Uses context.editOrReply if an instance of Context was passed
|
||||
// Defaults to message.reply
|
||||
// TODO: use editOrReply, kill old paginator if it exists
|
||||
this.editOrReply = (data.message.editOrReply || data.message.reply).bind(data.message);
|
||||
}
|
||||
|
||||
|
|
8
labscore/paginator/structures/InteractionPaginator.js
Normal file
8
labscore/paginator/structures/InteractionPaginator.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
const BasePaginator = require("./BasePaginator");
|
||||
|
||||
module.exports = class InteractionPaginator extends BasePaginator {
|
||||
constructor(client, data) {
|
||||
super(client, data);
|
||||
this.waitingForPage = null;
|
||||
}
|
||||
};
|
|
@ -1,4 +1,4 @@
|
|||
const ReactionPaginator = require("./ReactionPaginator");
|
||||
const InteractionPaginator = require("./InteractionPaginator");
|
||||
const assert = require("assert");
|
||||
|
||||
const { Constants, Utils } = require('detritus-client')
|
||||
|
@ -6,7 +6,6 @@ const { Components, ComponentActionRow } = Utils
|
|||
const { InteractionCallbackTypes } = Constants
|
||||
|
||||
const allowedEvents = new Set([
|
||||
"MESSAGE_REACTION_ADD",
|
||||
"MESSAGE_CREATE"
|
||||
]);
|
||||
|
||||
|
@ -17,7 +16,9 @@ function deprecate(message) {
|
|||
const ButtonEmoji = Object.freeze({
|
||||
NEXT: '<:right:977871577758707782>',
|
||||
PREVIOUS: '<:left:977871577532211200>',
|
||||
STOP: '<:ico_trash:929498022386221096>'
|
||||
STOP: '<:ico_trash:929498022386221096>',
|
||||
// NEXT: '<:next_page:977894273376727050>',
|
||||
// PREVIOUS: '<:previous_page:977894273443844167>'
|
||||
})
|
||||
|
||||
const { hasOwnProperty } = Object.prototype;
|
||||
|
@ -54,13 +55,10 @@ module.exports = class Paginator {
|
|||
if (!allowedEvents.has(event)) return;
|
||||
|
||||
for (const listener of this.activeListeners) {
|
||||
if (!(listener instanceof ReactionPaginator)) continue;
|
||||
if (!(listener instanceof InteractionPaginator)) continue;
|
||||
if (!listener.commandMessage) continue;
|
||||
|
||||
if (listener.isCommandMessage(data.message_id) &&
|
||||
listener.isTarget(data.user_id)) {
|
||||
await this.handleReactionEvent(data, listener);
|
||||
} else if (event === "MESSAGE_CREATE" &&
|
||||
if (event === "MESSAGE_CREATE" &&
|
||||
listener.isInChannel(data.channel_id) &&
|
||||
listener.isTarget(data.user_id) &&
|
||||
listener.waitingForPage) {
|
||||
|
@ -74,7 +72,7 @@ module.exports = class Paginator {
|
|||
// Get listener
|
||||
let listener;
|
||||
for (const l of this.activeListeners) {
|
||||
if (!(l instanceof ReactionPaginator)) continue;
|
||||
if (!(l instanceof InteractionPaginator)) continue;
|
||||
if (!l.commandMessage) continue;
|
||||
|
||||
if (l.isCommandMessage(context.message.id)) {
|
||||
|
@ -103,38 +101,6 @@ module.exports = class Paginator {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: Clean up legacy code from ReactionPaginator
|
||||
|
||||
// Legacy
|
||||
async handleReactionEvent(data, listener) {
|
||||
switch (data.emoji.name) {
|
||||
case listener.reactions.nextPage:
|
||||
listener.next();
|
||||
break;
|
||||
case listener.reactions.previousPage:
|
||||
listener.previous();
|
||||
break;
|
||||
case listener.reactions.firstPage:
|
||||
listener.jumpTo(0);
|
||||
break;
|
||||
case listener.reactions.lastPage:
|
||||
listener.jumpTo(listener.pages.length - 1);
|
||||
break;
|
||||
case listener.reactions.stop:
|
||||
listener.stop();
|
||||
break;
|
||||
case listener.reactions.skipTo:
|
||||
if (listener.waitingForPage) return;
|
||||
listener.waitingForPage = await this.client.rest.createMessage(data.channel_id, "What page do you want to go to?");
|
||||
break;
|
||||
default:
|
||||
if (!Object.values(listener.reactions).includes(data.emoji.name)) return;
|
||||
}
|
||||
|
||||
listener.emit("raw", data);
|
||||
listener.clearReaction(data.emoji.name);
|
||||
}
|
||||
|
||||
async handleMessageEvent(data, listener) {
|
||||
const page = parseInt(data.content, 10);
|
||||
if (isNaN(page)) {
|
||||
|
@ -180,7 +146,7 @@ module.exports = class Paginator {
|
|||
return components;
|
||||
}
|
||||
|
||||
async createReactionPaginator(data) {
|
||||
async createPaginator(data) {
|
||||
if (this.pageNumber && Array.isArray(data.pages)) {
|
||||
for (let i = 0; i < data.pages.length; ++i) {
|
||||
const element = data.pages[i];
|
||||
|
@ -188,7 +154,7 @@ module.exports = class Paginator {
|
|||
}
|
||||
}
|
||||
|
||||
const instance = new ReactionPaginator(this, data);
|
||||
const instance = new InteractionPaginator(this, data);
|
||||
this.activeListeners.push(instance);
|
||||
|
||||
setTimeout(() => {
|
||||
|
@ -199,7 +165,6 @@ module.exports = class Paginator {
|
|||
await instance.init();
|
||||
}
|
||||
|
||||
//await instance.addReactions();
|
||||
return instance;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -29,9 +29,9 @@ module.exports = class PaginatorCluster {
|
|||
return paginator;
|
||||
}
|
||||
|
||||
createReactionPaginator(data) {
|
||||
createPaginator(data) {
|
||||
const targetPaginator = this.findOrSetPaginator(data.message.client);
|
||||
|
||||
return targetPaginator.createReactionPaginator(data);
|
||||
return targetPaginator.createPaginator(data);
|
||||
}
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
const BasePaginator = require("./BasePaginator");
|
||||
|
||||
module.exports = class ReactionPaginator extends BasePaginator {
|
||||
constructor(client, data) {
|
||||
super(client, data);
|
||||
this.waitingForPage = null;
|
||||
this.reactions = data.reactions || {
|
||||
firstPage: "⏮️",
|
||||
previousPage: "⬅️",
|
||||
nextPage: "➡️",
|
||||
lastPage: "⏭️",
|
||||
skipTo: "🔢",
|
||||
stop: "⏹️"
|
||||
};
|
||||
}
|
||||
|
||||
async addReactions() {
|
||||
if (!this.commandMessage) return;
|
||||
|
||||
for (const reactions of Object.values(this.reactions)) {
|
||||
if (this.isShared) {
|
||||
for (const msg of this.commandMessage.values()) {
|
||||
await msg.react(reactions).catch();
|
||||
}
|
||||
} else {
|
||||
await this.commandMessage.react(reactions).catch(() => {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: this only works if cache is enabled
|
||||
// perhaps add option to use REST API to fetch all reactions?
|
||||
async clearReactions() {
|
||||
const reactions = this.isShared ? Array.from(this.commandMessage.values()).map(x => Array.from(x.reactions.values())).flat() : this.commandMessage.reactions.values();
|
||||
|
||||
for (const reaction of reactions) {
|
||||
this.clearReaction(reaction.emoji.name);
|
||||
}
|
||||
}
|
||||
|
||||
async clearReaction(emoji) {
|
||||
const reaction = this.commandMessage.reactions.find(x => x.emoji.name === emoji);
|
||||
|
||||
if (reaction) {
|
||||
reaction.delete(this.message.author.id).catch(() => {});
|
||||
}
|
||||
}
|
||||
};
|
|
@ -9,7 +9,6 @@
|
|||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"detritus-client": "^0.17.0-beta.12",
|
||||
"detritus-pagination": "^1.4.0",
|
||||
"dotenv": "^16.0.1",
|
||||
"express": "^4.17.1",
|
||||
"superagent": "^7.1.1"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue