const { dictionary } = require('#api'); const {createDynamicCardStack} = require("#cardstack/index"); const {PERMISSION_GROUPS} = require("#constants"); const { createEmbed, page } = require('#utils/embed'); const { acknowledge } = require('#utils/interactions'); const { link, smallPill, icon, pill } = require('#utils/markdown') const { editOrReply } = require('#utils/message') const { ApplicationCommandOptionTypes, InteractionContextTypes, ApplicationIntegrationTypes } = require('detritus-client/lib/constants'); // TODO(unity): single constant const LABELS = { "offensive": `Offensive`, "informal": `Informal` } // TODO(unity) function renderDictionaryEntry(context, result, definition, language) { let card = createEmbed("default", context, { description: `### ${result.word}\n-# ${result.phonetic} • ${definition.type}\n\n`, url: `https://en.wiktionary.org/wiki/${encodeURIComponent(result.word)}`, fields: [] }) if(definition.labels?.filter((l)=>LABELS[l]).length >= 1) card.description += `-# ⚠ ${definition.labels.map((l)=>LABELS[l]).join(" ⚠ ")}\n`; let defs = []; let i = 1; for(const d of definition.definitions){ let def = `${i++}. ${d.definition}`; if(d.labels?.filter((l)=>LABELS[l]).length >= 1) def = `-# ⚠ ${d.labels.map((l)=>LABELS[l]).join(" ⚠ ")}\n` + def; if(d.example) def += `\n - *${d.example}*` let nyms = []; if(d.synonyms) nyms = nyms.concat(d.synonyms.map((sd)=>smallPill(sd))) // Display up to 6 random synonyms/antonyms if(nyms.length >= 1) { nyms = nyms.splice(0 ,6) .map(value => ({ value, sort: Math.random() })) .sort((a, b) => a.sort - b.sort) .map(({ value }) => value) def += `\n-# ${nyms.join(" ")}` } defs.push(def); } if(defs.length > 5){ defs = defs.splice(0, 5); defs.push(link(`https://www.google.com/search?q=define+${encodeURIComponent(result.word)}`, `More Definitions ${icon("open_in_new")}`)) } card.description += defs.join("\n\n") return page(card); } module.exports = { name: 'dictionary', description: 'Define a word from the dictionary.', contexts: [ InteractionContextTypes.GUILD, InteractionContextTypes.PRIVATE_CHANNEL, InteractionContextTypes.BOT_DM ], integrationTypes: [ ApplicationIntegrationTypes.USER_INSTALL ], 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, [...PERMISSION_GROUPS.baseline_slash]); try{ let search = await dictionary(context, args.term, "en") search = search.response if(search.body.status === 1) return editOrReply(context, createEmbed("warning", context, search.body.message)) let pages = [] for(const r of search.body.results){ for(const d of r.entries){ pages.push(renderDictionaryEntry(context, r, d, "en")) } } return await createDynamicCardStack(context, { cards: pages }); }catch(e){ if(e.response?.body?.status && e.response.body.status === 2) return editOrReply(context, createEmbed("warning", context, e.response.body.message)) console.log(e) return editOrReply(context, createEmbed("error", context, `Unable to perform dictionary lookup.`)) } }, };