Skip to content

Commit

Permalink
Fixup permission checks
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLotU committed Jun 21, 2020
1 parent 6639d0a commit b194b4d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Sources/SwiftHooks/Commands/Command+Permissions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion Sources/SwiftHooks/Commands/SwiftHooks+Commands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftHooks/Types/Messagable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b194b4d

Please sign in to comment.