forked from PluralFlux/PluralFlux
refactor: Removing then/catch from async/await calls (#22)
* refactored async/await for import helper to not also use then/catch * added enum * refactor webhookHelper and tests to not use then/catch * changed docstring * refactoring bot and tests to not use then/catch * refactoring commands.js and tests to not use then/catch * refactoring memberHelper.js and tests to not use then/catch * removing then/catch from messageHelper.test.js * fixed set up for commands tests * edited bot to have top level main function * one more test in commands.js, and removed console.error * fixed typo in webhookHelper * forgot to switch over some tests in bot.test and commands.test * removed console.log from import helper * put console.error in commands * converted utils.js to not use then/catch * tested utils checkImageFormatValidity * removed jest-fetch-mock since it turns out I was just manually mocking it anyway * refactored database to not use then/catch * added dash to commands.js and test to pass * added the remaining webhook tests * changed utils to check for 10MB size not 1MB * removed unnecessary try/catch from utils * Simplify getWebhook to use .find() instead of foreach logic * make memberCommand exit when error occurs with parseMemberCommand * changed commands.js to not have user interaction within the catch * updated console.error message in database.js * made importHelper mock throw error instead of "resolve" error * replaced "pk;" with "pf;" in test * Got rid of unnecessary check for empty message from user (Fluxer doesn't allow this to happen) Removed export of token * getAllMembersInfo checks for fields.length * added default case to memberCommandHandler to throw error if command is not recognized * reversed check for valid proxy (was returning valid if the proxy existed and invalid if it didn't) * pushes e.message instead of full error object to errors array in importHelper * adjusted tests to properly use mockRejectedValue for async rejections * changed getAllMembersInfo map to say `index` not `name` as it actually gets the index of a member and then the member object * adjusted importHelper to properly test throwing of aggregate error * revamped setting of expiration warning (moved to utils and changed logic, wrote tests) --------- Co-authored-by: Aster Fialla <asterfialla@gmail.com>
This commit is contained in:
@@ -26,22 +26,28 @@ cmds.commandsMap.set('member', {
|
||||
* @param {string[]} args - The parsed arguments
|
||||
*
|
||||
**/
|
||||
cmds.memberCommand = async function(message, args) {
|
||||
cmds.memberCommand = async function (message, args) {
|
||||
const authorFull = `${message.author.username}#${message.author.discriminator}`
|
||||
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
|
||||
const attachmentExpires = message.attachments.size > 0 ? message.attachments.first().expires_at : null;
|
||||
|
||||
const reply = await memberHelper.parseMemberCommand(message.author.id, authorFull, args, attachmentUrl, attachmentExpires).catch(async (e) =>{console.error(e); await message.reply(e.message);});
|
||||
let reply;
|
||||
try {
|
||||
reply = await memberHelper.parseMemberCommand(message.author.id, authorFull, args, attachmentUrl, attachmentExpires)
|
||||
} catch (e) {
|
||||
return await message.reply(e.message);
|
||||
}
|
||||
|
||||
if (typeof reply === 'string') {
|
||||
await message.reply(reply);
|
||||
}
|
||||
else if (reply instanceof EmbedBuilder) {
|
||||
} else if (reply instanceof EmbedBuilder) {
|
||||
await message.reply({embeds: [reply]})
|
||||
}
|
||||
else if (typeof reply === 'object') {
|
||||
const errorsText = reply.errors.length > 0 ? reply.errors.join('\n- ') : null;
|
||||
return await message.reply({content: `${reply.success} ${errorsText ? `\n\n${enums.err.ERRORS_OCCURRED}\n` + errorsText : ""}`, embeds: [reply.embed]})
|
||||
} else if (typeof reply === 'object') {
|
||||
// The little dash is so that the errors print out in bullet points in Fluxer
|
||||
const errorsText = reply.errors.length > 0 ? '- ' + reply.errors.join('\n- ') : null;
|
||||
return await message.reply({
|
||||
content: `${reply.success} ${errorsText ? `\n\n${enums.err.ERRORS_OCCURRED}\n` + errorsText : ""}`,
|
||||
embeds: [reply.embed]
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -60,10 +66,10 @@ cmds.commandsMap.set('help', {
|
||||
.setTitle('Commands')
|
||||
.setDescription(enums.help.PLURALFLUX)
|
||||
.addFields(...fields)
|
||||
.setFooter({ text: `Prefix: ${messageHelper.prefix}` })
|
||||
.setFooter({text: `Prefix: ${messageHelper.prefix}`})
|
||||
.setTimestamp();
|
||||
|
||||
await message.reply({ embeds: [embed] });
|
||||
await message.reply({embeds: [embed]});
|
||||
},
|
||||
})
|
||||
|
||||
@@ -82,29 +88,35 @@ cmds.commandsMap.set('import', {
|
||||
* @param {string[]} args - The parsed arguments
|
||||
*
|
||||
**/
|
||||
cmds.importCommand = async function(message, args) {
|
||||
cmds.importCommand = async function (message, args) {
|
||||
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
|
||||
if ((message.content.includes('--help') || (args[0] === '' && args.length === 1)) && !attachmentUrl ) {
|
||||
if ((message.content.includes('--help') || (args[0] === '' && args.length === 1)) && !attachmentUrl) {
|
||||
return await message.reply(enums.help.IMPORT);
|
||||
}
|
||||
return await importHelper.pluralKitImport(message.author.id, attachmentUrl).then(async (successfullyAdded) => {
|
||||
await message.reply(successfullyAdded);
|
||||
}).catch(async (error) => {
|
||||
let errorsText;
|
||||
try {
|
||||
const successfullyAdded = await importHelper.pluralKitImport(message.author.id, attachmentUrl)
|
||||
return await message.reply(successfullyAdded);
|
||||
} catch (error) {
|
||||
if (error instanceof AggregateError) {
|
||||
// errors.message can be a list of successfully added members, or say that none were successful.
|
||||
let errorsText = `${error.message}.\n\n${enums.err.ERRORS_OCCURRED}\n${error.errors.join('\n')}`;
|
||||
|
||||
await message.reply(errorsText).catch(async () => {
|
||||
const returnedBuffer = messageHelper.returnBufferFromText(errorsText);
|
||||
await message.reply({content: returnedBuffer.text, files: [{ name: 'text.txt', data: returnedBuffer.file }]
|
||||
})
|
||||
});
|
||||
errorsText = `${error.message}.\n\n${enums.err.ERRORS_OCCURRED}\n\n${error.errors.join('\n')}`;
|
||||
}
|
||||
// If just one error was returned.
|
||||
else {
|
||||
return await message.reply(error.message);
|
||||
console.error(error);
|
||||
errorsText = error.message;
|
||||
}
|
||||
})
|
||||
}
|
||||
if (errorsText.length > 2000) {
|
||||
const returnedBuffer = messageHelper.returnBufferFromText(errorsText);
|
||||
await message.reply({
|
||||
content: returnedBuffer.text, files: [{name: 'text.txt', data: returnedBuffer.file}]
|
||||
})
|
||||
} else {
|
||||
await message.reply(errorsText)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const commands = cmds;
|
||||
Reference in New Issue
Block a user