mrmBot-Matrix/commands/general/image.js

37 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-03-15 14:09:09 +00:00
import { readFileSync } from "fs";
const { searx } = JSON.parse(readFileSync(new URL("../../config/servers.json", import.meta.url)));
import { random } from "../../utils/misc.js";
import { request } from "undici";
import Command from "../../classes/command.js";
class ImageSearchCommand extends Command {
2023-03-19 05:10:48 +00:00
static category = "general"
2023-03-15 14:09:09 +00:00
async run() {
this.success = false;
const query = this.options.query ?? this.args.join(" ");
if (!query || !query.trim()) return "You need to provide something to search for!";
2023-03-17 00:23:01 +00:00
// await this.acknowledge();
2023-03-15 14:09:09 +00:00
const embeds = [];
const rawImages = await request(`${random(searx)}/search?format=json&safesearch=2&categories=images&q=!goi%20!ddi%20${encodeURIComponent(query)}`).then(res => res.body.json());
if (rawImages.results.length === 0) return "I couldn't find any results!";
const images = rawImages.results.filter((val) => !val.img_src.startsWith("data:"));
for (const [i, value] of images.entries()) {
2023-03-17 04:18:24 +00:00
return encodeURI(value.img_src)
this.success = true;
2023-03-15 14:09:09 +00:00
}
2023-03-17 04:18:24 +00:00
return;
2023-03-15 14:09:09 +00:00
}
static flags = [{
name: "query",
type: 3,
description: "The query you want to search for",
required: true
}];
static description = "Searches for images across the web";
static aliases = ["im", "photo", "img"];
static arguments = ["[query]"];
}
export default ImageSearchCommand;