diff --git a/commands/message/dev/eval.js b/commands/message/dev/eval.js index af3418f..0190828 100644 --- a/commands/message/dev/eval.js +++ b/commands/message/dev/eval.js @@ -1,3 +1,5 @@ +const { Utils, Constants } = require("detritus-client"); + const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor; // TODO: remake this eventually, copy pasted it from v1 cause lazy diff --git a/commands/message/search/reddit.js b/commands/message/search/reddit.js new file mode 100644 index 0000000..d72a4af --- /dev/null +++ b/commands/message/search/reddit.js @@ -0,0 +1,92 @@ +const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed') +const { link, icon, highlight } = require('../../../labscore/utils/markdown') +const { editOrReply } = require('../../../labscore/utils/message') +const { STATICS } = require('../../../labscore/utils/statics') + +const { paginator } = require('../../../labscore/client'); +const { reddit } = require('../../../labscore/api'); +const { ICONS } = require('../../../labscore/constants'); + +const awards = [ + "gold", + "silver", + "wholesome" +] + +function createRedditPage(context, result){ + let res = { + "embeds": [ + createEmbed("default", context, { + author: { + iconUrl: result.subreddit.icon, + name: result.subreddit.name, + url: result.subreddit.link + }, + description: `**${link(result.post.link, result.post.title)}**`, + footer: { + iconUrl: STATICS.reddit, + text: `Reddit • ${context.application.name}` + } + }) + ] + } + + if(result.post.image) res.embeds[0].image = { url: result.post.image }; + + let awardData = [] + // Awards + for(const a of Object.keys(result.awards)){ + awardData.push(`${icon(`reddit_${a}`)}${highlight(result.awards[a])}`) + } + + if(awardData.length >= 1) res.embeds[0].fields = [ + { + name: 'Awards', + value: awardData.join(' ') + } + ] + + return res; +} + +module.exports = { + name: 'reddit', + label: 'query', + aliases: ['r'], + metadata: { + description: 'reddit search', + examples: ['reddit r/otters'], + category: 'search', + usage: 'reddit [r/] [-type image]' + }, + args: [ + { default: "all", name: "type", type: "image" } + ], + run: async (context, args) => { + context.triggerTyping(); + if(!args.query) return editOrReply(context, {embeds:[createEmbed("warning", context, `Missing Parameter (query).`)]}) + try{ + let search = await reddit(context, args.query, context.channel.nsfw) + search = search.response + + if(search.body.status == 2) return editOrReply(context, {embeds:[createEmbed("error", context, search.body.message)]}) + + let pages = [] + for(const res of search.body.results){ + if(args.type == "image" && !res.post.image) continue; + pages.push(createRedditPage(context, res)) + } + + if(pages.length == 0) return editOrReply(context, {embeds:[createEmbed("warning", context, `No results found.`)]}) + + pages = formatPaginationEmbeds(pages) + const paging = await paginator.createPaginator({ + context, + pages + }); + }catch(e){ + console.log(e) + return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform reddit search.`)]}) + } + }, +}; \ No newline at end of file diff --git a/labscore/api/endpoints.js b/labscore/api/endpoints.js index 8d16e3d..4e4f5c5 100644 --- a/labscore/api/endpoints.js +++ b/labscore/api/endpoints.js @@ -32,6 +32,7 @@ const Api = Object.freeze({ SEARCH_GOOGLE: '/search/google', SEARCH_GOOGLE_IMAGES: '/search/google-images', SEARCH_LYRICS: '/search/lyrics', + SEARCH_REDDIT: '/search/reddit', SEARCH_RULE34: '/search/booru', SEARCH_TINEYE: '/search/tineye', SEARCH_URBANDICTIONARY: '/search/urbandictionary', diff --git a/labscore/api/index.js b/labscore/api/index.js index 8e8e35b..37a313f 100644 --- a/labscore/api/index.js +++ b/labscore/api/index.js @@ -116,6 +116,13 @@ module.exports.googleImages = async function(context, query){ }) } +module.exports.reddit = async function(context, query, nsfw = false){ + return await request(Api.SEARCH_REDDIT, "GET", {}, { + q: query, + nsfw: nsfw + }) +} + module.exports.rule34 = async function(context, query, site){ return await request(Api.SEARCH_RULE34, "GET", {}, { q: query, diff --git a/labscore/constants.js b/labscore/constants.js index 2b0ec0e..cf51916 100644 --- a/labscore/constants.js +++ b/labscore/constants.js @@ -65,7 +65,11 @@ module.exports.ICONS = Object.freeze({ "locale": "<:ico_locale:925891616986791936>", "question": "<:ico_question:949420315677691934>", "upvote": "<:ico_upvote:980238682353205259>", - "downvote": "<:ico_downvote:980238681971494963>" + "downvote": "<:ico_downvote:980238681971494963>", + "reddit_gold": "<:rda_gold:993616956844425266>", + "reddit_silver": "<:rda_silver:993616958777983067>", + "reddit_wholesome": "<:rda_wholesome:993616961231663165>", + "reddit_helpful": "<:rda_helpful:993618441669984437>" }) module.exports.GUILD_FEATURES = Object.freeze({ diff --git a/labscore/utils/statics.js b/labscore/utils/statics.js index 3d6bef9..36c575f 100644 --- a/labscore/utils/statics.js +++ b/labscore/utils/statics.js @@ -28,6 +28,10 @@ const Statics = Object.freeze({ file: "brands/photofunia.png", revision: 1 }, + reddit: { + file: "brands/reddit.png", + revision: 0 + }, tineye: { file: "brands/tineye.png", revision: 1 @@ -55,7 +59,7 @@ const Statics = Object.freeze({ revision: 0 }, error: { - file: "icons/ico_warning_small.png", + file: "icons/ico_error_small.png", revision: 0 }, loading: { @@ -80,6 +84,7 @@ module.exports.STATICS = Object.freeze({ inferkit: staticAsset(Statics.brands.inferkit), makesweet: staticAsset(Statics.brands.makesweet), photofunia: staticAsset(Statics.brands.photofunia), + reddit: staticAsset(Statics.brands.reddit), tineye: staticAsset(Statics.brands.tineye), urbandictionary: staticAsset(Statics.brands.urbandictionary), wikihow: staticAsset(Statics.brands.wikihow),