language support for translate

This commit is contained in:
derpystuff 2023-11-04 01:04:38 +01:00
parent db8de1c6f9
commit 3cea3331da
4 changed files with 77 additions and 10 deletions

View file

@ -1,17 +1,19 @@
const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed') const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed')
const { link, iconPill, smallPill, citation, icon } = require('../../../labscore/utils/markdown') const { link, iconPill, smallPill, icon, iconLinkPill, pill } = require('../../../labscore/utils/markdown')
const { editOrReply } = require('../../../labscore/utils/message') const { editOrReply } = require('../../../labscore/utils/message')
const { paginator } = require('../../../labscore/client'); const { paginator } = require('../../../labscore/client');
const { dictionary } = require('../../../labscore/api'); const { dictionary } = require('../../../labscore/api');
const { Permissions } = require("detritus-client/lib/constants"); const { Permissions } = require("detritus-client/lib/constants");
const { dictionaryGetCodeFromAny } = require('../../../labscore/utils/translate');
const { TRANSLATE_LANGUAGE_MAPPINGS, DICTIONARY_LANGUAGES } = require('../../../labscore/constants');
const LABELS = { const LABELS = {
"offensive": `${iconPill("warning", "Offensive")}` "offensive": `${iconPill("warning", "Offensive")}`
} }
function createDictionaryPage(context, result, index){ function createDictionaryPage(context, result, index, language){
let phon = '' let phon = ''
if(result.phonetic) phon = `\n*${result.phonetic}*` if(result.phonetic) phon = `\n*${result.phonetic}*`
@ -22,12 +24,14 @@ function createDictionaryPage(context, result, index){
if(result.phonetic) e.description += smallPill(result.phonetic) 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 word = result.entries[index]
let defItms = [] let defItms = []
let i = 1; let i = 1;
for(const def of word.definitions){ for(const def of word.definitions.splice(0, 6)){
let entry = `${i}. ${def.definition}` let entry = `${i}. ${def.definition}`
if(def.example) entry += `\n - *${def.example}*` 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(def.synonyms) entry += `\n${icon("empty")}${def.synonyms.splice(0, 4).map((s)=>smallPill(s)).join(' ')}`
@ -35,6 +39,8 @@ function createDictionaryPage(context, result, index){
i++ i++
} }
if(word.definitions.length >= 6 ) defItms.push(iconLinkPill("link", `https://www.google.com/search?q=define+${encodeURIComponent(result.word)}`, 'More results', 'View More Results'))
let type = word.type let type = word.type
if(word.labels) type += " " + word.labels.map((label)=>{if(LABELS[label]) return LABELS[label]; else return ""}).join(' ') if(word.labels) type += " " + word.labels.map((label)=>{if(LABELS[label]) return LABELS[label]; else return ""}).join(' ')
@ -51,15 +57,26 @@ module.exports = {
metadata: { metadata: {
description: 'Returns dictionary definitions for words.', description: 'Returns dictionary definitions for words.',
description_short: 'Dictionary word definitions.', description_short: 'Dictionary word definitions.',
examples: ['dictionary Walking'], examples: ['dictionary Gehen -lang de'],
category: 'utils', category: 'utils',
usage: 'define <query>' usage: 'define <query> [-lang <language>]'
}, },
args: [
{name: 'lang', default: 'en', type: 'language', help: "Language to define in"},
],
permissionsClient: [Permissions.EMBED_LINKS, Permissions.SEND_MESSAGES, Permissions.USE_EXTERNAL_EMOJIS, Permissions.READ_MESSAGE_HISTORY], permissionsClient: [Permissions.EMBED_LINKS, Permissions.SEND_MESSAGES, Permissions.USE_EXTERNAL_EMOJIS, Permissions.READ_MESSAGE_HISTORY],
run: async (context, args) => { run: async (context, args) => {
context.triggerTyping(); context.triggerTyping();
let language = dictionaryGetCodeFromAny(args.lang);
console.log(language)
if(!language) return editOrReply(context, createEmbed("warning", context, "Invalid Language"))
try{ try{
let search = await dictionary(context, args.query) 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))
@ -68,7 +85,7 @@ module.exports = {
let i = 0; let i = 0;
for(const d of search.body.results[0].entries){ for(const d of search.body.results[0].entries){
pages.push(createDictionaryPage(context, search.body.results[0], i)) pages.push(createDictionaryPage(context, search.body.results[0], i, language))
i++; i++;
} }

View file

@ -291,9 +291,10 @@ module.exports.tiktok = async function(context, text, voice){
}) })
} }
module.exports.dictionary = async function(context, query){ module.exports.dictionary = async function(context, query, language){
return await request(Api.UTILS_DICTIONARY, "GET", {}, { return await request(Api.UTILS_DICTIONARY, "GET", {}, {
q: query q: query,
l: language
}) })
} }

View file

@ -435,6 +435,46 @@ module.exports.MICROSOFT_VOICE_CONFIG = {
"Male Whisper": { pitch: 113, speed: 170 } "Male Whisper": { pitch: 113, speed: 170 }
} }
module.exports.DICTIONARY_LANGUAGES = Object.freeze({
ar: "Arabic",
ca: "Catalan",
cs: "Czech",
da: "Danish",
de: "German",
el: "Greek",
"en-uk": "English (UK)",
en: "English (US)",
es: "Spanish",
fi: "Finnish",
fr: "French",
hi: "Hindi",
hr: "Croatian",
id: "Indonesian",
it: "Italian",
iw: "Hebrew",
ja: "Japanese",
ko: "Korean",
nl: "Dutch",
no: "Norwegian",
pl: "Polish",
"pt-br": "Portuguese",
"pt-pt": "Portuguese",
pt: "Portuguese",
ro: "Romanian",
ru: "Russian",
sr: "Serbian",
sk: "Slovak",
sv: "Swedish",
th: "Thai",
tl: "Tagalog",
tr: "Turkish",
uk: "Ukrainian",
vi: "Vietnamese",
"zh-cn": "Chinese",
"zh-tw": "Chinese",
zh: "Chinese"
})
// hey if you're looking at this and annoyed that a language doesnt // hey if you're looking at this and annoyed that a language doesnt
// work lmk on discord @bignutty and i'll add it (if reasonable) // work lmk on discord @bignutty and i'll add it (if reasonable)
// same goes for emoji below // same goes for emoji below

View file

@ -1,4 +1,4 @@
const { TRANSLATE_LANGUAGES, TRANSLATE_LANGUAGE_MAPPINGS, TRANSLATE_LANGUAGE_ALIASES } = require("../constants"); const { TRANSLATE_LANGUAGES, TRANSLATE_LANGUAGE_MAPPINGS, TRANSLATE_LANGUAGE_ALIASES, DICTIONARY_LANGUAGES } = require("../constants");
function getCode(desiredLang) { function getCode(desiredLang) {
if (!desiredLang) { if (!desiredLang) {
@ -38,6 +38,15 @@ module.exports.getCodeFromAny = function (prompt) {
return languages[0]; return languages[0];
}; };
module.exports.dictionaryGetCodeFromAny = function (prompt) {
if(DICTIONARY_LANGUAGES[prompt.toLowerCase()]) return prompt.toLowerCase()
let languages = [];
for(const i of Object.keys(DICTIONARY_LANGUAGES)) if(!languages.includes(i) && DICTIONARY_LANGUAGES[i].toLowerCase() == prompt.toLowerCase()) languages.push(i)
return languages[0];
};
module.exports.isSupported = function (desiredLang) { module.exports.isSupported = function (desiredLang) {
return Boolean(getCode(desiredLang)); return Boolean(getCode(desiredLang));
} }