Created
March 19, 2023 23:22
-
-
Save erickythierry/461d92ce0fd8ab49e186586603d9c0f0 to your computer and use it in GitHub Desktop.
função useMultiFileAuthState do Baileys de maneira mais legivel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { promises } from 'fs'; | |
const { writeFile, readFile, unlink, stat, mkdir } = promises; | |
import { join } from 'path'; | |
import { WAProto as proto, initAuthCreds, BufferJSON } from "@adiwajshing/baileys" | |
// import { proto } from "../../WAProto"; | |
// import { initAuthCreds } from "./auth-utils"; | |
// import { BufferJSON } from "./generics"; | |
export default async function useMultiFileAuthState(folder) { | |
async function writeData(data, file) { | |
const filePath = join(folder, fixFileName(file)); | |
const dataString = JSON.stringify(data, BufferJSON.replacer); | |
await writeFile(filePath, dataString); | |
return; | |
}; | |
async function readData(file) { | |
const filePath = join(folder, fixFileName(file)); | |
try { | |
const rawData = await readFile(filePath, { encoding: 'utf-8' }); | |
const parsedData = JSON.parse(rawData, BufferJSON.reviver); | |
return parsedData; | |
} catch (error) { | |
return null; | |
} | |
} | |
async function removeData(file, folder) { | |
try { | |
const filePath = join(folder, fixFileName(file)); | |
await unlink(filePath); | |
} catch (error) { | |
// Não fazer nada em caso de erro | |
} | |
} | |
let folderInfo; | |
try { | |
folderInfo = await stat(folder); | |
} catch (error) { | |
// Não fazer nada em caso de erro | |
} | |
if (folderInfo) { | |
if (!folderInfo.isDirectory()) { | |
throw new Error(`Foi encontrado algo que não é um diretório em ${folder}. Por favor, exclua ou especifique um local diferente.`); | |
} | |
} else { | |
await mkdir(folder, { recursive: true }); | |
} | |
function fixFileName(file) { | |
if (!file) { | |
return file; | |
} | |
const newFileName = file.replace(/\//g, '__').replace(/:/g, '-'); | |
return newFileName; | |
} | |
let creds = await readData('creds.json'); | |
if (!creds) { | |
creds = await initAuthCreds(); | |
} | |
return { | |
state: { | |
creds, | |
keys: { | |
get: async (type, ids) => { | |
const data = {}; | |
await Promise.all(ids.map(async (id) => { | |
let value = await readData(`${type}-${id}.json`); | |
if (type === 'app-state-sync-key' && value) { | |
value = proto.Message.AppStateSyncKeyData.fromObject(value); | |
} | |
data[id] = value; | |
})); | |
return data; | |
}, | |
set: async (data) => { | |
const tasks = []; | |
for (const category in data) { | |
for (const id in data[category]) { | |
const value = data[category][id]; | |
const file = `${category}-${id}.json`; | |
tasks.push(value ? writeData(value, file) : removeData(file)); | |
} | |
} | |
await Promise.all(tasks); | |
} | |
} | |
}, | |
saveCreds: () => { | |
return writeData(creds, 'creds.json'); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment