mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-07 13:43:06 -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 { dictionary } = require('#api');
|
||||||
const { paginator } = require('#client');
|
const {createDynamicCardStack} = require("#cardstack/index");
|
||||||
const { TRANSLATE_LANGUAGE_MAPPINGS, DICTIONARY_LANGUAGES, PERMISSION_GROUPS } = require('#constants');
|
const {PERMISSION_GROUPS} = require("#constants");
|
||||||
|
|
||||||
const { createEmbed, formatPaginationEmbeds, page } = require('#utils/embed');
|
const { createEmbed, page } = require('#utils/embed');
|
||||||
const { acknowledge } = require('#utils/interactions');
|
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 { editOrReply } = require('#utils/message')
|
||||||
|
|
||||||
const { ApplicationCommandOptionTypes, InteractionContextTypes, ApplicationIntegrationTypes } = require('detritus-client/lib/constants');
|
const { ApplicationCommandOptionTypes, InteractionContextTypes, ApplicationIntegrationTypes } = require('detritus-client/lib/constants');
|
||||||
|
|
||||||
|
// TODO(unity): single constant
|
||||||
const LABELS = {
|
const LABELS = {
|
||||||
"offensive": `${iconPill("warning", "Offensive")}`
|
"offensive": `Offensive`,
|
||||||
|
"informal": `Informal`
|
||||||
}
|
}
|
||||||
|
|
||||||
function createDictionaryPage(context, result, index, language){
|
// TODO(unity)
|
||||||
let phon = ''
|
function renderDictionaryEntry(context, result, definition, language) {
|
||||||
if(result.phonetic) phon = `\n*${result.phonetic}*`
|
let card = createEmbed("default", context, {
|
||||||
|
description: `### ${result.word}\n-# ${result.phonetic} • ${definition.type}\n\n`,
|
||||||
let e = createEmbed("default", context, {
|
url: `https://en.wiktionary.org/wiki/${encodeURIComponent(result.word)}`,
|
||||||
description: `${icon("definition")} **${link(`https://en.wiktionary.org/wiki/${encodeURIComponent(result.word)}`, result.word, "Definition on Wiktionary")}**`,
|
|
||||||
fields: []
|
fields: []
|
||||||
})
|
})
|
||||||
|
|
||||||
if(result.phonetic) e.description += smallPill(result.phonetic)
|
if(definition.labels?.filter((l)=>LABELS[l]).length >= 1) card.description += `-# ⚠ ${definition.labels.map((l)=>LABELS[l]).join(" ⚠ ")}\n`;
|
||||||
|
|
||||||
if(language !== "en") e.description += `\n${TRANSLATE_LANGUAGE_MAPPINGS[language]} ${pill(DICTIONARY_LANGUAGES[language])}`
|
|
||||||
|
|
||||||
let word = result.entries[index]
|
|
||||||
let defItms = []
|
|
||||||
|
|
||||||
|
|
||||||
|
let defs = [];
|
||||||
let i = 1;
|
let i = 1;
|
||||||
for(const def of word.definitions.splice(0, 6)){
|
for(const d of definition.definitions){
|
||||||
let entry = `${i}. ${def.definition}`
|
let def = `${i++}. ${d.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(' ')}`
|
if(d.labels?.filter((l)=>LABELS[l]).length >= 1) def = `-# ⚠ ${d.labels.map((l)=>LABELS[l]).join(" ⚠ ")}\n` + def;
|
||||||
defItms.push(entry)
|
if(d.example) def += `\n - *${d.example}*`
|
||||||
i++
|
|
||||||
|
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
|
return page(card);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -82,22 +93,21 @@ module.exports = {
|
||||||
let search = await dictionary(context, args.term, "en")
|
let search = await dictionary(context, args.term, "en")
|
||||||
search = search.response
|
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 pages = []
|
||||||
|
|
||||||
let i = 0;
|
for(const r of search.body.results){
|
||||||
for(const d of search.body.results[0].entries){
|
for(const d of r.entries){
|
||||||
pages.push(createDictionaryPage(context, search.body.results[0], i, "en"))
|
pages.push(renderDictionaryEntry(context, r, d, "en"))
|
||||||
i++;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await paginator.createPaginator({
|
return await createDynamicCardStack(context, {
|
||||||
context,
|
cards: pages
|
||||||
pages: formatPaginationEmbeds(pages)
|
|
||||||
});
|
});
|
||||||
}catch(e){
|
}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)
|
console.log(e)
|
||||||
return editOrReply(context, createEmbed("error", context, `Unable to perform dictionary lookup.`))
|
return editOrReply(context, createEmbed("error", context, `Unable to perform dictionary lookup.`))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,51 +1,62 @@
|
||||||
const { dictionary } = require('#api');
|
const { dictionary } = require('#api');
|
||||||
const { paginator } = require('#client');
|
const {createDynamicCardStack} = require("#cardstack/index");
|
||||||
const { TRANSLATE_LANGUAGE_MAPPINGS, DICTIONARY_LANGUAGES, PERMISSION_GROUPS } = require('#constants');
|
const {PERMISSION_GROUPS} = require("#constants");
|
||||||
|
|
||||||
const { createEmbed, formatPaginationEmbeds, page } = require('#utils/embed');
|
const { createEmbed, page } = require('#utils/embed');
|
||||||
const { acknowledge } = require('#utils/interactions');
|
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 { editOrReply } = require('#utils/message')
|
||||||
const { dictionaryGetCodeFromAny } = require('#utils/translate');
|
const { dictionaryGetCodeFromAny } = require('#utils/translate');
|
||||||
|
|
||||||
|
// TODO(unity): single constant
|
||||||
const LABELS = {
|
const LABELS = {
|
||||||
"offensive": `${iconPill("warning", "Offensive")}`
|
"offensive": `Offensive`,
|
||||||
|
"informal": `Informal`
|
||||||
}
|
}
|
||||||
|
|
||||||
function createDictionaryPage(context, result, index, language){
|
// TODO(unity)
|
||||||
let phon = ''
|
function renderDictionaryEntry(context, result, definition, language) {
|
||||||
if(result.phonetic) phon = `\n*${result.phonetic}*`
|
let card = createEmbed("default", context, {
|
||||||
|
description: `### ${result.word}\n-# ${result.phonetic} • ${definition.type}\n\n`,
|
||||||
let e = createEmbed("default", context, {
|
url: `https://en.wiktionary.org/wiki/${encodeURIComponent(result.word)}`,
|
||||||
description: `${icon("definition")} **${link(`https://en.wiktionary.org/wiki/${encodeURIComponent(result.word)}`, result.word, "Definition on Wiktionary")}**`,
|
|
||||||
fields: []
|
fields: []
|
||||||
})
|
})
|
||||||
|
|
||||||
if(result.phonetic) e.description += smallPill(result.phonetic)
|
if(definition.labels?.filter((l)=>LABELS[l]).length >= 1) card.description += `-# ⚠ ${definition.labels.map((l)=>LABELS[l]).join(" ⚠ ")}\n`;
|
||||||
|
|
||||||
if(language !== "en") e.description += `\n${TRANSLATE_LANGUAGE_MAPPINGS[language]} ${pill(DICTIONARY_LANGUAGES[language])}`
|
|
||||||
|
|
||||||
let word = result.entries[index]
|
|
||||||
let defItms = []
|
|
||||||
|
|
||||||
|
|
||||||
|
let defs = [];
|
||||||
let i = 1;
|
let i = 1;
|
||||||
for(const def of word.definitions.splice(0, 6)){
|
for(const d of definition.definitions){
|
||||||
let entry = `${i}. ${def.definition}`
|
let def = `${i++}. ${d.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(' ')}`
|
if(d.labels?.filter((l)=>LABELS[l]).length >= 1) def = `-# ⚠ ${d.labels.map((l)=>LABELS[l]).join(" ⚠ ")}\n` + def;
|
||||||
defItms.push(entry)
|
if(d.example) def += `\n - *${d.example}*`
|
||||||
i++
|
|
||||||
|
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
|
return page(card);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -76,22 +87,21 @@ module.exports = {
|
||||||
let search = await dictionary(context, args.query, language)
|
let search = await dictionary(context, args.query, language)
|
||||||
search = search.response
|
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 pages = []
|
||||||
|
|
||||||
let i = 0;
|
for(const r of search.body.results){
|
||||||
for(const d of search.body.results[0].entries){
|
for(const d of r.entries){
|
||||||
pages.push(createDictionaryPage(context, search.body.results[0], i, language))
|
pages.push(renderDictionaryEntry(context, r, d, language))
|
||||||
i++;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await paginator.createPaginator({
|
return await createDynamicCardStack(context, {
|
||||||
context,
|
cards: pages
|
||||||
pages: formatPaginationEmbeds(pages)
|
|
||||||
});
|
});
|
||||||
}catch(e){
|
}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)
|
console.log(e)
|
||||||
return editOrReply(context, createEmbed("error", context, `Unable to perform dictionary lookup.`))
|
return editOrReply(context, createEmbed("error", context, `Unable to perform dictionary lookup.`))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue