From bf8d5ce76edb3985b9f466553f8e4520a6d676bc Mon Sep 17 00:00:00 2001 From: bignutty <3515180-bignutty@users.noreply.gitlab.com> Date: Wed, 12 Mar 2025 17:57:25 +0100 Subject: [PATCH] refresh dictionary card --- .../interaction/slash/utils/dictionary.js | 92 ++++++++++--------- commands/message/utils/dictionary.js | 92 ++++++++++--------- 2 files changed, 102 insertions(+), 82 deletions(-) diff --git a/commands/interaction/slash/utils/dictionary.js b/commands/interaction/slash/utils/dictionary.js index 62daed1..5c11b02 100644 --- a/commands/interaction/slash/utils/dictionary.js +++ b/commands/interaction/slash/utils/dictionary.js @@ -1,52 +1,63 @@ const { dictionary } = require('#api'); -const { paginator } = require('#client'); -const { TRANSLATE_LANGUAGE_MAPPINGS, DICTIONARY_LANGUAGES, PERMISSION_GROUPS } = require('#constants'); +const {createDynamicCardStack} = require("#cardstack/index"); +const {PERMISSION_GROUPS} = require("#constants"); -const { createEmbed, formatPaginationEmbeds, page } = require('#utils/embed'); +const { createEmbed, page } = require('#utils/embed'); const { acknowledge } = require('#utils/interactions'); -const { link, iconPill, smallPill, icon, iconLinkPill, pill } = require('#utils/markdown') +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": `${iconPill("warning", "Offensive")}` + "offensive": `Offensive`, + "informal": `Informal` } -function createDictionaryPage(context, result, index, language){ - let phon = '' - if(result.phonetic) phon = `\n*${result.phonetic}*` - - let e = createEmbed("default", context, { - description: `${icon("definition")} **${link(`https://en.wiktionary.org/wiki/${encodeURIComponent(result.word)}`, result.word, "Definition on Wiktionary")}**`, +// 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(result.phonetic) e.description += smallPill(result.phonetic) - - if(language !== "en") e.description += `\n${TRANSLATE_LANGUAGE_MAPPINGS[language]} ${pill(DICTIONARY_LANGUAGES[language])}` - - let word = result.entries[index] - let defItms = [] - + 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 def of word.definitions.splice(0, 6)){ - let entry = `${i}. ${def.definition}` - if(def.example) entry += `\n - *${def.example}*` - if(def.synonyms) entry += `\n${icon("empty")}${def.synonyms.splice(0, 4).map((s)=>smallPill(s)).join(' ')}` - defItms.push(entry) - i++ + 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))) + if(d.antonyms) nyms = nyms.concat(d.antonyms.map((sd)=>pill(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(word.definitions.length >= 6 ) defItms.push(iconLinkPill("link", `https://www.google.com/search?q=define+${encodeURIComponent(result.word)}`, 'More results', 'View More Results')) + 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") - let type = word.type - if(word.labels) type += " " + word.labels.map((label)=>{if(LABELS[label]) return LABELS[label]; else return ""}).join(' ') - - e.description += `\n\n**${type}**\n${defItms.join('\n\n')}` - - return page(e); + return page(card); } module.exports = { @@ -82,22 +93,21 @@ module.exports = { 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)) + if(search.body.status === 1) return editOrReply(context, createEmbed("warning", context, search.body.message)) let pages = [] - let i = 0; - for(const d of search.body.results[0].entries){ - pages.push(createDictionaryPage(context, search.body.results[0], i, "en")) - i++; + for(const r of search.body.results){ + for(const d of r.entries){ + pages.push(renderDictionaryEntry(context, r, d, "en")) + } } - - await paginator.createPaginator({ - context, - pages: formatPaginationEmbeds(pages) + + 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)) + 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.`)) } diff --git a/commands/message/utils/dictionary.js b/commands/message/utils/dictionary.js index a228477..e644185 100644 --- a/commands/message/utils/dictionary.js +++ b/commands/message/utils/dictionary.js @@ -1,51 +1,62 @@ const { dictionary } = require('#api'); -const { paginator } = require('#client'); -const { TRANSLATE_LANGUAGE_MAPPINGS, DICTIONARY_LANGUAGES, PERMISSION_GROUPS } = require('#constants'); +const {createDynamicCardStack} = require("#cardstack/index"); +const {PERMISSION_GROUPS} = require("#constants"); -const { createEmbed, formatPaginationEmbeds, page } = require('#utils/embed'); +const { createEmbed, page } = require('#utils/embed'); const { acknowledge } = require('#utils/interactions'); -const { link, iconPill, smallPill, icon, iconLinkPill, pill } = require('#utils/markdown') +const { link, smallPill, icon, pill } = require('#utils/markdown') const { editOrReply } = require('#utils/message') const { dictionaryGetCodeFromAny } = require('#utils/translate'); +// TODO(unity): single constant const LABELS = { - "offensive": `${iconPill("warning", "Offensive")}` + "offensive": `Offensive`, + "informal": `Informal` } -function createDictionaryPage(context, result, index, language){ - let phon = '' - if(result.phonetic) phon = `\n*${result.phonetic}*` - - let e = createEmbed("default", context, { - description: `${icon("definition")} **${link(`https://en.wiktionary.org/wiki/${encodeURIComponent(result.word)}`, result.word, "Definition on Wiktionary")}**`, +// 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(result.phonetic) e.description += smallPill(result.phonetic) - - if(language !== "en") e.description += `\n${TRANSLATE_LANGUAGE_MAPPINGS[language]} ${pill(DICTIONARY_LANGUAGES[language])}` - - let word = result.entries[index] - let defItms = [] - + 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 def of word.definitions.splice(0, 6)){ - let entry = `${i}. ${def.definition}` - if(def.example) entry += `\n - *${def.example}*` - if(def.synonyms) entry += `\n${icon("empty")}${def.synonyms.splice(0, 4).map((s)=>smallPill(s)).join(' ')}` - defItms.push(entry) - i++ + 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))) + if(d.antonyms) nyms = nyms.concat(d.antonyms.map((sd)=>pill(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(word.definitions.length >= 6 ) defItms.push(iconLinkPill("link", `https://www.google.com/search?q=define+${encodeURIComponent(result.word)}`, 'More results', 'View More Results')) + 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") - let type = word.type - if(word.labels) type += " " + word.labels.map((label)=>{if(LABELS[label]) return LABELS[label]; else return ""}).join(' ') - - e.description += `\n\n**${type}**\n${defItms.join('\n\n')}` - - return page(e); + return page(card); } module.exports = { @@ -76,22 +87,21 @@ module.exports = { let search = await dictionary(context, args.query, language) search = search.response - if(search.body.status == 1) return editOrReply(context, createEmbed("warning", context, search.body.message)) + if(search.body.status === 1) return editOrReply(context, createEmbed("warning", context, search.body.message)) let pages = [] - let i = 0; - for(const d of search.body.results[0].entries){ - pages.push(createDictionaryPage(context, search.body.results[0], i, language)) - i++; + for(const r of search.body.results){ + for(const d of r.entries){ + pages.push(renderDictionaryEntry(context, r, d, language)) + } } - - await paginator.createPaginator({ - context, - pages: formatPaginationEmbeds(pages) + + 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)) + 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.`)) }