Make pagination use interactions instead of reactions, filter out base64 images in image

This commit is contained in:
Essem 2021-08-12 18:45:17 -05:00
parent 5c0cb6463a
commit da709c485f
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
5 changed files with 119 additions and 40 deletions

View file

@ -11,15 +11,16 @@ class ImageSearchCommand extends Command {
if (this.args.length === 0) return "You need to provide something to search for!"; if (this.args.length === 0) return "You need to provide something to search for!";
await this.message.channel.sendTyping(); await this.message.channel.sendTyping();
const embeds = []; const embeds = [];
const images = await fetch(`${random(searx)}/search?format=json&safesearch=2&categories=images&q=!goi%20!bii%20!ddi%20${encodeURIComponent(this.args.join(" "))}`).then(res => res.json()); const rawImages = await fetch(`${random(searx)}/search?format=json&safesearch=2&categories=images&q=!goi%20!bii%20!ddi%20${encodeURIComponent(this.args.join(" "))}`).then(res => res.json());
if (images.results.length === 0) return "I couldn't find any results!"; if (rawImages.results.length === 0) return "I couldn't find any results!";
for (const [i, value] of images.results.entries()) { const images = rawImages.results.filter((val) => !val.img_src.startsWith("data:image"));
for (const [i, value] of images.entries()) {
embeds.push({ embeds.push({
"embed": { "embed": {
"title": "Search Results", "title": "Search Results",
"color": 16711680, "color": 16711680,
"footer": { "footer": {
"text": `Page ${i + 1} of ${images.results.length}` "text": `Page ${i + 1} of ${images.length}`
}, },
"description": value.title, "description": value.title,
"image": { "image": {

View file

@ -8,7 +8,7 @@ const { clean } = require("../utils/misc.js");
// run when someone sends a message // run when someone sends a message
module.exports = async (client, cluster, worker, ipc, message) => { module.exports = async (client, cluster, worker, ipc, message) => {
// ignore dms and other bots // ignore other bots
if (message.author.bot) return; if (message.author.bot) return;
// don't run command if bot can't send messages // don't run command if bot can't send messages
@ -47,7 +47,7 @@ module.exports = async (client, cluster, worker, ipc, message) => {
} }
// ignore other stuff // ignore other stuff
if (message.content.startsWith(prefix) === false) return; if (!message.content.startsWith(prefix)) return;
// separate commands and args // separate commands and args
const replace = isMention ? `@${client.user.username} ` : prefix; const replace = isMention ? `@${client.user.username} ` : prefix;

View file

@ -0,0 +1,34 @@
// eris doesn't come with a method to wait for interactions by default, so we make our own
const EventEmitter = require("events").EventEmitter;
class InteractionCollector extends EventEmitter {
constructor(client, message, options = {}) {
super();
this.message = message;
this.options = options;
this.ended = false;
this.bot = client;
this.listener = async (packet) => {
if (packet.t !== "INTERACTION_CREATE") return;
await this.verify(packet.d.message, packet.d.data.custom_id, packet.d.id, packet.d.token, packet.d.member);
};
this.bot.on("rawWS", this.listener);
if (options.time) setTimeout(() => this.stop("time"), options.time);
}
async verify(message, interaction, id, token, member) {
if (this.message.id !== message.id) return false;
const msg = await this.bot.getMessage(message.channel_id, message.id);
this.emit("interaction", msg, interaction, id, token, member);
return true;
}
stop(reason) {
if (this.ended) return;
this.ended = true;
this.bot.removeListener("rawWS", this.listener);
this.emit("end", this.collected, reason);
}
}
module.exports = InteractionCollector;

View file

@ -1,9 +1,10 @@
const ReactionCollector = require("./awaitreactions.js");
const MessageCollector = require("./awaitmessages.js"); const MessageCollector = require("./awaitmessages.js");
const InteractionCollector = require("./awaitinteractions.js");
const fetch = require("node-fetch");
module.exports = async (client, message, pages, timeout = 120000) => { module.exports = async (client, message, pages, timeout = 120000) => {
const manageMessages = message.channel.guild && message.channel.permissionsOf(client.user.id).has("manageMessages") ? true : false; const manageMessages = message.channel.guild && message.channel.permissionsOf(client.user.id).has("manageMessages") ? true : false;
/*const options = { const options = {
messageReference: { messageReference: {
channelID: message.channel.id, channelID: message.channel.id,
messageID: message.id, messageID: message.id,
@ -13,25 +14,80 @@ module.exports = async (client, message, pages, timeout = 120000) => {
allowedMentions: { allowedMentions: {
repliedUser: false repliedUser: false
} }
};*/ };
let page = 0; let page = 0;
//let currentPage = await client.createMessage(message.channel.id, Object.assign(pages[page], options)); const components = {
let currentPage = await client.createMessage(message.channel.id, pages[page]); components: [{
if (pages.length > 1) { type: 1,
const emojiList = ["◀", "🔢", "▶", "🗑"]; components: [
for (const emoji of emojiList) { {
await currentPage.addReaction(emoji); type: 2,
label: "Back",
emoji: {
id: null,
name: "◀"
},
style: 1,
custom_id: "back"
},
{
type: 2,
label: "Forward",
emoji: {
id: null,
name: "▶"
},
style: 1,
custom_id: "forward"
},
{
type: 2,
label: "Jump",
emoji: {
id: null,
name: "🔢"
},
style: 1,
custom_id: "jump"
},
{
type: 2,
label: "Delete",
emoji: {
id: null,
name: "🗑"
},
style: 4,
custom_id: "delete"
}
]
}]
};
const ackOptions = {
method: "POST",
body: "{\"type\":6}",
headers: {
"Content-Type": "application/json"
} }
const reactionCollector = new ReactionCollector(client, currentPage, (message, reaction, member) => emojiList.includes(reaction.name) && !member.bot, { time: timeout }); };
reactionCollector.on("reaction", async (msg, reaction, member) => { let currentPage = await client.createMessage(message.channel.id, Object.assign(pages[page], options, components));
if (member.id === message.author.id) { if (pages.length > 1) {
switch (reaction.name) { const interactionCollector = new InteractionCollector(client, currentPage, { time: timeout });
case "◀": interactionCollector.on("interaction", async (msg, interaction, id, token, member) => {
if (member.user.id === message.author.id) {
switch (interaction) {
case "back":
await fetch(`https://discord.com/api/v8/interactions/${id}/${token}/callback`, ackOptions);
page = page > 0 ? --page : pages.length - 1; page = page > 0 ? --page : pages.length - 1;
currentPage = await currentPage.edit(pages[page]); currentPage = await currentPage.edit(pages[page]);
if (manageMessages) msg.removeReaction("◀", member.id);
break; break;
case "🔢": case "forward":
await fetch(`https://discord.com/api/v8/interactions/${id}/${token}/callback`, ackOptions);
page = page + 1 < pages.length ? ++page : 0;
currentPage = await currentPage.edit(pages[page]);
break;
case "jump":
await fetch(`https://discord.com/api/v8/interactions/${id}/${token}/callback`, ackOptions);
client.createMessage(message.channel.id, Object.assign({ content: "What page do you want to jump to?" }, { client.createMessage(message.channel.id, Object.assign({ content: "What page do you want to jump to?" }, {
messageReference: { messageReference: {
channelID: currentPage.channel.id, channelID: currentPage.channel.id,
@ -52,19 +108,14 @@ module.exports = async (client, message, pages, timeout = 120000) => {
if (manageMessages) await response.delete(); if (manageMessages) await response.delete();
page = Number(response.content) - 1; page = Number(response.content) - 1;
currentPage = await currentPage.edit(pages[page]); currentPage = await currentPage.edit(pages[page]);
if (manageMessages) msg.removeReaction("🔢", member.id);
}); });
}).catch(error => { }).catch(error => {
throw error; throw error;
}); });
break; break;
case "▶": case "delete":
page = page + 1 < pages.length ? ++page : 0; await fetch(`https://discord.com/api/v8/interactions/${id}/${token}/callback`, ackOptions);
currentPage = await currentPage.edit(pages[page]); interactionCollector.emit("end");
if (manageMessages) msg.removeReaction("▶", member.id);
break;
case "🗑":
reactionCollector.emit("end");
if (await client.getMessage(currentPage.channel.id, currentPage.id).catch(() => undefined)) await currentPage.delete(); if (await client.getMessage(currentPage.channel.id, currentPage.id).catch(() => undefined)) await currentPage.delete();
return; return;
default: default:
@ -72,15 +123,8 @@ module.exports = async (client, message, pages, timeout = 120000) => {
} }
} }
}); });
reactionCollector.once("end", async () => { interactionCollector.once("end", () => {
try { interactionCollector.removeAllListeners("interaction");
await client.getMessage(currentPage.channel.id, currentPage.id);
if (manageMessages) {
await currentPage.removeReactions();
}
} catch {
return;
}
}); });
} }
return currentPage; return currentPage;

View file

@ -49,7 +49,7 @@ esmbot_connected_workers ${servers.length}
} }
shutdown(done) { shutdown(done) {
this.httpServer.close(); if (this.httpServer) this.httpServer.close();
done(); done();
} }
} }