mirror of
https://github.com/pieartsy/PluralFlux.git
synced 2026-04-14 20:15:28 +10:00
Tests are mostly complete (though some are failing -- to be fixed in the async/await refactor. Also refactored, fixed bugs found while testing, and added ability to type `pf;m` instead of `pf;member`. --------- Co-authored-by: Aster Fialla <asterfialla@gmail.com>
43 lines
1.8 KiB
JavaScript
43 lines
1.8 KiB
JavaScript
import {enums} from "../enums.js";
|
|
import {memberHelper} from "./memberHelper.js";
|
|
|
|
const ih = {};
|
|
|
|
/**
|
|
* Tries to import from Pluralkit.
|
|
*
|
|
* @async
|
|
* @param {string} authorId - The author of the message
|
|
* @param {string | null} [attachmentUrl] - The attached JSON url.
|
|
* @returns {string} A successful addition of all members.
|
|
* @throws {Error} When the member exists, or creating a member doesn't work.
|
|
*/
|
|
ih.pluralKitImport = async function (authorId, attachmentUrl= null) {
|
|
if (!attachmentUrl) {
|
|
throw new Error(enums.err.NOT_JSON_FILE);
|
|
}
|
|
return fetch(attachmentUrl).then((res) => res.json()).then(async(pkData) => {
|
|
const pkMembers = pkData.members;
|
|
let errors = [];
|
|
const addedMembers = [];
|
|
for (let pkMember of pkMembers) {
|
|
const proxy = pkMember.proxy_tags[0] ? `${pkMember.proxy_tags[0].prefix ?? ''}text${pkMember.proxy_tags[0].suffix ?? ''}` : null;
|
|
await memberHelper.addFullMember(authorId, pkMember.name, pkMember.display_name, proxy, pkMember.avatar_url).then((memberObj) => {
|
|
addedMembers.push(memberObj.member.name);
|
|
if (memberObj.errors.length > 0) {
|
|
errors.push(`\n**${pkMember.name}:** `)
|
|
errors = errors.concat(memberObj.errors);
|
|
}
|
|
}).catch(e => {
|
|
errors.push(e.message);
|
|
});
|
|
}
|
|
const aggregatedText = addedMembers.length > 0 ? `Successfully added members: ${addedMembers.join(', ')}` : `${enums.err.NO_MEMBERS_IMPORTED}`;
|
|
if (errors.length > 0) {
|
|
throw new AggregateError(errors, aggregatedText);
|
|
}
|
|
return aggregatedText;
|
|
});
|
|
}
|
|
|
|
export const importHelper = ih; |