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`
|
"dev": `${icon("analytics")} Development`
|
||||||
}
|
}
|
||||||
|
|
||||||
const reactions = {
|
|
||||||
previousPage: "⬅️",
|
|
||||||
nextPage: "➡️"
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'help',
|
name: 'help',
|
||||||
label: 'command',
|
label: 'command',
|
||||||
|
@ -58,10 +53,9 @@ module.exports = {
|
||||||
|
|
||||||
pages = formatPaginationEmbeds(pages)
|
pages = formatPaginationEmbeds(pages)
|
||||||
const message = context.message
|
const message = context.message
|
||||||
const paging = await paginator.createReactionPaginator({
|
const paging = await paginator.createPaginator({
|
||||||
message,
|
message,
|
||||||
pages,
|
pages
|
||||||
reactions
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,11 +1,6 @@
|
||||||
const { Constants } = require('detritus-client');
|
const { Constants } = require('detritus-client');
|
||||||
const { paginator } = require('../../../../labscore/client');
|
const { paginator } = require('../../../../labscore/client');
|
||||||
|
|
||||||
const reactions = {
|
|
||||||
previousPage: "⬅️",
|
|
||||||
nextPage: "➡️"
|
|
||||||
};
|
|
||||||
|
|
||||||
const createEmbedMessage = (title, description) => ({
|
const createEmbedMessage = (title, description) => ({
|
||||||
embeds: [{ title, description }]
|
embeds: [{ title, description }]
|
||||||
});
|
});
|
||||||
|
@ -25,13 +20,11 @@ module.exports = {
|
||||||
createEmbedMessage("Page2", "eee"),
|
createEmbedMessage("Page2", "eee"),
|
||||||
createEmbedMessage("Page 3", "h")
|
createEmbedMessage("Page 3", "h")
|
||||||
]
|
]
|
||||||
const paging = await paginator.createReactionPaginator({
|
const paging = await paginator.createPaginator({
|
||||||
// message is the message the user has sent
|
// message is the message the user has sent
|
||||||
message,
|
message,
|
||||||
// pages is an array of pages (will be passed as parameter in Message#edit)
|
// pages is an array of pages (will be passed as parameter in Message#edit)
|
||||||
pages,
|
pages
|
||||||
// reactions is an object that includes `previousPage` and `nextPage` emojis (defined above)
|
|
||||||
reactions
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
|
@ -25,11 +25,6 @@ function createImageResultPage(context, result){
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
const reactions = {
|
|
||||||
previousPage: "⬅️",
|
|
||||||
nextPage: "➡️"
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'bingimage',
|
name: 'bingimage',
|
||||||
label: 'query',
|
label: 'query',
|
||||||
|
@ -54,10 +49,9 @@ module.exports = {
|
||||||
|
|
||||||
pages = formatPaginationEmbeds(pages)
|
pages = formatPaginationEmbeds(pages)
|
||||||
const message = context.message
|
const message = context.message
|
||||||
const paging = await paginator.createReactionPaginator({
|
const paging = await paginator.createPaginator({
|
||||||
message,
|
message,
|
||||||
pages,
|
pages
|
||||||
reactions
|
|
||||||
});
|
});
|
||||||
}catch(e){
|
}catch(e){
|
||||||
console.log(e)
|
console.log(e)
|
||||||
|
|
|
@ -21,11 +21,6 @@ function createSearchResultPage(context, result){
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
const reactions = {
|
|
||||||
previousPage: "⬅️",
|
|
||||||
nextPage: "➡️"
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'bing',
|
name: 'bing',
|
||||||
label: 'query',
|
label: 'query',
|
||||||
|
@ -50,10 +45,9 @@ module.exports = {
|
||||||
|
|
||||||
pages = formatPaginationEmbeds(pages)
|
pages = formatPaginationEmbeds(pages)
|
||||||
const message = context.message
|
const message = context.message
|
||||||
const paging = await paginator.createReactionPaginator({
|
const paging = await paginator.createPaginator({
|
||||||
message,
|
message,
|
||||||
pages,
|
pages
|
||||||
reactions
|
|
||||||
});
|
});
|
||||||
}catch(e){
|
}catch(e){
|
||||||
console.log(e)
|
console.log(e)
|
||||||
|
|
|
@ -25,11 +25,6 @@ function createImageResultPage(context, result){
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
const reactions = {
|
|
||||||
previousPage: "⬅️",
|
|
||||||
nextPage: "➡️"
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'image',
|
name: 'image',
|
||||||
label: 'query',
|
label: 'query',
|
||||||
|
@ -54,10 +49,9 @@ module.exports = {
|
||||||
|
|
||||||
pages = formatPaginationEmbeds(pages)
|
pages = formatPaginationEmbeds(pages)
|
||||||
const message = context.message
|
const message = context.message
|
||||||
const paging = await paginator.createReactionPaginator({
|
const paging = await paginator.createPaginator({
|
||||||
message,
|
message,
|
||||||
pages,
|
pages
|
||||||
reactions
|
|
||||||
});
|
});
|
||||||
}catch(e){
|
}catch(e){
|
||||||
console.log(e)
|
console.log(e)
|
||||||
|
|
|
@ -22,11 +22,6 @@ function createSearchResultPage(context, result){
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
const reactions = {
|
|
||||||
previousPage: "⬅️",
|
|
||||||
nextPage: "➡️"
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'google',
|
name: 'google',
|
||||||
label: 'query',
|
label: 'query',
|
||||||
|
@ -51,10 +46,9 @@ module.exports = {
|
||||||
|
|
||||||
pages = formatPaginationEmbeds(pages)
|
pages = formatPaginationEmbeds(pages)
|
||||||
const message = context.message
|
const message = context.message
|
||||||
const paging = await paginator.createReactionPaginator({
|
const paging = await paginator.createPaginator({
|
||||||
message,
|
message,
|
||||||
pages,
|
pages
|
||||||
reactions
|
|
||||||
});
|
});
|
||||||
}catch(e){
|
}catch(e){
|
||||||
console.log(e)
|
console.log(e)
|
||||||
|
|
|
@ -5,11 +5,6 @@ const { STATICS } = require('../../../labscore/utils/statics')
|
||||||
const { lyrics } = require('../../../labscore/api');
|
const { lyrics } = require('../../../labscore/api');
|
||||||
const { paginator } = require('../../../labscore/client');
|
const { paginator } = require('../../../labscore/client');
|
||||||
|
|
||||||
const reactions = {
|
|
||||||
previousPage: "⬅️",
|
|
||||||
nextPage: "➡️"
|
|
||||||
};
|
|
||||||
|
|
||||||
function createLyricsPage(context, search, fields){
|
function createLyricsPage(context, search, fields){
|
||||||
let em= createEmbed("default", context, {
|
let em= createEmbed("default", context, {
|
||||||
description: `**${search.body.song.title}**\n*Released ${search.body.song.release}*\n\n`,
|
description: `**${search.body.song.title}**\n*Released ${search.body.song.release}*\n\n`,
|
||||||
|
@ -70,10 +65,9 @@ module.exports = {
|
||||||
const message = context.message
|
const message = context.message
|
||||||
|
|
||||||
pages = formatPaginationEmbeds(pages)
|
pages = formatPaginationEmbeds(pages)
|
||||||
const paging = await paginator.createReactionPaginator({
|
const paging = await paginator.createPaginator({
|
||||||
message,
|
message,
|
||||||
pages,
|
pages
|
||||||
reactions
|
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,11 +23,6 @@ function createWolframPage(context, pod){
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
const reactions = {
|
|
||||||
previousPage: "⬅️",
|
|
||||||
nextPage: "➡️"
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'wolframalpha',
|
name: 'wolframalpha',
|
||||||
label: 'query',
|
label: 'query',
|
||||||
|
@ -52,10 +47,9 @@ module.exports = {
|
||||||
|
|
||||||
pages = formatPaginationEmbeds(pages)
|
pages = formatPaginationEmbeds(pages)
|
||||||
const message = context.message
|
const message = context.message
|
||||||
const paging = await paginator.createReactionPaginator({
|
const paging = await paginator.createPaginator({
|
||||||
message,
|
message,
|
||||||
pages,
|
pages
|
||||||
reactions
|
|
||||||
});
|
});
|
||||||
}catch(e){
|
}catch(e){
|
||||||
console.log(e)
|
console.log(e)
|
||||||
|
|
|
@ -21,7 +21,7 @@ const cluster = new ClusterClient("", {
|
||||||
|
|
||||||
// Create this clusters paginator
|
// Create this clusters paginator
|
||||||
const paginator = new Paginator(cluster, {
|
const paginator = new Paginator(cluster, {
|
||||||
maxTime: 120000,
|
maxTime: 300000,
|
||||||
pageLoop: true,
|
pageLoop: true,
|
||||||
pageNumber: true
|
pageNumber: true
|
||||||
});
|
});
|
||||||
|
|
|
@ -11,9 +11,7 @@ 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;
|
||||||
|
|
||||||
// Reference to reply function
|
// TODO: use editOrReply, kill old paginator if it exists
|
||||||
// Uses context.editOrReply if an instance of Context was passed
|
|
||||||
// Defaults to message.reply
|
|
||||||
this.editOrReply = (data.message.editOrReply || data.message.reply).bind(data.message);
|
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 assert = require("assert");
|
||||||
|
|
||||||
const { Constants, Utils } = require('detritus-client')
|
const { Constants, Utils } = require('detritus-client')
|
||||||
|
@ -6,7 +6,6 @@ const { Components, ComponentActionRow } = Utils
|
||||||
const { InteractionCallbackTypes } = Constants
|
const { InteractionCallbackTypes } = Constants
|
||||||
|
|
||||||
const allowedEvents = new Set([
|
const allowedEvents = new Set([
|
||||||
"MESSAGE_REACTION_ADD",
|
|
||||||
"MESSAGE_CREATE"
|
"MESSAGE_CREATE"
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -17,7 +16,9 @@ function deprecate(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>',
|
||||||
|
// PREVIOUS: '<:previous_page:977894273443844167>'
|
||||||
})
|
})
|
||||||
|
|
||||||
const { hasOwnProperty } = Object.prototype;
|
const { hasOwnProperty } = Object.prototype;
|
||||||
|
@ -54,13 +55,10 @@ module.exports = class Paginator {
|
||||||
if (!allowedEvents.has(event)) return;
|
if (!allowedEvents.has(event)) return;
|
||||||
|
|
||||||
for (const listener of this.activeListeners) {
|
for (const listener of this.activeListeners) {
|
||||||
if (!(listener instanceof ReactionPaginator)) continue;
|
if (!(listener instanceof InteractionPaginator)) continue;
|
||||||
if (!listener.commandMessage) continue;
|
if (!listener.commandMessage) continue;
|
||||||
|
|
||||||
if (listener.isCommandMessage(data.message_id) &&
|
if (event === "MESSAGE_CREATE" &&
|
||||||
listener.isTarget(data.user_id)) {
|
|
||||||
await this.handleReactionEvent(data, listener);
|
|
||||||
} else if (event === "MESSAGE_CREATE" &&
|
|
||||||
listener.isInChannel(data.channel_id) &&
|
listener.isInChannel(data.channel_id) &&
|
||||||
listener.isTarget(data.user_id) &&
|
listener.isTarget(data.user_id) &&
|
||||||
listener.waitingForPage) {
|
listener.waitingForPage) {
|
||||||
|
@ -74,7 +72,7 @@ module.exports = class Paginator {
|
||||||
// Get listener
|
// Get listener
|
||||||
let listener;
|
let listener;
|
||||||
for (const l of this.activeListeners) {
|
for (const l of this.activeListeners) {
|
||||||
if (!(l instanceof ReactionPaginator)) 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)) {
|
||||||
|
@ -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) {
|
async handleMessageEvent(data, listener) {
|
||||||
const page = parseInt(data.content, 10);
|
const page = parseInt(data.content, 10);
|
||||||
if (isNaN(page)) {
|
if (isNaN(page)) {
|
||||||
|
@ -180,7 +146,7 @@ module.exports = class Paginator {
|
||||||
return components;
|
return components;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createReactionPaginator(data) {
|
async createPaginator(data) {
|
||||||
if (this.pageNumber && Array.isArray(data.pages)) {
|
if (this.pageNumber && Array.isArray(data.pages)) {
|
||||||
for (let i = 0; i < data.pages.length; ++i) {
|
for (let i = 0; i < data.pages.length; ++i) {
|
||||||
const element = data.pages[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);
|
this.activeListeners.push(instance);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -199,7 +165,6 @@ module.exports = class Paginator {
|
||||||
await instance.init();
|
await instance.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
//await instance.addReactions();
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -29,9 +29,9 @@ module.exports = class PaginatorCluster {
|
||||||
return paginator;
|
return paginator;
|
||||||
}
|
}
|
||||||
|
|
||||||
createReactionPaginator(data) {
|
createPaginator(data) {
|
||||||
const targetPaginator = this.findOrSetPaginator(data.message.client);
|
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",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"detritus-client": "^0.17.0-beta.12",
|
"detritus-client": "^0.17.0-beta.12",
|
||||||
"detritus-pagination": "^1.4.0",
|
|
||||||
"dotenv": "^16.0.1",
|
"dotenv": "^16.0.1",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"superagent": "^7.1.1"
|
"superagent": "^7.1.1"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue