mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-07 05:42:57 -04:00
refresh dictionary card
This commit is contained in:
parent
a509476622
commit
bf8d5ce76e
2 changed files with 102 additions and 82 deletions
|
@ -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.`))
|
||||
}
|
||||
|
|
|
@ -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.`))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue