diff --git a/commands/interaction/slash/search/urbandictionary.js b/commands/interaction/slash/search/urbandictionary.js new file mode 100644 index 0000000..2932e7c --- /dev/null +++ b/commands/interaction/slash/search/urbandictionary.js @@ -0,0 +1,89 @@ +const { urbandictionary } = require('#api'); +const { paginator } = require('#client'); + +const { createEmbed, formatPaginationEmbeds, page } = require('#utils/embed'); +const { acknowledge } = require('#utils/interactions'); +const { link, iconPill } = require('#utils/markdown') +const { editOrReply } = require('#utils/message') +const { STATICS } = require('#utils/statics') + +// TODO: Turn this into a general purpose permissions constant +const { Permissions, ApplicationCommandOptionTypes } = require("detritus-client/lib/constants"); + +function createUrbanPage(context, result){ + let e = createEmbed("default", context, { + description: `**${link(result.link, result.title)}**`, + fields: [], + footer: { + iconUrl: STATICS.urbandictionary, + text: `UrbanDictionary • ${context.application.name}` + } + }) + if(result.description) e.fields.push({ + name: "Description", + value: result.description.substr(0, 1023), + inline: true + }) + e.fields.push({ + name: "Stats", + value: `${iconPill("upvote", result.score.likes)} ​ ${iconPill("downvote", result.score.dislikes)}\n**Author:** ${link(`https://www.urbandictionary.com/author.php?author=${encodeURIComponent(result.author)}`, result.author)}`, + inline: true + }) + if(result.example) e.fields.push({ + name: "Example", + value: result.example.substr(0, 1023), + inline: false + }) + return page(e); +} + +module.exports = { + name: 'urbandictionary', + description: 'Define a word on urban dictionary.', + contexts: [ + 0, + 1, + 2 + ], + integrationTypes: [ + 1 + ], + options: [ + { + name: 'term', + description: 'Term to look up.', + type: ApplicationCommandOptionTypes.TEXT, + required: true + }, + { + name: 'incognito', + description: 'Makes the response only visible to you.', + type: ApplicationCommandOptionTypes.BOOLEAN, + required: false, + default: false + } + ], + run: async (context, args) => { + await acknowledge(context, args.incognito); + + try{ + let search = await urbandictionary(context, args.term) + search = search.response + + if(search.body.status == 1) return editOrReply(context, createEmbed("warning", context, search.body.message)) + + let pages = [] + for(const res of search.body.results){ + pages.push(createUrbanPage(context, res)) + } + + await paginator.createPaginator({ + context, + pages: formatPaginationEmbeds(pages) + }); + }catch(e){ + console.log(e) + return editOrReply(context, createEmbed("error", context, `Unable to perform urban dictionary search.`)) + } + }, +}; \ No newline at end of file