55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
|
|
import { ConsoleCommandData } from "../types/Command"
|
||
|
|
import bcrypt from 'bcrypt'
|
||
|
|
import { db } from "../util/database";
|
||
|
|
import Logger from "../util/Logger";
|
||
|
|
const cm = new Logger("CLIENT/register.js")
|
||
|
|
export const metadata: ConsoleCommandData = {
|
||
|
|
name: 'register',
|
||
|
|
description: "Creates an account.",
|
||
|
|
usage: "register <username> <password>",
|
||
|
|
aliases: ["reg"]
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function Main(args: Array<string>): Promise<any> {
|
||
|
|
if (args.length < 2)
|
||
|
|
return "Missing arguments. Run /help register for help with this command.";
|
||
|
|
|
||
|
|
const username = args[0]
|
||
|
|
const password = args[1]
|
||
|
|
const passwordHash = await bcrypt.hash(password, 12);
|
||
|
|
|
||
|
|
db.get(
|
||
|
|
`SELECT username FROM users WHERE username = ?`,
|
||
|
|
[username],
|
||
|
|
(err: any, row: any) => {
|
||
|
|
var output = ""
|
||
|
|
if (err) {
|
||
|
|
console.error(err);
|
||
|
|
return "Database error!"
|
||
|
|
}
|
||
|
|
|
||
|
|
if (row) {
|
||
|
|
output = `The username ${username} is already taken!`;
|
||
|
|
cm.log(output)
|
||
|
|
} else {
|
||
|
|
db.run(
|
||
|
|
`INSERT INTO users (username, password_hash) VALUES (?, ?)`,
|
||
|
|
[username, passwordHash],
|
||
|
|
(err: any) => {
|
||
|
|
if (err) {
|
||
|
|
console.error(err);
|
||
|
|
output = "Error creating account!";
|
||
|
|
} else {
|
||
|
|
output = "Account created.";
|
||
|
|
}
|
||
|
|
cm.log(output)
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return output
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|