Skip to content

Commit

Permalink
❌ Don't use the encryption module inside lang
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed Dec 17, 2024
1 parent b1ee755 commit e7a8c66
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
31 changes: 17 additions & 14 deletions core/src/discord/commands/lang.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { RootPATH } from '@/index.js'
import { lang } from '@/register'
import { Crypt } from 'crypt'
import { Command } from 'discord'
import { type ApplicationCommandOptionChoiceData, ApplicationCommandOptionType, ApplicationCommandType, EmbedBuilder } from 'discord.js'
import { glob } from 'glob'
Expand Down Expand Up @@ -39,21 +41,22 @@ new Command({
const { options } = interaction
const language = options.getString('name', true)

await lang.setLanguage(language, true)
.then(async () => {
await interaction.editReply({
embeds: [new EmbedBuilder({
title: i18('commands.lang.sucess')
}).setColor('Green')]
})
const languageChange = await lang.set(language)
const crypt = new Crypt()
await crypt.write({ language: 'en' })

if (languageChange === language)
await interaction.editReply({
embeds: [new EmbedBuilder({
title: i18('commands.lang.sucess')
}).setColor('Green')]
})
.catch(async (err) => {
console.log(err)
await interaction.editReply({
embeds: [new EmbedBuilder({
title: i18('commands.lang.error')
}).setColor('Red')]
})
else {
await interaction.editReply({
embeds: [new EmbedBuilder({
title: i18('commands.lang.error')
}).setColor('Red')]
})
}
},
})
9 changes: 6 additions & 3 deletions core/src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export const { i18, lang } = await (async () => {

global.i18 = i18

const crypt = await new Crypt().read(true)
if (crypt?.language === undefined) {
await lang.selectLanguage()
const crypt = new Crypt()
const data = await crypt.read()

if (data?.language === undefined) {
const language = await lang.select()
await crypt.write({ language })
}
8 changes: 1 addition & 7 deletions packages/crypt/src/controllers/Crypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ const PRIVATE_KEY_PATH = resolve(ROOT_PATH, '../privateKey.pem')
const PUBLIC_KEY_PATH = resolve(ROOT_PATH, '../publicKey.pem')

export class Crypt {
private readonly pathKey: string

constructor() {
this.pathKey = resolve(ROOT_PATH, '../')
}

async checker() {
// Verificação de arquivos chave
if (!(await exists(KEY_PATH)) && process.env?.Token === undefined) await this.create()
Expand Down Expand Up @@ -150,7 +144,7 @@ export class Crypt {
).toString(CryptoJS.enc.Utf8)

const outputData = JSON.parse(decrypted) as DataCrypted
if (outputData.language) lang.setLanguage(outputData.language)
if (outputData.language) lang.set(outputData.language)

for (const [key, value] of Object.entries(outputData)) {
credentials.set(key, value)
Expand Down
16 changes: 7 additions & 9 deletions packages/lang/src/controllers/Lang.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import flags from 'country-code-to-flag-emoji'
import { Crypt } from 'crypt'
import { existsSync, watch } from 'fs'
import { mkdir, readFile, writeFile } from 'fs/promises'
import { glob } from 'glob'
Expand Down Expand Up @@ -54,22 +53,20 @@ export class Lang<Languages extends readonly LangLyrics<string, Record<string, u
}


async setLanguage (lang: string, change?: boolean) {
async set (lang: string) {
const path = join(this.sourcePath, 'locales', lang)
const crypt = new Crypt()

if (!(await exists(path))) {
console.log(`⛔ The selected language (${lang}) does not exist, using English by default`)
if (change) await crypt.write({ language: 'en' })
this.language = 'en'
return
return this.language
}

this.language = lang
if (change) await crypt.write({ language: lang })
return this.language
}

async selectLanguage () {
async select (): Promise<string> {
const path = join(this.sourcePath, 'locales')
const allLangs = (await glob('**/*.json', { cwd: path })).map((lang) => lang.split('/')[0])
const langs = []
Expand All @@ -85,7 +82,8 @@ export class Lang<Languages extends readonly LangLyrics<string, Record<string, u
initial: 1
})
if (response.Language === undefined) throw new Error('Please select a language')

this.setLanguage(response.Language, true)

await this.set(response.Language)
return response.Language
}
}

0 comments on commit e7a8c66

Please sign in to comment.