From 99a9a3911de7497e34b3df8850639728e1ab4930 Mon Sep 17 00:00:00 2001 From: Bsian Date: Sun, 27 Oct 2019 01:08:42 +0000 Subject: [PATCH] Created command --- src/Util.ts | 15 +++++++++ src/commands/createaccount.ts | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/commands/createaccount.ts diff --git a/src/Util.ts b/src/Util.ts index 47b4e64..bf34646 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -4,6 +4,7 @@ import childProcess from 'child_process'; import nodemailer from 'nodemailer'; import { Message, TextChannel, PrivateChannel } from 'eris'; import { outputFile } from 'fs-extra'; +import uuid from 'uuid/v4'; import { Client } from '.'; import { Command, RichEmbed } from './class'; @@ -112,4 +113,18 @@ export default class Util { tempPass += passChars[Math.floor(Math.random() * passChars.length)]; return tempPass; } + + public async createAccount(hash: string, etcPasswd: string, username: string, userID: string, emailAddress: string, moderatorID: string): Promise { + await this.exec(`useradd -m -p ${hash} -c ${etcPasswd} -s /bin/bash ${username}`); + await this.exec(`chage -d0 ${username}`); + + const log = await new this.client.db.Moderation({ + username, userID, logID: uuid(), moderatorID, reason: 'User requested account creation', type: 0, date: new Date(), + }); + const account = await new this.client.db.Account({ + username, userID, emailAddress, createdBy: moderatorID, createdAt: new Date(), locked: false, + }); + await log.save(); + await account.save(); + } } diff --git a/src/commands/createaccount.ts b/src/commands/createaccount.ts new file mode 100644 index 0000000..6461c42 --- /dev/null +++ b/src/commands/createaccount.ts @@ -0,0 +1,62 @@ +import { Message, PrivateChannel } from 'eris'; +import { Client, config } from '..'; +import { Command, RichEmbed } from '../class'; + +export default class CreateAccount extends Command { + constructor(client: Client) { + super(client); + this.name = 'createaccount'; + this.description = 'Create an account on the Cloud VM'; + this.usage = `${config.prefix}createaccount [User ID] [Email] [Account name]`; + this.aliases = ['createacc', 'cacc', 'caccount']; + this.permissions = { roles: ['475817826251440128', '525441307037007902'] }; + this.enabled = true; + } + + /* + args[0] is the user ID + args[1] is the email + args[2] is the username of the account to be created + */ + + public async run(message: Message, args: string[]) { + try { + if (message.channel instanceof PrivateChannel) return message; // Stop TS being gay + if (!args[2]) return this.client.commands.get('help').run(message, [this.name]); + if (!message.channel.guild.members.has(args[0])) return message.channel.createMessage(`${this.client.stores.emojis.error} ***User not found***`); + if (message.channel.guild.members.get(args[0]).bot) return message.channel.createMessage(`${this.client.stores.emojis.error} ***I cannot create accounts for bots***`); + const checkUser = await this.client.db.Account.findOne({ userID: args[0] }); + if (checkUser) return message.channel.createMessage(`${this.client.stores.emojis.error} ***<@${args[0]}> already has an account***`); + const checkEmail = await this.client.db.Account.findOne({ emailAddress: args[1] }); + if (checkEmail) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account already exists with this email address***`); + const checkAccount = await this.client.db.Account.findOne({ username: args[2] }); + if (checkAccount) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account already exists with this username***`); + + if (!this.client.util.isValidEmail(args[1])) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Invalid email address supplied***`); + if (!/^[a-z][-a-z0-9]*$/.test(args[2])) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Invalid username supplied***`); + + const confirmation = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Creating account...***`); + + const tempPass = this.client.util.randomPassword(); + let passHash = await this.client.util.createHash(tempPass); passHash = passHash.replace(/[$]/g, '\\$').replace('\n', ''); + const acctName = message.author.username.replace(/[!@#$%^&*(),.?":{}|<>]/g, '-').replace(/\s/g, '-'); + const etcPasswd = `${acctName},${args[0]},,`; + + await this.client.util.createAccount(passHash, etcPasswd, args[2], args[0], args[1], message.author.id); + + const embed = new RichEmbed(); + embed.setTitle('Cloud Account | Create'); + embed.setColor('00ff00'); + embed.addField('User', `${args[2]} | <@${args[0]}>`); + embed.addField('Engineer', `<@${message.author.id}>`, true); + embed.addField('Reason', 'User requested account creation'); + embed.setFooter(this.client.user.username, this.client.user.avatarURL); + embed.setTimestamp(); + // @ts-ignore + this.client.createMessage('580950455581147146', { embed }); + return confirmation.edit(`${this.client.stores.emojis.success} ***Account successfully created***\n**Username:** \`${args[2]}\`\n**Temporary Password:** \`${tempPass}\``); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +}