Added Permission interface + Tweaked addcrowduser

This commit is contained in:
Jason Durand 2020-09-01 18:50:20 -04:00
parent 2e2d49fff1
commit 37e2631ded
5 changed files with 44 additions and 15 deletions

View File

@ -37,6 +37,11 @@ export class CommandCollection {
} }
} }
interface Permissions {
users: string[];
groups: string[];
}
export class Command { export class Command {
name : string; name : string;
description? : string; description? : string;
@ -45,7 +50,7 @@ export class Command {
cooldown? : number; cooldown? : number;
aliases? : string[]; aliases? : string[];
usage? : string; usage? : string;
permissions? : string[]; permissions? : Permissions;
constructor( constructor(
name : string, name : string,
@ -57,7 +62,7 @@ export class Command {
cooldown = 0, cooldown = 0,
aliases = null as string[], aliases = null as string[],
usage = null as string, usage = null as string,
permissions = null as string[], permissions = null as Permissions,
} }
) { ) {
this.name = name.toLowerCase(); this.name = name.toLowerCase();
@ -73,4 +78,17 @@ export class Command {
checkName(commandName : string) { checkName(commandName : string) {
return commandName === this.name || (this.aliases && this.aliases.includes(commandName)); return commandName === this.name || (this.aliases && this.aliases.includes(commandName));
} }
checkPermission(userId : string) : boolean {
if (!this.permissions) return true;
if (this.permissions.users.includes(userId))
return true;
for (const group of this.permissions.groups)
if (group.includes(userId))
return true;
return false;
}
} }

View File

@ -1,17 +1,19 @@
import {Command} from "../Command"; import {Command} from "../Command";
import got from 'got'; import got from 'got';
const {userDict, crowd} = require("../config.json"); const {userDict, crowd, groupDict} = require("../config.json");
const addcrowduser = new Command( const addcrowduser = new Command(
'addcrowduser', 'addcrowduser',
async (message, args) => { async (message, args) => {
const email = args[0]; const email = args[0];
const firstNameLetter = args[1][0].toLowerCase(); const firstName = args[1];
const lastName = args[2].toLowerCase(); const lastName = args.splice(2).join(' ');
const firstNameLetter = firstName[0].toLowerCase();
const lastNameLowercase = lastName.toLowerCase().replace(/\s/g, '');;
const username = firstNameLetter + lastName; const username = firstNameLetter + lastNameLowercase;
const password = lastName + firstNameLetter; const password = lastNameLowercase + firstNameLetter;
try { try {
await got.post("https://conjure.etsmtl.ca/crowd/rest/usermanagement/1/user", { await got.post("https://conjure.etsmtl.ca/crowd/rest/usermanagement/1/user", {
@ -27,9 +29,9 @@ const addcrowduser = new Command(
"value": password "value": password
}, },
"active": true, "active": true,
"email": args[0], "email": email,
"first-name": args[1], "first-name": firstName,
"last-name": args[2] "last-name": lastName
} }
}); });
} catch (error) { } catch (error) {
@ -62,7 +64,10 @@ const addcrowduser = new Command(
message.channel.send(`Member added.\nUsername: ${username}\nPassword: ${password}`); message.channel.send(`Member added.\nUsername: ${username}\nPassword: ${password}`);
}, { }, {
description: "Adds a member to Crowd.", description: "Adds a member to Crowd.",
permissions: [userDict.misabiko, userDict.massimo], permissions: {
users: [],
groups: [groupDict.admin]
},
args: ['email', 'first name', 'last name'] args: ['email', 'first name', 'last name']
} }
); );

View File

@ -2,7 +2,7 @@ import {Command} from "../Command";
import { TextChannel } from "discord.js"; import { TextChannel } from "discord.js";
import * as mysql from 'mysql'; import * as mysql from 'mysql';
const {userDict, channels, mysql: mysqlCreds} = require("../config.json"); const {userDict, channels, mysql: mysqlCreds, groupDict} = require("../config.json");
const listdiscordusers = new Command( const listdiscordusers = new Command(
'listdiscordusers', 'listdiscordusers',
@ -42,7 +42,10 @@ const listdiscordusers = new Command(
} }
}, { }, {
description: "Lists discord missing from database.", description: "Lists discord missing from database.",
permissions: [userDict.misabiko, userDict.massimo] permissions: {
users: [],
groups: [groupDict.admin]
}
} }
); );
export default listdiscordusers; export default listdiscordusers;

View File

@ -17,7 +17,10 @@ const reloadcommand = new Command(
description: "Reloads either every, or a given command module.", description: "Reloads either every, or a given command module.",
aliases: ["reload"], aliases: ["reload"],
usage: "[commandName]", usage: "[commandName]",
permissions: [userDict.misabiko] permissions: {
users: [userDict.misabiko],
groups: []
}
} }
); );
export default reloadcommand; export default reloadcommand;

View File

@ -53,7 +53,7 @@ export class CustomClient extends Discord.Client {
if (command.guildOnly && message.channel.type !== 'text') if (command.guildOnly && message.channel.type !== 'text')
return message.reply(`You can only call the ${command} commmand on a server.`); return message.reply(`You can only call the ${command} commmand on a server.`);
if (command.permissions && !command.permissions.includes(message.author.id)) if (!command.checkPermission(message.author.id))
return message.reply(`You don't have the permission for that command, ask <@${userDict.misabiko}> for help.`); return message.reply(`You don't have the permission for that command, ask <@${userDict.misabiko}> for help.`);
if (!this.cooldowns[command.name]) if (!this.cooldowns[command.name])