diff --git a/commands/analyze/repost.js b/commands/analyze/repost.js new file mode 100644 index 00000000..260424c6 --- /dev/null +++ b/commands/analyze/repost.js @@ -0,0 +1,66 @@ +const Command = require('../../structures/Command'); +const request = require('node-superfetch'); +const UserAgent = require('user-agents'); +const { stripIndents } = require('common-tags'); +const fileTypeRe = /\.(jpe?g|png|gif|jfif|bmp)(\?.+)?$/i; + +module.exports = class RepostCommand extends Command { + constructor(client) { + super(client, { + name: 'repost', + aliases: ['repost-sleuth'], + group: 'analyze', + memberName: 'repost', + description: 'Checks if an image is a repost.', + credit: [ + { + name: 'Reddit Repost Sleuth', + url: 'https://www.repostsleuth.com/', + reason: 'API' + } + ], + args: [ + { + key: 'image', + prompt: 'What image would you like to check?', + type: 'image-or-avatar' + } + ] + }); + } + + async run(msg, { image }) { + try { + const { body } = await request.get(image); + const results = await this.checkImage(body, image); + if (results === false) return msg.reply('This image is clean.'); + return msg.reply(stripIndents` + This image may be a repost. I've seen it **${body.matches.length}** times. + The closest match is at **${body.closest_match.hamming_match_percent}%** similarity. + ${body.closest_match.post.url} + `); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async checkImage(image, fileurl) { + const { body } = await request + .post('https://api.repostsleuth.com/image') + .query({ + filter: true, + same_sub: false, + filter_author: false, + only_older: false, + include_crossposts: false, + meme_filter: false, + target_match_percent: 90, + filter_dead_matches: false, + target_days_old: 0 + }) + .attach('image', image, `image.${fileurl.match(fileTypeRe)[1]}`) + .set({ 'user-agent': new UserAgent().toString() }); + if (!body.closest_match) return false; + return body; + } +}; diff --git a/package.json b/package.json index 9c62f0cf..faf971d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "138.2.0", + "version": "138.3.0", "description": "Your personal server companion.", "main": "Xiao.js", "private": true,