From 01e0cafb6ef9937dd030832873ea83d0e78db930 Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 10:51:45 +0000 Subject: [PATCH 01/23] Remove role on account delete --- src/class/Util.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/class/Util.ts b/src/class/Util.ts index 0f0bedb..bb87099 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -173,8 +173,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 { From 6e39a27dc534a3949445176f7a686d56e0550236 Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 12:11:25 +0000 Subject: [PATCH 02/23] Hopefully fix second+ subcommand aliases not working --- src/class/Util.ts | 2 -- src/commands/cwg_delete.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/class/Util.ts b/src/class/Util.ts index bb87099..6ae38eb 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -69,8 +69,6 @@ export default class Util { for (const subCmd of resolvedCommand.subcommands.toArray()) { if (subCmd.aliases.includes(args[0])) { resolvedCommand = subCmd; parentLabel += ` ${args[0]}`; args.shift(); break; - } else { - hasSubCommands = false; break; } } } 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'; From db7785c749404e5650e5dc3cb86cd4117e708e40 Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 15:00:00 +0000 Subject: [PATCH 03/23] Changed usage --- src/commands/cwg.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/cwg.ts b/src/commands/cwg.ts index 9595950..5ac60f9 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} ${this.client.commands.get(this.name).subcommands.map((s) => s.name).join(' / ')} for usage information`; this.permissions = { roles: ['446104438969466890'] }; this.subcmds = [Create, Data, Delete]; this.enabled = true; From da2435fbcc3e2dc48369c4e7a1ec70e05b591573 Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 15:00:25 +0000 Subject: [PATCH 04/23] Implement Array#map --- src/class/Collection.ts | 36 +++--------------------------------- src/class/Util.ts | 8 ++++---- src/commands/help.ts | 11 ++++++----- 3 files changed, 13 insertions(+), 42 deletions(-) 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 6ae38eb..279f6a9 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,12 +63,12 @@ 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()) { if (subCmd.aliases.includes(args[0])) { - resolvedCommand = subCmd; parentLabel += ` ${args[0]}`; args.shift(); break; + parentLabel += `${resolvedCommand.name} `; resolvedCommand = subCmd; args.shift(); break; } } } diff --git a/src/commands/help.ts b/src/commands/help.ts index d44954f..909f748 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 + 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 }); From 98cfa9f49ea25a76f960e526007a3c90e8169a7c Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 15:07:21 +0000 Subject: [PATCH 05/23] Stop failing to load command --- src/commands/cwg.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/cwg.ts b/src/commands/cwg.ts index 5ac60f9..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 = `Run ${this.client.config.prefix}${this.name} ${this.client.commands.get(this.name).subcommands.map((s) => s.name).join(' / ')} for usage information`; + 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; From 4e04e20f9fbf1b607dfc314d5a9114b92a2c3479 Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 15:13:21 +0000 Subject: [PATCH 06/23] Fixed subcommand list --- src/commands/help.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/help.ts b/src/commands/help.ts index 909f748..1ecace7 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -53,7 +53,7 @@ export default class Help extends Command { if (allowedUsers) { allowedUsers = `**Users:** ${allowedUsers}`; perms.push(allowedUsers); } 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 + s.name).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}${cmd.name}`); embed.setAuthor(`${this.client.user.username}#${this.client.user.discriminator}`, this.client.user.avatarURL); From bf7d86d5a580c391ac2cfa2f5f56e41cadb1ba6b Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 16:04:24 +0000 Subject: [PATCH 07/23] Fixed subcommand text --- src/commands/help.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/help.ts b/src/commands/help.ts index 1ecace7..99804fb 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -53,7 +53,7 @@ export default class Help extends Command { if (allowedUsers) { allowedUsers = `**Users:** ${allowedUsers}`; perms.push(allowedUsers); } 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 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}${cmd.name}`); embed.setAuthor(`${this.client.user.username}#${this.client.user.discriminator}`, this.client.user.avatarURL); From 0c4902fb61416203d4367186ca587d13885b602e Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 22:27:15 +0000 Subject: [PATCH 08/23] Added disk command --- package.json | 1 + src/commands/disk.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ src/commands/index.ts | 1 + 3 files changed, 45 insertions(+) create mode 100644 src/commands/disk.ts 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/commands/disk.ts b/src/commands/disk.ts new file mode 100644 index 0000000..3379c24 --- /dev/null +++ b/src/commands/disk.ts @@ -0,0 +1,43 @@ +import { Message } from 'eris'; +import moment from 'moment'; +import { Client } from '..'; +import { RichEmbed, Command } from '../class'; +import { dataConversion } from '../functions'; +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/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'; From d918cd30bd8aa2784700b38400cfa0404006d0ca Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 23:15:39 +0000 Subject: [PATCH 09/23] included extra stuff --- src/commands/pull.ts | 61 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 12 deletions(-) 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) { From 2c528c36c9dbdbe3323d5b29bb1a28ba9baa61d7 Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 23:26:19 +0000 Subject: [PATCH 10/23] Allowed multiple domains to use the same port --- src/commands/cwg_create.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/commands/cwg_create.ts b/src/commands/cwg_create.ts index 02a758f..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 edit.edit(`***${this.client.stores.emojis.error} This port is already binded to a 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); From e0a4c7121cfb1b76df97e12e8ec5a4011dd8d060 Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 23:29:28 +0000 Subject: [PATCH 11/23] Testing something --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 707e959..0fb9ffc 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ "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", From 720ff46640941cfad4cb8e5f434a78fd15c5e07b Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 23:34:41 +0000 Subject: [PATCH 12/23] nvm --- package.json | 1 + 1 file changed, 1 insertion(+) 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", From a5cf30652c725ec009e817d67374f5b41c1ec989 Mon Sep 17 00:00:00 2001 From: Bsian Date: Fri, 1 Nov 2019 23:53:21 +0000 Subject: [PATCH 13/23] Support multiple domains using a single port --- src/commands/cwg_data.ts | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/commands/cwg_data.ts b/src/commands/cwg_data.ts index bb8f001..5b884e9 100644 --- a/src/commands/cwg_data.ts +++ b/src/commands/cwg_data.ts @@ -1,6 +1,7 @@ import fs from 'fs-extra'; import moment from 'moment'; import x509 from '@ghaiklor/x509'; +import { createPaginationEmbed } from 'eris-pagination'; import { Message } from 'eris'; import { Command, RichEmbed } from '../class'; import { Client } from '..'; @@ -18,21 +19,25 @@ 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 = dom.map(async (domain) => { + 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(); + return embed; + }); // @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); } From 7c6d9e23cf6829ba6e936f8e3f12a19a8157f128 Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 2 Nov 2019 00:02:16 +0000 Subject: [PATCH 14/23] Attempted fix at embed not showing --- src/commands/cwg_data.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/commands/cwg_data.ts b/src/commands/cwg_data.ts index 5b884e9..c84f745 100644 --- a/src/commands/cwg_data.ts +++ b/src/commands/cwg_data.ts @@ -21,7 +21,8 @@ export default class CWG_Data extends Command { if (!args[0]) return this.client.commands.get('help').run(message, ['cwg', this.name]); 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 = dom.map(async (domain) => { + const embeds: RichEmbed[] = []; + dom.forEach(async (domain) => { const embed = new RichEmbed(); embed.setTitle('Domain Information'); embed.addField('Account Username', domain.account.username, true); @@ -33,7 +34,7 @@ export default class CWG_Data extends Command { 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(); - return embed; + return embeds.push(embed); }); // @ts-ignore if (embeds.length === 1) return message.channel.createMessage({ embed: embeds[0] }); From a2d267465c1565c96e1a8cb72c70145253530da7 Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 2 Nov 2019 00:06:14 +0000 Subject: [PATCH 15/23] Diagnosis --- src/commands/cwg_data.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/commands/cwg_data.ts b/src/commands/cwg_data.ts index c84f745..6c6156f 100644 --- a/src/commands/cwg_data.ts +++ b/src/commands/cwg_data.ts @@ -34,8 +34,10 @@ export default class CWG_Data extends Command { 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(); + this.client.signale.log(embed); return embeds.push(embed); }); + this.client.signale.log(embeds); // @ts-ignore if (embeds.length === 1) return message.channel.createMessage({ embed: embeds[0] }); return createPaginationEmbed(message, this.client, embeds, {}); From 1d687f6d691c0875f7c0376601810a09331dbe6c Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 2 Nov 2019 00:09:25 +0000 Subject: [PATCH 16/23] k --- src/commands/cwg_data.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/commands/cwg_data.ts b/src/commands/cwg_data.ts index 6c6156f..d3786cd 100644 --- a/src/commands/cwg_data.ts +++ b/src/commands/cwg_data.ts @@ -34,7 +34,6 @@ export default class CWG_Data extends Command { 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(); - this.client.signale.log(embed); return embeds.push(embed); }); this.client.signale.log(embeds); From bb0a371ccf246f24b02243302895c19fc4824fd6 Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 2 Nov 2019 00:10:16 +0000 Subject: [PATCH 17/23] Ignore yarn error log --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From ad1b70888e22605c210f7f0698e5500362308ada Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 2 Nov 2019 00:15:22 +0000 Subject: [PATCH 18/23] k --- src/commands/cwg_data.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/commands/cwg_data.ts b/src/commands/cwg_data.ts index d3786cd..76db3d2 100644 --- a/src/commands/cwg_data.ts +++ b/src/commands/cwg_data.ts @@ -22,19 +22,20 @@ export default class CWG_Data extends Command { 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[] = []; - dom.forEach(async (domain) => { + await dom.map(async (domain) => { + const cert = await fs.readFile(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(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.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 embeds.push(embed); + return embed; // s.push(embed); }); this.client.signale.log(embeds); // @ts-ignore From 9b85bea477e1149d124b3d84a04f45931261d19c Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 2 Nov 2019 00:19:09 +0000 Subject: [PATCH 19/23] k --- src/commands/cwg_data.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/cwg_data.ts b/src/commands/cwg_data.ts index 76db3d2..7725a0f 100644 --- a/src/commands/cwg_data.ts +++ b/src/commands/cwg_data.ts @@ -21,8 +21,8 @@ export default class CWG_Data extends Command { if (!args[0]) return this.client.commands.get('help').run(message, ['cwg', this.name]); 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[] = []; - await dom.map(async (domain) => { + // const embeds: RichEmbed[] = []; + const embeds = await dom.map(async (domain) => { const cert = await fs.readFile(domain.x509.cert, { encoding: 'utf8' }); const embed = new RichEmbed(); embed.setTitle('Domain Information'); From c7e036062f09b6df282001505eb5089e9552c80c Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 2 Nov 2019 00:32:58 +0000 Subject: [PATCH 20/23] Hopefully stop memory leak --- src/class/Util.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/class/Util.ts b/src/class/Util.ts index 279f6a9..b1b9b99 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -66,10 +66,14 @@ export default class Util { 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])) { parentLabel += `${resolvedCommand.name} `; resolvedCommand = subCmd; args.shift(); break; } + if (subcommandArray.findIndex((v) => v === subCmd) === subcommandArray.length - 1) { + hasSubCommands = false; break; + } } } } From 41914db777c8ae758213c7a4eef5e633b6029f5b Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 2 Nov 2019 00:41:15 +0000 Subject: [PATCH 21/23] Fix? --- src/commands/cwg_data.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/commands/cwg_data.ts b/src/commands/cwg_data.ts index 7725a0f..91fb549 100644 --- a/src/commands/cwg_data.ts +++ b/src/commands/cwg_data.ts @@ -21,9 +21,9 @@ export default class CWG_Data extends Command { if (!args[0]) return this.client.commands.get('help').run(message, ['cwg', this.name]); 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 = await dom.map(async (domain) => { - const cert = await fs.readFile(domain.x509.cert, { encoding: 'utf8' }); + const embeds: RichEmbed[] = []; + dom.map(async (domain) => { + const cert = await fs.readFile(domain.x509.cert, { encoding: 'utf8' }).then((a) => a); const embed = new RichEmbed(); embed.setTitle('Domain Information'); embed.addField('Account Username', domain.account.username, true); @@ -36,7 +36,7 @@ export default class CWG_Data extends Command { embed.setFooter(this.client.user.username, this.client.user.avatarURL); embed.setTimestamp(); return embed; // s.push(embed); - }); + }).forEach(async (e) => embeds.push(await e)); this.client.signale.log(embeds); // @ts-ignore if (embeds.length === 1) return message.channel.createMessage({ embed: embeds[0] }); From 16cfd9062ef25da956c54cd11fe6f456a98cb60a Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 2 Nov 2019 00:53:34 +0000 Subject: [PATCH 22/23] Fix? --- src/commands/cwg_data.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/commands/cwg_data.ts b/src/commands/cwg_data.ts index 91fb549..b519cc2 100644 --- a/src/commands/cwg_data.ts +++ b/src/commands/cwg_data.ts @@ -1,8 +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 '..'; @@ -21,9 +22,9 @@ export default class CWG_Data extends Command { if (!args[0]) return this.client.commands.get('help').run(message, ['cwg', this.name]); 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[] = []; - dom.map(async (domain) => { - const cert = await fs.readFile(domain.x509.cert, { encoding: 'utf8' }).then((a) => a); + // const embeds: RichEmbed[] = []; + const embeds = dom.map(async (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); @@ -35,8 +36,8 @@ export default class CWG_Data extends Command { 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; // s.push(embed); - }).forEach(async (e) => embeds.push(await e)); + return embed; + }); this.client.signale.log(embeds); // @ts-ignore if (embeds.length === 1) return message.channel.createMessage({ embed: embeds[0] }); From 1a59bdcc77e2713ad15f375e9ffd01c987c0f050 Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 2 Nov 2019 00:55:46 +0000 Subject: [PATCH 23/23] last try --- src/commands/cwg_data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/cwg_data.ts b/src/commands/cwg_data.ts index b519cc2..36f3801 100644 --- a/src/commands/cwg_data.ts +++ b/src/commands/cwg_data.ts @@ -23,7 +23,7 @@ export default class CWG_Data extends Command { 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(async (domain) => { + const embeds = dom.map((domain) => { const cert = fs.readFileSync(domain.x509.cert, { encoding: 'utf8' }); const embed = new RichEmbed(); embed.setTitle('Domain Information');