diff --git a/commands/message/search/urbandictionary.js b/commands/message/search/urbandictionary.js index a22a29b..49a11c1 100644 --- a/commands/message/search/urbandictionary.js +++ b/commands/message/search/urbandictionary.js @@ -62,7 +62,7 @@ module.exports = { }); }catch(e){ console.log(e) - return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform google search.`)]}) + return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform urban dictionary search.`)]}) } }, }; \ No newline at end of file diff --git a/commands/message/utils/define.js b/commands/message/utils/define.js new file mode 100644 index 0000000..7dfb1b5 --- /dev/null +++ b/commands/message/utils/define.js @@ -0,0 +1,66 @@ +const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed') +const { link } = require('../../../labscore/utils/markdown') +const { editOrReply } = require('../../../labscore/utils/message') + +const { paginator } = require('../../../labscore/client'); +const { dictionary } = require('../../../labscore/api'); + +function createDictionaryPage(context, result){ + let phon = '' + if(result.phonetic) phon = `\n*${result.phonetic}*` + + let e = createEmbed("default", context, { + description: `**${link(result.source, result.word)}**${phon}`, + fields: [], + thumbnail: { + url: `https://cdn.discordapp.com/emojis/925891616986791936.png?size=4096` + } + }) + + for(const d of result.definitions){ + let v = d.definition + if(d.example) v = v + `\n\n**Example**\n${d.example}` + if(d.synonyms.length >= 1) v = v + `\n\n**Synonyms**\n*${d.synonyms.join(', ')}*` + e.fields.push({ + name: d.type, + value: v, + inline: true + }) + } + + let res = {"embeds": [e]} + return res; +} + +module.exports = { + name: 'define', + label: 'query', + aliases: ['dictionary'], + metadata: { + description: 'dictionary lookup', + examples: ['dictionary Flask'], + category: 'search', + usage: 'define ' + }, + run: async (context, args) => { + context.triggerTyping(); + try{ + let search = await dictionary(context, args.query) + search = search.response + + let pages = [] + for(const res of search.body.results){ + pages.push(createDictionaryPage(context, res)) + } + + 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 dictionary lookup.`)]}) + } + }, +}; \ No newline at end of file diff --git a/labscore/api/endpoints.js b/labscore/api/endpoints.js index 5159ebc..8d16e3d 100644 --- a/labscore/api/endpoints.js +++ b/labscore/api/endpoints.js @@ -28,6 +28,7 @@ const Api = Object.freeze({ SEARCH_AUDIO: '/search/audio', SEARCH_BING: '/search/bing', SEARCH_BING_IMAGES: '/search/bing-images', + SEARCH_DICTIONARY: '/search/dictionary', SEARCH_GOOGLE: '/search/google', SEARCH_GOOGLE_IMAGES: '/search/google-images', SEARCH_LYRICS: '/search/lyrics', diff --git a/labscore/api/index.js b/labscore/api/index.js index 8206a45..8e8e35b 100644 --- a/labscore/api/index.js +++ b/labscore/api/index.js @@ -135,6 +135,12 @@ module.exports.bingImages = async function(context, query){ }) } +module.exports.dictionary = async function(context, query){ + return await request(Api.SEARCH_DICTIONARY, "GET", {}, { + q: query + }) +} + module.exports.tineye = async function(context, url){ return await request(Api.SEARCH_TINEYE, "GET", {}, { url: url