From 692b4af059dfc72d621bcc56556f42d6ec3bb275 Mon Sep 17 00:00:00 2001 From: Bsian Date: Mon, 23 Dec 2019 23:10:04 +0000 Subject: [PATCH] Added password reset command --- src/commands/index.ts | 1 + src/commands/resetpassword.ts | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/commands/resetpassword.ts diff --git a/src/commands/index.ts b/src/commands/index.ts index 7312475..f8e4bf7 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -15,6 +15,7 @@ export { default as parse } from './parse'; export { default as parseall } from './parseall'; export { default as ping } from './ping'; export { default as pull } from './pull'; +export { default as resetpassword } from './resetpassword'; export { default as restart } from './restart'; export { default as securesign } from './securesign'; export { default as sysinfo } from './sysinfo'; diff --git a/src/commands/resetpassword.ts b/src/commands/resetpassword.ts new file mode 100644 index 0000000..0754a5c --- /dev/null +++ b/src/commands/resetpassword.ts @@ -0,0 +1,42 @@ +import { Message } from 'eris'; +import { Client } from '..'; +import { Command } from '../class'; + +export default class ResetPassword extends Command { + constructor(client: Client) { + super(client); + + this.name = 'resetpassword'; + this.description = 'Reset a cloud account password'; + this.aliases = ['resetpasswd', 'resetpw']; + this.usage = `${this.client.config.prefix}resetpassword [Username | User ID | Email]`; + this.permissions = { roles: ['455972169449734144', '643619219988152321'] }; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[0]) return this.client.commands.get('help').run(message, [this.name]); + 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***`); + + const msg = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Resetting password for ${account.username}...***`); + const tempPass = this.client.util.randomPassword(); + await this.client.util.exec(`echo '${account.username}:${tempPass}' | sudo chpasswd`); + + let completeMessage = `${this.client.stores.emojis.success} ***Password for ${account.userID} reset to \`${tempPass}\`***`; + const dmChannel = await this.client.getDMChannel(account.userID); + try { + await dmChannel.createMessage(`We received a password reset request from you, your new password is \`${tempPass}\`.\n` + + `You will be asked to change your password when you log back in, \`(current) UNIX password\` is \`${tempPass}\`, then create a password that is at least 12 characters long, with at least one number, special character, and an uppercase letter.\n` + + 'Bear in mind that when you enter your password, it will be blank, so be careful not to type in your password incorrectly.'); + } catch (error) { + if (error.code === 50007) completeMessage += '\n*Unable to DM user*'; + throw error; + } + return msg.edit(completeMessage); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +}