UNPKG

constatic

Version:
5 lines (4 loc) 4.66 kB
import{discordEmojis as v,divider as f,equalsIgnoringCase as D,log as t,pathExists as A,sleep as j,uiMessage as e}from"#helpers";import{menus as h}from"#menus";import{confirm as b,input as q,select as E}from"@inquirer/prompts";import o from"chalk";import{glob as C}from"@reliverse/reglob";import U from"node:fs/promises";import l from"node:path";import B from"ora";import{fetchDiscordEmojis as I}from"./fetch.js";import{withDefaults as g}from"#prompts";const S=o.underline;async function X(m,n){const $=await I({props:m,token:n,notCheckAmount:!0});if(!$)return;const y=o.dim.underline(l.basename(m.cwd)+"/"),R=await q(g({message:e({"pt-BR":"Informe o caminho diret\xF3rio de imagens ","en-US":"Provide the image directory path "})+`${y} `,async validate(i){return await A(l.resolve(i))?!0:e({"en-US":`${i} is not a valid path`,"pt-BR":`${i} n\xE3o \xE9 um caminho v\xE1lido!`})},default:"./"})),w=B();w.start(e({"en-US":"\u{1F50D} Searching for files in nested folders...","pt-BR":"\u{1F50D} Procurando por arquivos nas pastas aninhadas..."}));const x=await C("**/*.{png,jpeg,gif}",{cwd:l.resolve(R),absolute:!0});w.text=e({"en-US":"\u{1F5C3}\uFE0F Getting information from found files...","pt-BR":"\u{1F5C3}\uFE0F Obtendo informa\xE7\xF5es dos arquivos encontrados..."});const d=[];for(const i of x){const u=await U.stat(i);if(Math.ceil(u.size/1024)>=256)continue;const a=l.extname(i),p=l.basename(i,a),s=await U.readFile(i,{encoding:"base64"}),c=`data:image/${a.slice(1)};base64,${s}`;d.push({base64:c,name:v.formatName(p)})}if(w.stop(),!d.length){t.fail(e({"en-US":"No images found in the given directory!","pt-BR":"Nenhum imagem encontrada no diret\xF3rio fornecido!"})),h.discord.emojis.main(m,n);return}if(t.success(e({"en-US":`Image files available for upload: ${d.length}`,"pt-BR":`Arquivos de imagens dispon\xEDveis para o envio: ${d.length}`})),d.length>=50&&(f(),t.warn(e({"en-US":["Depending on the amount of files,","this process may take some time due","to Discord API rate limitations."].join(` `),"pt-BR":["Dependendo da quantidade de arquivos","esse processo pode levar algum tempo devido","\xE0s limita\xE7\xF5es de taxa da API do Discord."].join(` `)})),f()),!await b(g({message:e({"en-US":"Do you want to confirm the image files upload process?","pt-BR":"Voc\xEA quer confirmar o processo de upload dos arquivos de imagem?"})}))){await j(400),h.discord.emojis.main(m,n);return}const k=await E(g({message:e({"en-US":"Select the overwrite method","pt-BR":"Selecione o m\xE9todo de sobrescri\xE7\xE3o"}),choices:[{name:o.green("\u270E ")+e({"en-US":"Overwrite if exists","pt-BR":"Sobrescrever se existir"},o.green),value:"all"},{name:o.cyan("\u25A3 ")+e({"en-US":"Ask before overwrite","pt-BR":"Perguntar antes de sobrescrever"},o.cyan),value:"ask"},{name:o.blue("\u25CE ")+e({"en-US":"Skip and do not overwrite","pt-BR":"Pular e n\xE3o sobrescrever"},o.blue),value:"skip"}]}));for(const{name:i,base64:u}of d){const r=S.yellow(i),a=$.find(c=>D(c.name,i));if(a){const c=async()=>{(await v.delete(n,a.id)).success||t.error(e({"en-US":`An error occurred while trying delete the ${r} emoji!`,"pt-BR":`Ocorreu um erro ao tentar excluir o emoji ${r}!`},o.red))};switch(k){case"all":{await c();break}case"ask":{if(t.warn(e({"en-US":`An emoji named ${r} already exists!`,"pt-BR":`Um emoji chamado ${r} j\xE1 existe!`})),!await b(g({message:e({"en-US":"Do you want to overwrite?","pt-BR":"Deseja sobrescrever?"})})))continue;await c();break}case"skip":{t.custom(o.yellow("\u25CE"),e({"en-US":`${o.bold.yellow("Skipped")} \u2192 Skipped existing ${r} emoji!`,"pt-BR":`${o.bold.yellow("Pulado")} \u2192 Emoji ${r} j\xE1 existente pulado!`}));continue}}}const p=B();p.start(e({"en-US":`Uploading emoji ${o.yellow.underline(i)}`,"pt-BR":`Enviando emoji ${o.yellow.underline(i)}`}));const s=await v.create(n,{image:u,name:i});if(p.stop(),!s.success&&s.exists){t.fail(e({"en-US":`An emoji named ${r} already exists for this application!`,"pt-BR":`Um emoji chamado ${r} j\xE1 existe para essa aplica\xE7\xE3o!`},o.red));continue}if(!s.success){t.error(e({"en-US":`An error occurred while trying ${a?"overwrite":"create"} the ${r} emoji!`,"pt-BR":`Ocorreu um erro ao tentar ${a?"sobrescrever":"criar"} o emoji ${r}!`},o.red)+o.red.dim(s.error));continue}t.custom(o.green("\u2726"),e({"en-US":`${o.bold.green(a?"Overwrited":"Created")} \u2192 ${r} ${S.dim.green(s.data.id)}`,"pt-BR":`${o.bold.green(a?"Sobrescrito":"Criado")} \u2192 ${r} ${S.dim.green(s.data.id)}`}))}f(),t.success(e({"en-US":"Uploading process completed!","pt-BR":"Processo de envio conclu\xEDdo!"},o.green)),f(),await j(500),h.discord.emojis.main(m,n)}export{X as discordEmojisUploadMenu};