Make pagination use interactions instead of reactions, filter out base64 images in image
This commit is contained in:
parent
5c0cb6463a
commit
da709c485f
5 changed files with 119 additions and 40 deletions
|
@ -11,15 +11,16 @@ class ImageSearchCommand extends Command {
|
|||
if (this.args.length === 0) return "You need to provide something to search for!";
|
||||
await this.message.channel.sendTyping();
|
||||
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());
|
||||
if (images.results.length === 0) return "I couldn't find any results!";
|
||||
for (const [i, value] of images.results.entries()) {
|
||||
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 (rawImages.results.length === 0) return "I couldn't find any results!";
|
||||
const images = rawImages.results.filter((val) => !val.img_src.startsWith("data:image"));
|
||||
for (const [i, value] of images.entries()) {
|
||||
embeds.push({
|
||||
"embed": {
|
||||
"title": "Search Results",
|
||||
"color": 16711680,
|
||||
"footer": {
|
||||
"text": `Page ${i + 1} of ${images.results.length}`
|
||||
"text": `Page ${i + 1} of ${images.length}`
|
||||
},
|
||||
"description": value.title,
|
||||
"image": {
|
||||
|
|
|
@ -8,7 +8,7 @@ const { clean } = require("../utils/misc.js");
|
|||
|
||||
// run when someone sends a message
|
||||
module.exports = async (client, cluster, worker, ipc, message) => {
|
||||
// ignore dms and other bots
|
||||
// ignore other bots
|
||||
if (message.author.bot) return;
|
||||
|
||||
// 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
|
||||
if (message.content.startsWith(prefix) === false) return;
|
||||
if (!message.content.startsWith(prefix)) return;
|
||||
|
||||
// separate commands and args
|
||||
const replace = isMention ? `@${client.user.username} ` : prefix;
|
||||
|
|
34
utils/pagination/awaitinteractions.js
Normal file
34
utils/pagination/awaitinteractions.js
Normal 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;
|
|
@ -1,9 +1,10 @@
|
|||
const ReactionCollector = require("./awaitreactions.js");
|
||||
const MessageCollector = require("./awaitmessages.js");
|
||||
const InteractionCollector = require("./awaitinteractions.js");
|
||||
const fetch = require("node-fetch");
|
||||
|
||||
module.exports = async (client, message, pages, timeout = 120000) => {
|
||||
const manageMessages = message.channel.guild && message.channel.permissionsOf(client.user.id).has("manageMessages") ? true : false;
|
||||
/*const options = {
|
||||
const options = {
|
||||
messageReference: {
|
||||
channelID: message.channel.id,
|
||||
messageID: message.id,
|
||||
|
@ -13,25 +14,80 @@ module.exports = async (client, message, pages, timeout = 120000) => {
|
|||
allowedMentions: {
|
||||
repliedUser: false
|
||||
}
|
||||
};*/
|
||||
};
|
||||
let page = 0;
|
||||
//let currentPage = await client.createMessage(message.channel.id, Object.assign(pages[page], options));
|
||||
let currentPage = await client.createMessage(message.channel.id, pages[page]);
|
||||
if (pages.length > 1) {
|
||||
const emojiList = ["◀", "🔢", "▶", "🗑"];
|
||||
for (const emoji of emojiList) {
|
||||
await currentPage.addReaction(emoji);
|
||||
const components = {
|
||||
components: [{
|
||||
type: 1,
|
||||
components: [
|
||||
{
|
||||
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) => {
|
||||
if (member.id === message.author.id) {
|
||||
switch (reaction.name) {
|
||||
case "◀":
|
||||
};
|
||||
let currentPage = await client.createMessage(message.channel.id, Object.assign(pages[page], options, components));
|
||||
if (pages.length > 1) {
|
||||
const interactionCollector = new InteractionCollector(client, currentPage, { time: timeout });
|
||||
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;
|
||||
currentPage = await currentPage.edit(pages[page]);
|
||||
if (manageMessages) msg.removeReaction("◀", member.id);
|
||||
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?" }, {
|
||||
messageReference: {
|
||||
channelID: currentPage.channel.id,
|
||||
|
@ -52,19 +108,14 @@ module.exports = async (client, message, pages, timeout = 120000) => {
|
|||
if (manageMessages) await response.delete();
|
||||
page = Number(response.content) - 1;
|
||||
currentPage = await currentPage.edit(pages[page]);
|
||||
if (manageMessages) msg.removeReaction("🔢", member.id);
|
||||
});
|
||||
}).catch(error => {
|
||||
throw error;
|
||||
});
|
||||
break;
|
||||
case "▶":
|
||||
page = page + 1 < pages.length ? ++page : 0;
|
||||
currentPage = await currentPage.edit(pages[page]);
|
||||
if (manageMessages) msg.removeReaction("▶", member.id);
|
||||
break;
|
||||
case "🗑":
|
||||
reactionCollector.emit("end");
|
||||
case "delete":
|
||||
await fetch(`https://discord.com/api/v8/interactions/${id}/${token}/callback`, ackOptions);
|
||||
interactionCollector.emit("end");
|
||||
if (await client.getMessage(currentPage.channel.id, currentPage.id).catch(() => undefined)) await currentPage.delete();
|
||||
return;
|
||||
default:
|
||||
|
@ -72,15 +123,8 @@ module.exports = async (client, message, pages, timeout = 120000) => {
|
|||
}
|
||||
}
|
||||
});
|
||||
reactionCollector.once("end", async () => {
|
||||
try {
|
||||
await client.getMessage(currentPage.channel.id, currentPage.id);
|
||||
if (manageMessages) {
|
||||
await currentPage.removeReactions();
|
||||
}
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
interactionCollector.once("end", () => {
|
||||
interactionCollector.removeAllListeners("interaction");
|
||||
});
|
||||
}
|
||||
return currentPage;
|
||||
|
|
|
@ -49,7 +49,7 @@ esmbot_connected_workers ${servers.length}
|
|||
}
|
||||
|
||||
shutdown(done) {
|
||||
this.httpServer.close();
|
||||
if (this.httpServer) this.httpServer.close();
|
||||
done();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue