pissbot-9000/commands/message/search/reddit.js
2023-06-14 23:46:32 +02:00

87 lines
No EOL
3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed')
const { link, icon, highlight, iconPill } = 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');
function createRedditPage(context, result){
let res = {
"embeds": [
createEmbed("default", context, {
author: {
iconUrl: result.subreddit.icon,
name: result.subreddit.name,
url: result.subreddit.link
},
footer: {
iconUrl: STATICS.reddit,
text: `Reddit • ${context.application.name}`
}
})
]
}
if(result.post.image) res.embeds[0].image = { url: result.post.image };
let description = [`**${link(result.post.link, result.post.title)}**`]
let awardData = []
// Awards
for(const a of Object.keys(result.awards)){
awardData.push(`${icon(`reddit_${a}`)}${highlight(result.awards[a])}`)
}
if(awardData.length >= 1) description.push(`${awardData.join(' ')}`)
description.push(``)
description.push(`${iconPill("upvote", result.post.score)} ${icon("person")} ${link(result.author.link, `u/${result.author.name}`)}`)
res.embeds[0].description = description.join('\n')
return res;
}
module.exports = {
name: 'reddit',
label: 'query',
aliases: ['r'],
metadata: {
description: 'Returns search results from reddit. Allows global and subreddit-specific search.',
description_short: 'Search on Reddit',
examples: ['reddit r/otters'],
category: 'search',
usage: 'reddit [r/<subreddit>] <query> [-type image]'
},
args: [
{ default: "all", name: "type", type: "image", help: "Types of post the search query should return" }
],
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)
if(e.response && e.response.body.message) return editOrReply(context, {embeds:[createEmbed("error", context, e.response.body.message)]})
return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform reddit search.`)]})
}
},
};