forked from PluralFlux/PluralFlux
110 lines
3.9 KiB
JavaScript
110 lines
3.9 KiB
JavaScript
import {messageHelper} from "./helpers/messageHelper.js";
|
|
import {enums} from "./enums.js";
|
|
import {memberHelper} from "./helpers/memberHelper.js";
|
|
import {EmbedBuilder} from "@fluxerjs/core";
|
|
import {importHelper} from "./helpers/importHelper.js";
|
|
|
|
const cmds = {
|
|
commandsMap: new Map(),
|
|
aliasesMap: new Map()
|
|
};
|
|
|
|
cmds.aliasesMap.set('m', {command: 'member'})
|
|
|
|
cmds.commandsMap.set('member', {
|
|
description: enums.help.SHORT_DESC_MEMBER,
|
|
async execute(message, args) {
|
|
await cmds.memberCommand(message, args)
|
|
}
|
|
})
|
|
|
|
/**
|
|
* Calls the member-related functions.
|
|
*
|
|
* @async
|
|
* @param {Message} message - The message object
|
|
* @param {string[]} args - The parsed arguments
|
|
*
|
|
**/
|
|
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);});
|
|
|
|
if (typeof reply === 'string') {
|
|
return await message.reply(reply);
|
|
}
|
|
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 ? "\nThese errors occurred:\n" + errorsText : ""}`, embeds: [reply.embed]})
|
|
}
|
|
|
|
}
|
|
|
|
|
|
cmds.commandsMap.set('help', {
|
|
description: enums.help.SHORT_DESC_HELP,
|
|
async execute(message) {
|
|
const fields = [...cmds.entries()].map(([name, cmd]) => ({
|
|
name: `${messageHelper.prefix}${name}`,
|
|
value: cmd.description,
|
|
inline: true,
|
|
}));
|
|
|
|
const embed = new EmbedBuilder()
|
|
.setTitle('Commands')
|
|
.setDescription(enums.help.PLURALFLUX)
|
|
.addFields(...fields)
|
|
.setFooter({ text: `Prefix: ${messageHelper.prefix}` })
|
|
.setTimestamp();
|
|
|
|
await message.reply({ embeds: [embed.toJSON()] });
|
|
},
|
|
})
|
|
|
|
cmds.commandsMap.set('import', {
|
|
description: enums.help.SHORT_DESC_IMPORT,
|
|
async execute(message, args) {
|
|
await cmds.importCommand(message, args);
|
|
}
|
|
})
|
|
|
|
/**
|
|
* Calls the import-related functions.
|
|
*
|
|
* @async
|
|
* @param {Message} message - The message object
|
|
* @param {string[]} args - The parsed arguments
|
|
*
|
|
**/
|
|
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 ) {
|
|
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) => {
|
|
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\nThese 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.pdf', data: returnedBuffer.file }]
|
|
})
|
|
});
|
|
}
|
|
// If just one error was returned.
|
|
else {
|
|
return await message.reply(error.message);
|
|
}
|
|
})
|
|
}
|
|
|
|
export const commands = cmds; |