1
0
Fork 0
cloudservices/src/commands/securesign_activatekey.ts

47 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-11-16 21:57:33 -05:00
import { Message } from 'eris';
import axios from 'axios';
import { Client } from '..';
2019-11-16 21:57:59 -05:00
import { Command } from '../class';
2019-11-16 21:57:33 -05:00
export default class SecureSign_ActivateKey extends Command {
constructor(client: Client) {
super(client);
this.name = 'activatekey';
this.description = 'Claims an Activation Key';
this.usage = `${this.client.config.prefix}securesign activatekey [key]`;
2020-03-28 04:58:14 -04:00
this.enabled = false;
2019-11-16 21:57:33 -05:00
}
public async run(message: Message, args: string[]) {
try {
if (!args[0]) return this.client.commands.get('help').run(message, ['securesign', this.name]);
const account = await this.client.db.Account.findOne({ userID: message.author.id });
if (!account) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not found***`);
if (!account.hash) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not initialized***`);
const msg = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Activating key...***`);
2019-12-23 12:08:42 -05:00
const hash = this.client.util.getAcctHash(account.homepath);
2019-11-16 21:57:33 -05:00
try {
await axios({
method: 'POST',
url: 'https://api.securesign.org/account/keys/activation',
2019-11-19 09:06:44 -05:00
headers: { Authorization: hash, 'Content-Type': 'application/json' },
2019-11-16 21:57:33 -05:00
data: JSON.stringify({ key: args[0] }),
});
} catch (error) {
const { code } = error.response.data;
if (code === 1001) {
2019-11-19 09:06:44 -05:00
await this.client.db.Account.updateOne({ userID: account.userID }, { $set: { hash: false } });
2019-11-16 21:57:33 -05:00
this.client.getDMChannel(account.userID).then((channel) => channel.createMessage('Your SecureSign password has been reset - please reinitialize your SecureSign account')).catch();
return msg.edit(`${this.client.stores.emojis.error} ***Authentication failed***`);
}
if (code === 1002) return msg.edit(`${this.client.stores.emojis.error} ***Invalid Activation Key***`);
if (code === 1003) return msg.edit(`${this.client.stores.emojis.error} ***${error.response.data.message}***`);
throw error;
}
return msg.edit(`${this.client.stores.emojis.success} ***Activation Key Accepted***`);
} catch (error) {
return this.client.util.handleError(error, message, this);
}
}
}