From b194b4d9ff8eed5d56f22569d6f1dd2407263e84 Mon Sep 17 00:00:00 2001 From: MrLotU Date: Sun, 21 Jun 2020 11:15:34 +0200 Subject: [PATCH] Fixup permission checks --- Sources/SwiftHooks/Commands/Command+Permissions.swift | 4 ++++ Sources/SwiftHooks/Commands/SwiftHooks+Commands.swift | 7 ++++++- Sources/SwiftHooks/Types/Messagable.swift | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftHooks/Commands/Command+Permissions.swift b/Sources/SwiftHooks/Commands/Command+Permissions.swift index 76e6845..4d79585 100644 --- a/Sources/SwiftHooks/Commands/Command+Permissions.swift +++ b/Sources/SwiftHooks/Commands/Command+Permissions.swift @@ -32,6 +32,10 @@ public struct IDChecker: CommandPermissionChecker { /// List of whitelisted IDs let ids: [String] + public init(_ ids: String...) { + self.ids = ids + } + public func check(_ user: Userable, canUse command: _ExecutableCommand, on event: CommandEvent) -> Bool { guard let id = user.identifier else { return false } return ids.contains(id) diff --git a/Sources/SwiftHooks/Commands/SwiftHooks+Commands.swift b/Sources/SwiftHooks/Commands/SwiftHooks+Commands.swift index 944915f..3d2d6a0 100644 --- a/Sources/SwiftHooks/Commands/SwiftHooks+Commands.swift +++ b/Sources/SwiftHooks/Commands/SwiftHooks+Commands.swift @@ -22,13 +22,18 @@ extension SwiftHooks { foundCommands.forEach { (command) in guard command.hookWhitelist.isEmpty || command.hookWhitelist.contains(h.id) else { return } let event = CommandEvent(hooks: self, cmd: command, msg: message, prefix: prefix, for: h, on: eventLoop) + guard command.permissionChecks.map({ check in + check.check(message.gAuthor, canUse: command, on: event) + }).allSatisfy({ bool in bool }) else { + return event.message.error(CommandError.InvalidPermissions, on: command) + } event.logger.debug("Invoking command") event.logger.trace("Full message: \(message.content)") let timer = Timer(label: "command_duration", dimensions: [("command", command.fullTrigger)]) let start = DispatchTime.now().uptimeNanoseconds command.invoke(on: event) - .flatMapErrorThrowing({ (e) in + .flatMapErrorThrowing({ (e) in event.message.error(e, on: command) throw e }) diff --git a/Sources/SwiftHooks/Types/Messagable.swift b/Sources/SwiftHooks/Types/Messagable.swift index e647b29..93d0fff 100644 --- a/Sources/SwiftHooks/Types/Messagable.swift +++ b/Sources/SwiftHooks/Types/Messagable.swift @@ -52,7 +52,7 @@ public extension Messageable { case CommandError.ArgumentNotFound(let arg): self.reply("Missing argument: \(arg)\nUsage: \(command.help)") case CommandError.InvalidPermissions: - self.reply("Invalid permissions!\nUsage: \(command.help)") + self.reply("Invalid permissions!") case CommandError.UnableToConvertArgument(let arg, let type): self.reply("Error converting \(arg) to \(type)\nUsage: \(command.help)") default: