From 37e2631dedaa02e2c51533eeb7c58b667237d581 Mon Sep 17 00:00:00 2001 From: Jason Durand Date: Tue, 1 Sep 2020 18:50:20 -0400 Subject: [PATCH] Added Permission interface + Tweaked addcrowduser --- src/Command.ts | 22 ++++++++++++++++++++-- src/commands/addcrowduser.ts | 23 ++++++++++++++--------- src/commands/listdiscordusers.ts | 7 +++++-- src/commands/reloadcommand.ts | 5 ++++- src/index.ts | 2 +- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/Command.ts b/src/Command.ts index b75f1cf..782ccad 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -37,6 +37,11 @@ export class CommandCollection { } } +interface Permissions { + users: string[]; + groups: string[]; +} + export class Command { name : string; description? : string; @@ -45,7 +50,7 @@ export class Command { cooldown? : number; aliases? : string[]; usage? : string; - permissions? : string[]; + permissions? : Permissions; constructor( name : string, @@ -57,7 +62,7 @@ export class Command { cooldown = 0, aliases = null as string[], usage = null as string, - permissions = null as string[], + permissions = null as Permissions, } ) { this.name = name.toLowerCase(); @@ -73,4 +78,17 @@ export class Command { checkName(commandName : string) { 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; + } } \ No newline at end of file diff --git a/src/commands/addcrowduser.ts b/src/commands/addcrowduser.ts index d360ea5..93fe5ae 100644 --- a/src/commands/addcrowduser.ts +++ b/src/commands/addcrowduser.ts @@ -1,17 +1,19 @@ import {Command} from "../Command"; import got from 'got'; -const {userDict, crowd} = require("../config.json"); +const {userDict, crowd, groupDict} = require("../config.json"); const addcrowduser = new Command( 'addcrowduser', async (message, args) => { const email = args[0]; - const firstNameLetter = args[1][0].toLowerCase(); - const lastName = args[2].toLowerCase(); + 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 + lastName; - const password = lastName + firstNameLetter; + const username = firstNameLetter + lastNameLowercase; + const password = lastNameLowercase + firstNameLetter; try { await got.post("https://conjure.etsmtl.ca/crowd/rest/usermanagement/1/user", { @@ -27,9 +29,9 @@ const addcrowduser = new Command( "value": password }, "active": true, - "email": args[0], - "first-name": args[1], - "last-name": args[2] + "email": email, + "first-name": firstName, + "last-name": lastName } }); } catch (error) { @@ -62,7 +64,10 @@ const addcrowduser = new Command( message.channel.send(`Member added.\nUsername: ${username}\nPassword: ${password}`); }, { description: "Adds a member to Crowd.", - permissions: [userDict.misabiko, userDict.massimo], + permissions: { + users: [], + groups: [groupDict.admin] + }, args: ['email', 'first name', 'last name'] } ); diff --git a/src/commands/listdiscordusers.ts b/src/commands/listdiscordusers.ts index 50c7f90..b6ed19f 100644 --- a/src/commands/listdiscordusers.ts +++ b/src/commands/listdiscordusers.ts @@ -2,7 +2,7 @@ import {Command} from "../Command"; import { TextChannel } from "discord.js"; 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( 'listdiscordusers', @@ -42,7 +42,10 @@ const listdiscordusers = new Command( } }, { description: "Lists discord missing from database.", - permissions: [userDict.misabiko, userDict.massimo] + permissions: { + users: [], + groups: [groupDict.admin] + } } ); export default listdiscordusers; \ No newline at end of file diff --git a/src/commands/reloadcommand.ts b/src/commands/reloadcommand.ts index 0631e53..dc29271 100644 --- a/src/commands/reloadcommand.ts +++ b/src/commands/reloadcommand.ts @@ -17,7 +17,10 @@ const reloadcommand = new Command( description: "Reloads either every, or a given command module.", aliases: ["reload"], usage: "[commandName]", - permissions: [userDict.misabiko] + permissions: { + users: [userDict.misabiko], + groups: [] + } } ); export default reloadcommand; diff --git a/src/index.ts b/src/index.ts index 2fc39b8..6cd3978 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,7 +53,7 @@ export class CustomClient extends Discord.Client { if (command.guildOnly && message.channel.type !== 'text') 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.`); if (!this.cooldowns[command.name])