From 644ebcefc958354a0e352ee7df58e7d686b46c66 Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 7 Apr 2020 18:59:13 +0000 Subject: [PATCH] Simplify command system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All that type class and language extension madness was completely and utterly unnecessary, but I only noticed it after I had already pushed the implementation. Oh well ¯\_(ツ)_/¯ --- src/Haboli/Euphoria/Command.hs | 37 ++++++---------------------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/src/Haboli/Euphoria/Command.hs b/src/Haboli/Euphoria/Command.hs index 09f402c..fd0e27c 100644 --- a/src/Haboli/Euphoria/Command.hs +++ b/src/Haboli/Euphoria/Command.hs @@ -1,14 +1,7 @@ -{-# LANGUAGE ExistentialQuantification #-} -{-# LANGUAGE MultiParamTypeClasses #-} - --- | This module provides an abstraction for bot commands in the form of the --- 'Hook' type class and the 'Command' type. +-- | This module provides an abstraction for bot commands. module Haboli.Euphoria.Command - ( Hook(..) - , Command - , cmd - , runCommand + ( Command , runCommands , respondingToCommands ) where @@ -18,34 +11,16 @@ import Control.Monad import Haboli.Euphoria.Api import Haboli.Euphoria.Client --- | A hook is a way to react to new messages in a room. These typically don't --- include the messages sent by the client itself. -class Hook h where - -- | @reel h msg@ applies the hook @h@ to the 'Message' @msg@. If no further - -- hooks should be applied to this message, it should return 'True'. - -- Otherwise, it should return 'False'. - reel :: h e -> Message -> Client e Bool - --- | A wrapper around hooks that allows for heterogenous lists of hooks. In --- other words, it lets you combine different 'Hook' instances into a single --- list. -data Command e = forall h. (Hook h) => Command (h e) - --- | Wrap a hook. Notice how the @h@ type disappears: This function can convert --- different 'Hook' instances into the same type. -cmd :: (Hook h) => h e -> Command e -cmd = Command - --- | Apply a 'Command' to a 'Message'. For more information, see 'reel'. -runCommand :: Command e -> Message -> Client e Bool -runCommand (Command h) = reel h +-- | If a command should block any further commands from executing on a message, +-- it should return 'True'. Otherwise. it should return 'False'. +type Command e = Message -> Client e Bool -- | Apply multiple 'Command's to a 'Message' in order until one returns 'True'. -- All commands following that one are not applied. runCommands :: [Command e] -> Message -> Client e Bool runCommands [] _ = pure False runCommands (c:cs) msg = do - abort <- runCommand c msg + abort <- c msg if abort then pure True else runCommands cs msg