mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-07 13:43:06 -04:00
108 lines
No EOL
3.5 KiB
JavaScript
108 lines
No EOL
3.5 KiB
JavaScript
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 { dictionaryGetCodeFromAny } = require('#utils/translate');
|
|
|
|
// 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',
|
|
label: 'query',
|
|
aliases: ['define', 'dict', 'definition', 'def', 'd'],
|
|
metadata: {
|
|
description: 'Looks up words and teminology in a dictionary.',
|
|
description_short: 'Dictionary word definitions.',
|
|
examples: ['define Gehen -lang de'],
|
|
category: 'utils',
|
|
usage: 'dictionary <query> [-lang <language>]',
|
|
slashCommand: "dictionary"
|
|
},
|
|
args: [
|
|
{name: 'lang', default: 'en', type: 'language', help: "Language to define in"},
|
|
],
|
|
permissionsClient: [...PERMISSION_GROUPS.baseline],
|
|
run: async (context, args) => {
|
|
await acknowledge(context);
|
|
|
|
|
|
let language = dictionaryGetCodeFromAny(args.lang);
|
|
|
|
if(!language) return editOrReply(context, createEmbed("warning", context, "Invalid Language"))
|
|
|
|
try{
|
|
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))
|
|
|
|
let pages = []
|
|
|
|
for(const r of search.body.results){
|
|
for(const d of r.entries){
|
|
pages.push(renderDictionaryEntry(context, r, d, language))
|
|
}
|
|
}
|
|
|
|
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.`))
|
|
}
|
|
},
|
|
}; |