From ea190de02cea99347ae125d86cc22bf8cd926c88 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 14 Sep 2017 03:44:38 +0200 Subject: Add `Bot` to rest of plugins Add our `Bot` monad to the rest of the plugins: * Factorial * GitRemoteSetOrigin * Help The only problem is with the Help plugin. Still trying to figure out how to set up my list comprehension so that it works with the `Bot`-wrapped `Plugin` list. --- src/Plugin/Factorial.hs | 20 ++++++++++++-------- src/Plugin/GitRemoteSetOrigin.hs | 7 +++++-- src/Plugin/Help.hs | 32 ++++++++++++++++++++------------ src/PluginList.hs | 3 ++- 4 files changed, 39 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/Plugin/Factorial.hs b/src/Plugin/Factorial.hs index 9284fe7..9d7a169 100644 --- a/src/Plugin/Factorial.hs +++ b/src/Plugin/Factorial.hs @@ -7,20 +7,24 @@ module Plugin.Factorial import Text.Regex.TDFA ((=~)) import TextShow (showt) +import Bot (Bot) import qualified Message as M import Plugin.Base -factorial = defaultPlugin - { matchRegex = "^([0-9]+)!$" - , perform = factorialAction - , command = "!" - , description = "Calculate the factorial of for whole numbers \ - \up to 35000." - } +factorial :: Bot Plugin +factorial = do + return defaultPlugin + { matchRegex = "^([0-9]+)!$" + , perform = factorialAction + , command = "!" + , description = "Calculate the factorial of for whole numbers \ + \up to 35000." + } factorialAction :: PluginAction factorialAction message = do - case M.textStr message =~ matchRegex factorial :: [[String]] of + plugin <- factorial + case M.textStr message =~ matchRegex plugin :: [[String]] of [] -> return $ Left "I didn't understand" (m:_) -> do let number = last m diff --git a/src/Plugin/GitRemoteSetOrigin.hs b/src/Plugin/GitRemoteSetOrigin.hs index 0fdb4ef..7d46e46 100644 --- a/src/Plugin/GitRemoteSetOrigin.hs +++ b/src/Plugin/GitRemoteSetOrigin.hs @@ -10,10 +10,12 @@ import qualified Data.Text as T import Database.SQLite.Simple import Text.Regex.TDFA ((=~)) +import Bot (Bot) import qualified Message as M import Plugin.Base -gitRemoteSetOrigin = defaultPlugin +gitRemoteSetOrigin :: Bot Plugin +gitRemoteSetOrigin = return defaultPlugin { matchRegex = "^git remote set origin ([^ ]+)$" , perform = gitRemoteSetOriginAction , command = "git remote set origin " @@ -22,7 +24,8 @@ gitRemoteSetOrigin = defaultPlugin gitRemoteSetOriginAction :: PluginAction gitRemoteSetOriginAction message = do - case M.textStr message =~ matchRegex gitRemoteSetOrigin :: [[String]] of + plugin <- gitRemoteSetOrigin + case M.textStr message =~ matchRegex plugin :: [[String]] of [] -> return $ Left "blast" (m:_) -> do let url = last m diff --git a/src/Plugin/Help.hs b/src/Plugin/Help.hs index 63143b1..b29d1b9 100644 --- a/src/Plugin/Help.hs +++ b/src/Plugin/Help.hs @@ -4,28 +4,36 @@ module Plugin.Help ( help ) where +import Control.Monad (sequence) import qualified Data.Text as T +import Bot (Bot) import qualified PluginList as PL (plugins) import Plugin.Base -help = defaultPlugin - { matchRegex = "^help$" - , perform = helpAction - , command = "help" - , description = "Show a list of available bot commands." - , queryOnly = True - } +help :: Bot Plugin +help = do + return defaultPlugin + { matchRegex = "^help$" + , perform = helpAction + , command = "help" + , description = "Show a list of available bot commands." + , queryOnly = True + } helpAction :: PluginAction helpAction _ = do return $ Right $ T.intercalate "\n" - [T.justifyRight longestCommandLen ' ' (command p) - `T.append` " – " - `T.append` description p - | p <- plugins] + -- [T.justifyRight longestCommandLen ' ' (command p) + -- `T.append` " – " + -- `T.append` description p + -- | p <- plugins'] where longestCommandLen = foldr (max) 0 (map (T.length . command) plugins) -plugins :: [Plugin] + helpText plugin = T.justifyRight longestCommandLen ' ' (command plugin) + `T.append` " – " + `T.append` description plugin + +plugins :: [Bot Plugin] plugins = PL.plugins ++ [help] diff --git a/src/PluginList.hs b/src/PluginList.hs index bd2af35..070033a 100644 --- a/src/PluginList.hs +++ b/src/PluginList.hs @@ -2,6 +2,7 @@ module PluginList ( plugins ) where +import Bot (Bot) import Plugin.Base (Plugin) import Plugin.Factorial (factorial) import Plugin.GitHubCommit (gitHubCommit) @@ -9,7 +10,7 @@ import Plugin.GitRemoteSetOrigin (gitRemoteSetOrigin) -- | The list of plugins to load, minus the Help plugin, which would otherwise -- cause a circular import. -plugins :: [Plugin] +plugins :: [Bot Plugin] plugins = [ factorial , gitHubCommit -- cgit v1.2.3