From 7401cdb88cc1c2048c65f6ec3d730c4d0206ddfd Mon Sep 17 00:00:00 2001 From: Dragory Date: Thu, 12 Jul 2018 01:42:18 +0300 Subject: [PATCH] ModActions: add helper function for checking if user can act on another --- src/plugins/ModActions.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/plugins/ModActions.ts b/src/plugins/ModActions.ts index 31d260a3..79967732 100644 --- a/src/plugins/ModActions.ts +++ b/src/plugins/ModActions.ts @@ -272,15 +272,9 @@ export class ModActionsPlugin extends Plugin { } // Make sure we're allowed to mute this member - if (msg.member.id !== args.member.id) { - const ourLevel = this.getMemberLevel(msg.member); - const memberLevel = this.getMemberLevel(args.member); - if (ourLevel <= memberLevel) { - msg.channel.createMessage( - errorMessage("Cannot mute: insufficient permissions") - ); - return; - } + if (!this.canActOn(msg.member, args.member)) { + msg.channel.createMessage(errorMessage("Cannot mute: insufficient permissions")); + return; } // Convert mute time from e.g. "2h30m" to milliseconds @@ -382,6 +376,16 @@ export class ModActionsPlugin extends Plugin { } } + protected canActOn(member1, member2) { + if (member1.id === member2.id) { + return true; + } + + const ourLevel = this.getMemberLevel(member1); + const memberLevel = this.getMemberLevel(member2); + return (ourLevel > memberLevel); + } + /** * Attempts to message the specified user through DMs and/or the message channel. * Returns a promise that resolves to a boolean indicating whether we were able to message them or not.