ConjureBot/src/commands/addcrowduser.ts
2020-09-01 18:50:20 -04:00

74 lines
2.4 KiB
TypeScript

import {Command} from "../Command";
import got from 'got';
const {userDict, crowd, groupDict} = require("../config.json");
const addcrowduser = new Command(
'addcrowduser',
async (message, args) => {
const email = args[0];
const firstName = args[1];
const lastName = args.splice(2).join(' ');
const firstNameLetter = firstName[0].toLowerCase();
const lastNameLowercase = lastName.toLowerCase().replace(/\s/g, '');;
const username = firstNameLetter + lastNameLowercase;
const password = lastNameLowercase + firstNameLetter;
try {
await got.post("https://conjure.etsmtl.ca/crowd/rest/usermanagement/1/user", {
responseType: 'json',
username: crowd.username,
password: crowd.password,
headers: {
Accept: 'application/json'
},
json: {
"name": username,
"password": {
"value": password
},
"active": true,
"email": email,
"first-name": firstName,
"last-name": lastName
}
});
} catch (error) {
console.error(error.response.body);
message.channel.send("Couldn't add the new member.");
return;
}
try {
await got.post("https://conjure.etsmtl.ca/crowd/rest/usermanagement/1/user/group/direct", {
responseType: 'json',
username: crowd.username,
password: crowd.password,
headers: {
Accept: 'application/json'
},
json: {
name: 'conjure-member'
},
searchParams: new URLSearchParams([
['username', username]
])
});
} catch (error) {
console.error(error.response.body);
message.channel.send("Couldn't add the user to conjure-member.");
return;
}
message.channel.send(`Member added.\nUsername: ${username}\nPassword: ${password}`);
}, {
description: "Adds a member to Crowd.",
permissions: {
users: [],
groups: [groupDict.admin]
},
args: ['email', 'first name', 'last name']
}
);
export default addcrowduser;