diff --git a/.gitignore b/.gitignore index eb5f3f0..9ce8048 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules yarn.lock src/config.json package-lock.json -htmlEmail_templates \ No newline at end of file +htmlEmail_templates +yarn-error.log \ No newline at end of file diff --git a/package.json b/package.json index 0fb9ffc..707e959 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "eris-pagination": "bsian03/eris-pagination", "fs-extra": "^8.1.0", "moment": "^2.24.0", + "moment-precise-range-plugin": "^1.3.0", "mongoose": "^5.7.4", "nodemailer": "^6.3.1", "signale": "^1.4.0", diff --git a/src/class/Collection.ts b/src/class/Collection.ts index 9f810dc..71e8a07 100644 --- a/src/class/Collection.ts +++ b/src/class/Collection.ts @@ -78,12 +78,12 @@ export default class Collection extends Map { /** * Return an array with the results of applying the given function to each element - * @param func A function that takes an object and returns something + * @param callbackfn A function that takes an object and returns something */ - map(func: Function) { + map(callbackfn: (value?: V, index?: number, array?: V[]) => U): U[] { const arr = []; for (const item of this.values()) { - arr.push(func(item)); + arr.push(callbackfn(item)); } return arr; } @@ -102,22 +102,6 @@ export default class Collection extends Map { return arr; } - /** - * Reduce values by function - * @param callbackFn Function to execute on each element in the array - * @param initialValue Value to use as the first argument to the first call of the callback - * @returns Accumulator - */ - reduce(func: Function, initialValue = 0) { - const iter = this.values(); - let val: any; - let result = initialValue === undefined ? iter.next().value : initialValue; - while ((val = iter.next().value) !== undefined) { // eslint-disable-line - result = func(result, val); - } - return result; - } - /** * Test if at least one element passes the test implemented by the provided function. Returns true if yes, or false if not. * @param func A function that takes an object and returns true if it matches @@ -131,20 +115,6 @@ export default class Collection extends Map { return false; } - /** - * Test if all elements passe the test implemented by the provided function. Returns true if yes, or false if not. - * @param func A function that takes an object and returns true if it matches - * @returns An array containing booleans that matched - */ - every(func: Function): boolean[] { - const array = []; - for (const item of this.values()) { - if (func(item)) array.push(true); - else array.push(false); - } - return array; - } - /** * Update an object * @param key The key of the object diff --git a/src/class/Util.ts b/src/class/Util.ts index 0f0bedb..b1b9b99 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -55,7 +55,7 @@ export default class Util { } if (!resolvedCommand) return Promise.resolve({ cmd: null, args }); - let parentLabel = `${command}`; + let parentLabel = ''; let hasSubCommands = true; while (hasSubCommands) { if (!resolvedCommand.subcommands.size) { @@ -63,13 +63,15 @@ export default class Util { } else if (!args[0]) { hasSubCommands = false; break; } else if (resolvedCommand.subcommands.has(args[0])) { - resolvedCommand = resolvedCommand.subcommands.get(args[0]); - parentLabel += ` ${args[0]}`; args.shift(); + parentLabel += `${resolvedCommand.name} `; + resolvedCommand = resolvedCommand.subcommands.get(args[0]); args.shift(); } else { - for (const subCmd of resolvedCommand.subcommands.toArray()) { + const subcommandArray = resolvedCommand.subcommands.toArray(); + for (const subCmd of subcommandArray) { if (subCmd.aliases.includes(args[0])) { - resolvedCommand = subCmd; parentLabel += ` ${args[0]}`; args.shift(); break; - } else { + parentLabel += `${resolvedCommand.name} `; resolvedCommand = subCmd; args.shift(); break; + } + if (subcommandArray.findIndex((v) => v === subCmd) === subcommandArray.length - 1) { hasSubCommands = false; break; } } @@ -173,8 +175,12 @@ export default class Util { } public async deleteAccount(username: string): Promise { + const account = await this.client.db.Account.findOne({ username }); + if (!account) return Promise.reject(new Error('Account not found')); await this.exec(`deluser ${username} --remove-home --backup-to /management/Archives && rm -rf -R /home/${username}`); + await this.client.removeGuildMemberRole('446067825673633794', account.userID, '546457886440685578', 'Cloud Account Deleted'); await this.client.db.Account.deleteOne({ username }); + return Promise.resolve(); } public async messageCollector(message: Message, question: string, timeout: number, shouldDelete = false, choices: string[] = null, filter = (msg: Message): boolean|void => {}): Promise { diff --git a/src/commands/cwg.ts b/src/commands/cwg.ts index 9595950..be3e69b 100644 --- a/src/commands/cwg.ts +++ b/src/commands/cwg.ts @@ -16,7 +16,7 @@ export default class CWG extends Command { super(client); this.name = 'cwg'; this.description = 'Manages aspects for the CWG.'; - this.usage = `${this.client.config.prefix}cwg create [User ID | Username] [Domain] [Port] \n${this.client.config.prefix}cwg data [Domain/Port]`; + this.usage = `Run ${this.client.config.prefix}${this.name} [subcommand] for usage information`; this.permissions = { roles: ['446104438969466890'] }; this.subcmds = [Create, Data, Delete]; this.enabled = true; diff --git a/src/commands/cwg_create.ts b/src/commands/cwg_create.ts index 11ebfd4..0dd4b7e 100644 --- a/src/commands/cwg_create.ts +++ b/src/commands/cwg_create.ts @@ -33,7 +33,18 @@ export default class CWG_Create extends Command { if (args[3] && !args[4]) return edit.edit(`${this.client.stores.emojis.error} x509 Certificate key required`); let certs: { cert?: string, key?: string }; if (args[4]) certs = { cert: args[3], key: args[4] }; if (await this.client.db.Domain.exists({ domain: args[1] })) return edit.edit(`***${this.client.stores.emojis.error} This domain already exists.***`); - if (await this.client.db.Domain.exists({ port: Number(args[2]) })) return message.channel.createMessage('***WARNING: This port is already binded to another domain.***'); + if (await this.client.db.Domain.exists({ port: Number(args[2]) })) { + // await edit.edit(`***${this.client.stores.emojis.error} This port is already binded to a domain. Do you wish to continue? (y/n)***`); + let answer: Message; + try { + answer = await this.client.util.messageCollector(message, + `***${this.client.stores.emojis.error} This port is already binded to a domain. Do you wish to continue? (y/n)***`, + 30000, true, ['y', 'n'], (msg) => msg.author.id === message.author.id && msg.channel.id === message.channel.id); + } catch (error) { + return edit.edit(`***${this.client.stores.emojis.error} Bind request cancelled***`); + } + if (answer.content === 'n') return edit.edit(`***${this.client.stores.emojis.error} Bind request cancelled***`); + } const domain = await this.createDomain(account, args[1], Number(args[2]), certs); const embed = new RichEmbed(); embed.setTitle('Domain Creation'); @@ -98,7 +109,6 @@ export default class CWG_Create extends Command { public async createDomain(account: AccountInterface, domain: string, port: number, x509Certificate: { cert?: string, key?: string } = { cert: '/etc/nginx/ssl/cloud-org.chain.crt', key: '/etc/nginx/ssl/cloud-org.key.pem' }) { try { if (port <= 1024 || port >= 65535) throw new RangeError(`Port range must be between 1024 and 65535, received ${port}.`); - // if (await this.client.db.Domain.exists({ port })) throw new Error(`Port ${port} already exists in the database.`); if (await this.client.db.Domain.exists({ domain })) throw new Error(`Domain ${domain} already exists in the database.`); if (!await this.client.db.Account.exists({ userID: account.userID })) throw new Error(`Cannot find account ${account.userID}.`); await fs.access(x509Certificate.cert, fs.constants.R_OK); diff --git a/src/commands/cwg_data.ts b/src/commands/cwg_data.ts index bb8f001..36f3801 100644 --- a/src/commands/cwg_data.ts +++ b/src/commands/cwg_data.ts @@ -1,7 +1,9 @@ -import fs from 'fs-extra'; +import fs from 'fs'; import moment from 'moment'; import x509 from '@ghaiklor/x509'; +import { createPaginationEmbed } from 'eris-pagination'; import { Message } from 'eris'; +import { promisify } from 'util'; import { Command, RichEmbed } from '../class'; import { Client } from '..'; @@ -18,21 +20,28 @@ export default class CWG_Data extends Command { public async run(message: Message, args: string[]) { try { if (!args[0]) return this.client.commands.get('help').run(message, ['cwg', this.name]); - const domain = await this.client.db.Domain.findOne({ $or: [{ domain: args[0] }, { port: Number(args[0]) || '' }] }); - if (!domain) return message.channel.createMessage(`***${this.client.stores.emojis.error} The domain or port you provided could not be found.***`); - const embed = new RichEmbed(); - embed.setTitle('Domain Information'); - embed.addField('Account Username', domain.account.username, true); - embed.addField('Account ID', domain.account.userID, true); - embed.addField('Domain', domain.domain, true); - embed.addField('Port', String(domain.port), true); - embed.addField('Certificate Issuer', x509.getIssuer(await fs.readFile(domain.x509.cert, { encoding: 'utf8' })).organizationName, true); - embed.addField('Certificate Subject', x509.getSubject(await fs.readFile(domain.x509.cert, { encoding: 'utf8' })).commonName, true); - embed.addField('Certificate Expiration Date', moment(x509.parseCert(await fs.readFile(domain.x509.cert, { encoding: 'utf8' })).notAfter).format('dddd, MMMM Do YYYY, h:mm:ss A'), true); - embed.setFooter(this.client.user.username, this.client.user.avatarURL); - embed.setTimestamp(); + const dom = await this.client.db.Domain.find({ $or: [{ domain: args[0] }, { port: Number(args[0]) || '' }] }); + if (!dom.length) return message.channel.createMessage(`***${this.client.stores.emojis.error} The domain or port you provided could not be found.***`); + // const embeds: RichEmbed[] = []; + const embeds = dom.map((domain) => { + const cert = fs.readFileSync(domain.x509.cert, { encoding: 'utf8' }); + const embed = new RichEmbed(); + embed.setTitle('Domain Information'); + embed.addField('Account Username', domain.account.username, true); + embed.addField('Account ID', domain.account.userID, true); + embed.addField('Domain', domain.domain, true); + embed.addField('Port', String(domain.port), true); + embed.addField('Certificate Issuer', x509.getIssuer(cert).organizationName, true); + embed.addField('Certificate Subject', x509.getSubject(cert).commonName, true); + embed.addField('Certificate Expiration Date', moment(x509.parseCert(cert).notAfter).format('dddd, MMMM Do YYYY, h:mm:ss A'), true); + embed.setFooter(this.client.user.username, this.client.user.avatarURL); + embed.setTimestamp(); + return embed; + }); + this.client.signale.log(embeds); // @ts-ignore - return message.channel.createMessage({ embed }); + if (embeds.length === 1) return message.channel.createMessage({ embed: embeds[0] }); + return createPaginationEmbed(message, this.client, embeds, {}); } catch (error) { return this.client.util.handleError(error, message, this); } diff --git a/src/commands/cwg_delete.ts b/src/commands/cwg_delete.ts index aa51456..3998fab 100644 --- a/src/commands/cwg_delete.ts +++ b/src/commands/cwg_delete.ts @@ -4,7 +4,7 @@ import { Message } from 'eris'; import { Command, RichEmbed } from '../class'; import { Client } from '..'; -export default class CWG_Data extends Command { +export default class CWG_Delete extends Command { constructor(client: Client) { super(client); this.name = 'delete'; diff --git a/src/commands/disk.ts b/src/commands/disk.ts new file mode 100644 index 0000000..0dd9afd --- /dev/null +++ b/src/commands/disk.ts @@ -0,0 +1,44 @@ +import { Message } from 'eris'; +import moment from 'moment'; +import { Client } from '..'; +import { RichEmbed, Command } from '../class'; +import { dataConversion } from '../functions'; +// eslint-disable-next-line import/no-unresolved +import 'moment-precise-range-plugin'; + +export default class Disk extends Command { + constructor(client: Client) { + super(client); + this.name = 'disk'; + this.description = 'Checks the used disk space by a user'; + this.usage = `${this.client.config.prefix}disk [Username/User ID/Email]`; + this.permissions = { roles: ['446104438969466890'] }; + this.enabled = true; + } + + async run(message: Message, args: string[]) { + try { + const account = await this.client.db.Account.findOne({ $or: [{ username: args[0] }, { userID: args[0] }, { emailAddress: args[0] }] }); + if (!account) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not found***`); + if (account.root || args[0].includes('./')) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Permission denied***`); + const diskReply = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Fetching total disk size may up to 10 minutes. This message will edit when the disk size has been located.***`); + const start = Date.now(); + const result = await this.client.util.exec(`du -s /home/${account.username}`); + const end = Date.now(); + // @ts-ignore + const totalTime: string = moment.preciseDiff(start, end); + const embed = new RichEmbed(); + embed.setTitle('Disk Usage'); + embed.setColor('ff0000'); + embed.setDescription(`/home/${account.username}`); + embed.addField('Result', dataConversion(Number(result)), true); + embed.addField('Time taken', totalTime, true); + embed.setFooter(`Requested by ${message.author.username}#${message.author.discriminator}`, message.author.avatarURL); + embed.setTimestamp(); + // @ts-ignore + return diskReply.edit({ content: '', embed }); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +} diff --git a/src/commands/help.ts b/src/commands/help.ts index d44954f..99804fb 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -19,7 +19,7 @@ export default class Help extends Command { if (!args[0]) { const cmdList: Command[] = []; this.client.commands.forEach((c) => cmdList.push(c)); - const commands = this.client.commands.map((c: Command) => { + const commands = this.client.commands.map((c) => { const aliases = c.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', '); const perms: string[] = []; let allowedRoles = c.permissions && c.permissions.roles && c.permissions.roles.map((r) => `<@&${r}>`).join(', '); @@ -51,12 +51,13 @@ export default class Help extends Command { if (allowedRoles) { allowedRoles = `**Roles:** ${allowedRoles}`; perms.push(allowedRoles); } let allowedUsers = cmd.permissions && cmd.permissions.users && cmd.permissions.users.map((u) => `<@${u}>`).join(', '); if (allowedUsers) { allowedUsers = `**Users:** ${allowedUsers}`; perms.push(allowedUsers); } - const displayedPerms = perms.length ? `**Permissions:**\n${perms.join('\n')}` : ''; - const aliases = cmd.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', '); + const displayedPerms = perms.length ? `\n**Permissions:**\n${perms.join('\n')}` : ''; + const aliases = cmd.aliases.length ? `\n**Aliases:** ${cmd.aliases.map((alias) => `${this.client.config.prefix}${cmd.parentName}${alias}`).join(', ')}` : ''; + const subcommands = cmd.subcommands.size ? `\n**Subcommands:** ${cmd.subcommands.map((s) => s.parentName || `${cmd.name} ${s.name}`).join(', ')}` : ''; const embed = new RichEmbed(); embed.setTimestamp(); embed.setFooter(`Requested by ${message.author.username}#${message.author.discriminator}`, message.author.avatarURL); - embed.setTitle(`${this.client.config.prefix}${cmd.parentName}`); embed.setAuthor(`${this.client.user.username}#${this.client.user.discriminator}`, this.client.user.avatarURL); - const description = `**Description**: ${cmd.description}\n**Usage:** ${cmd.usage}\n**Aliases:** ${aliases}\n${displayedPerms}`; + embed.setTitle(`${this.client.config.prefix}${cmd.parentName}${cmd.name}`); embed.setAuthor(`${this.client.user.username}#${this.client.user.discriminator}`, this.client.user.avatarURL); + const description = `**Description**: ${cmd.description}\n**Usage:** ${cmd.usage}${aliases}${displayedPerms}${subcommands}`; embed.setDescription(description); // @ts-ignore message.channel.createMessage({ embed }); diff --git a/src/commands/index.ts b/src/commands/index.ts index f370c62..80123f3 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -2,6 +2,7 @@ export { default as Announce } from './announce'; export { default as CreateAccount } from './createaccount'; export { default as CWG } from './cwg'; export { default as DeleteAccount } from './deleteaccount'; +export { default as Disk } from './disk'; export { default as Eval } from './eval'; export { default as Exec } from './exec'; export { default as Help } from './help'; diff --git a/src/commands/pull.ts b/src/commands/pull.ts index 4686365..569f940 100644 --- a/src/commands/pull.ts +++ b/src/commands/pull.ts @@ -14,27 +14,64 @@ export default class Pull extends Command { public async run(message: Message) { try { - const updateMessage = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Fetching latest commit...***`); + const updateMessage = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Fetching latest commit...***\n\`\`\`sh\ngit pull\n\`\`\``); let pull: string; try { pull = await this.client.util.exec('git pull'); } catch (error) { - return updateMessage.edit(`${this.client.stores.emojis.error} ***Could not fetch latest commit***\n\`\`\`sh\n${error.message}\n\`\`\``); - } - if (pull.includes('Already up to date')) return updateMessage.edit(`${this.client.stores.emojis.success} ***No updates available***`); - if (!pull.includes('origin/master')) return updateMessage.edit(`${this.client.stores.emojis.error} ***Unexpected output:***\n\`\`\`sh\n${pull}\n\`\`\``); - - const passedPull = await updateMessage.edit(`${this.client.stores.emojis.success} ***Pulled latest commit***\n${this.client.stores.emojis.loading} ***Rebuilding files...***\n\`\`\`sh\n${pull}\n\`\`\``); - try { - await this.client.util.exec('cd ../ && tsc -p ./tsconfig.json'); - } catch (error) { - const updatedMessage = passedPull.content.replace(`${this.client.stores.emojis.loading} ***Rebuilding files...***`, `${this.client.stores.emojis.error} ***Failed to rebuild files***`) + const updatedMessage = updateMessage.content.replace(`${this.client.stores.emojis.loading} ***Fetching latest commit...***`, `${this.client.stores.emojis.error} ***Could not fetch latest commit***`) .replace(/```$/, `${error.message}\n\`\`\``); return updateMessage.edit(updatedMessage); } + if (pull.includes('Already up to date')) { + const updatedMessage = updateMessage.content.replace(`${this.client.stores.emojis.loading} ***Fetching latest commit...***`, `${this.client.stores.emojis.success} ***No updates available***`) + .replace(/```$/, `${pull}\n\`\`\``); + return updateMessage.edit(updatedMessage); + } + if (!pull.includes('origin/master')) { + const updatedMessage = updateMessage.content.replace(`${this.client.stores.emojis.loading} ***Fetching latest commit...***`, `${this.client.stores.emojis.error} ***Unexpected git output***`) + .replace(/```$/, `${pull}\n\`\`\``); + return updateMessage.edit(updatedMessage); + } + const continueMessage = updateMessage.content.replace(`${this.client.stores.emojis.loading} ***Fetching latest commit...***`, `${this.client.stores.emojis.success} ***Pulled latest commit***\n${this.client.stores.emojis.loading} ***Reinstalling dependencies...***`) + .replace(/```$/, `${pull}\nyarn install\n\`\`\``); + const passedPull = await updateMessage.edit(continueMessage); - const finalMessage = passedPull.content.replace(`${this.client.stores.emojis.loading} ***Rebuilding files...***`, `${this.client.stores.emojis.success} ***Files rebuilt***`); + + let install: string; + try { + install = await this.client.util.exec('yarn install'); + } catch (error) { + const updatedMessage = passedPull.content.replace(`${this.client.stores.emojis.loading} ***Reinstalling dependencies...***`, `${this.client.stores.emojis.error} ***Failed to reinstall dependencies***`) + .replace(/```$/, `${error.message}\n\`\`\``); + return updateMessage.edit(updatedMessage); + } + let updatedPackages: Message; + if (install.includes('Already up-to-date')) { + const updatedMessage = passedPull.content.replace(`${this.client.stores.emojis.loading} ***Reinstalling dependencies...***`, `${this.client.stores.emojis.success} ***No dependency updates available***\n${this.client.stores.emojis.loading} ***Rebuilding files...***`) + .replace(/```$/, `${install}\nyarn run build\n\`\`\``); + updatedPackages = await updateMessage.edit(updatedMessage); + } else if (install.includes('success Saved lockfile.')) { + const updatedMessage = passedPull.content.replace(`${this.client.stores.emojis.loading} ***Reinstalling dependencies...***`, `${this.client.stores.emojis.success} ***Updated dependencies***\n${this.client.stores.emojis.loading} ***Rebuilding files...***`) + .replace(/```$/, `${install}\nyarn run build\n\`\`\``); + updatedPackages = await updateMessage.edit(updatedMessage); + } else { + const updatedMessage = passedPull.content.replace(`${this.client.stores.emojis.loading} ***Reinstalling dependencies...***`, `${this.client.stores.emojis.error} ***Unexpected yarn install output***`) + .replace(/```$/, `${pull}\n\`\`\``); + return updateMessage.edit(updatedMessage); + } + + let build: string; + try { + build = await this.client.util.exec('yarn run build'); + } catch (error) { + const updatedMessage = updatedPackages.content.replace(`${this.client.stores.emojis.loading} ***Rebuilding files...***`, `${this.client.stores.emojis.error} ***Failed to rebuild files***`) + .replace(/```$/, `${error.message}\n\`\`\``); + return updateMessage.edit(updatedMessage); + } + const finalMessage = updatedPackages.content.replace(`${this.client.stores.emojis.loading} ***Rebuilding files...***`, `${this.client.stores.emojis.success} ***Files rebuilt***`) + .replace(/```$/, `${build}\n\`\`\``); return updateMessage.edit(finalMessage); } catch (error) {