const { InteractionContextType, ApplicationIntegrationType, SlashCommandBuilder, EmbedBuilder, escapeMarkdown, bold, ButtonBuilder, ButtonStyle } = require("discord.js"); const { generateImageUrl } = require('@imgproxy/imgproxy-node'); const { stringify } = require("node:querystring"); const { Pagination } = require('pagination.djs'); const data = new SlashCommandBuilder() .setName("isrch") .setDescription("SearxNG-powered image search") .addStringOption(builder => builder // .setName("query") .setRequired(true) .setDescription("What to search for") ) .setContexts([ InteractionContextType.Guild, InteractionContextType.BotDM, InteractionContextType.PrivateChannel ]) .setIntegrationTypes([ ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall ]); function notEmpty(str) { return str.trim() !== '' } function proxy(url) { if (!process.env.IMGPROXY_HOST) return url; url = generateImageUrl({ endpoint: process.env.IMGPROXY_HOST, url: url, salt: process.env.IMGPROXY_SALT, key: process.env.IMGPROXY_KEY }); return url; } module.exports = { data, async execute(interaction) { const query = interaction.options.getString("query"); await interaction.deferReply(); var queryString = stringify({ "category_images": "", "format": "json", "q": "!goi " + query }); const embeds = []; const data = await (await fetch(`https://s.koda.re/search?${queryString}`)).json(); for (const result of data.results) { if (result.img_src == '') continue; var description = result.source ?? result.title; if (description.length > 1024) description = description.slice(0, 1021) + "..."; if (result.img_src.startsWith("//")) result.img_src = "http:" + result.img_src const embed = new EmbedBuilder() .setColor("#cba6f7") .setImage(proxy(result.img_src)) .setFooter({ text: result.engines.join(", ") }); if (description.length >= 1) embed.setDescription(description); embeds.push(embed); } const pagination = new Pagination(interaction); pagination.setEmbeds(embeds, (embed, index, array) => { const footerText = [ `${index + 1}/${array.length}`, embed.data.footer.text ].filter(notEmpty).join(' ยท ') return embed.setFooter({ text: footerText }); }); pagination.render(); } }