mrmBot-Matrix/commands/image.js

37 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-09-13 20:02:41 +00:00
const client = require("../utils/client.js");
const paginator = require("../utils/pagination/pagination.js");
const { image_search } = require("duckduckgo-images-api");
2019-09-13 20:02:41 +00:00
exports.run = async (message, args) => {
if (message.channel.guild && !message.channel.guild.members.get(client.user.id).permission.has("addReactions") && !message.channel.permissionsOf(client.user.id).has("addReactions")) return `${message.author.mention}, I don't have the \`Add Reactions\` permission!`;
if (message.channel.guild && !message.channel.guild.members.get(client.user.id).permission.has("embedLinks") && !message.channel.permissionsOf(client.user.id).has("embedLinks")) return `${message.author.mention}, I don't have the \`Embed Links\` permission!`;
2019-09-13 20:02:41 +00:00
if (args.length === 0) return `${message.author.mention}, you need to provide something to search for!`;
const embeds = [];
const images = await image_search({ query: args.join(" "), moderate: true });
if (images.error && images.error.code === 403) return `${message.author.mention}, the daily search quota has been exceeded. Check back later.`;
if (images.length === 0) return `${message.author.mention}, I couldn't find any results!`;
for (const [i, value] of images.entries()) {
2019-09-13 20:02:41 +00:00
embeds.push({
"embed": {
"title": "Search Results",
"color": 16711680,
"footer": {
"text": `Page ${i + 1} of ${images.length}`
2019-09-13 20:02:41 +00:00
},
"image": {
"url": value.image
2019-09-13 20:02:41 +00:00
},
"author": {
"name": message.author.username,
"icon_url": message.author.avatarURL
}
}
});
}
return paginator(message, embeds);
};
exports.aliases = ["im", "photo", "img"];
exports.category = 1;
exports.help = "Searches for images on DuckDuckGo";
exports.params = "[query]";