mrmBot-Matrix/commands/general/image.js

44 lines
2.1 KiB
JavaScript
Raw Normal View History

const paginator = require("../../utils/pagination/pagination.js");
const { searx } = require("../../servers.json");
const { random } = require("../../utils/misc.js");
const fetch = require("node-fetch");
const Command = require("../../classes/command.js");
class ImageSearchCommand extends Command {
async run() {
if (this.message.channel.guild && !this.message.channel.permissionsOf(this.client.user.id).has("addReactions")) return "I don't have the `Add Reactions` permission!";
if (this.message.channel.guild && !this.message.channel.permissionsOf(this.client.user.id).has("embedLinks")) return "I don't have the `Embed Links` permission!";
if (this.args.length === 0) return "You need to provide something to search for!";
2021-05-24 19:31:44 +00:00
await this.message.channel.sendTyping();
const embeds = [];
const images = await fetch(`${random(searx)}/search?format=json&safesearch=1&categories=images&disabled_engines=flickr__images,ccengine__images,library of congress__images,deviantart__images,bing images__images&q=${encodeURIComponent(this.args.join(" "))}`).then(res => res.json());
//if (images.error && images.error.code === 403) return "The daily search quota has been exceeded. Check back later.";
if (images.results.length === 0) return "I couldn't find any results!";
for (const [i, value] of images.results.entries()) {
embeds.push({
"embed": {
"title": "Search Results",
"color": 16711680,
"footer": {
"text": `Page ${i + 1} of ${images.results.length}`
},
"description": `[${value.title}](${encodeURI(value.img_src)})`,
"image": {
"url": encodeURI(value.img_src)
},
"author": {
"name": this.message.author.username,
"icon_url": this.message.author.avatarURL
}
}
});
}
return paginator(this.client, this.message, embeds);
}
static description = "Searches for images across the web";
static aliases = ["im", "photo", "img"];
static arguments = ["[query]"];
}
module.exports = ImageSearchCommand;