diff options
author | Teddy Wing | 2017-09-19 04:52:46 +0200 |
---|---|---|
committer | Teddy Wing | 2017-09-19 04:58:26 +0200 |
commit | affb573718b28e2df9f5c5f10fb7a1fff434495e (patch) | |
tree | 6c00eb5a343896e4eda1ff5f10d93bccc9221564 /src | |
parent | e689b4b8cbf1c73f04b1b348fd0d598d401085b1 (diff) | |
download | sorbot-affb573718b28e2df9f5c5f10fb7a1fff434495e.tar.bz2 |
Plugin(matchPlugin): Get our functions to actually work with `Bot`
Previously we added the `Bot` monad to our signature, but we didn't have
a way to filter and match plugins wrapped in the `Bot` monad. This is
what we're doing here. It's the same work as before, but we need to
account for a wrapped `Plugin` type.
There's a lot of extra cruft code here from when I was trying things out
to get it working. The short story is that I do some binding and
sequencing dances to extract `Plugin`s so that we can actually match
against them.
I've seen recommendations against `fail`, which is why I tried to use
`mzero`, but it just seemed to be too complicated so went with `fail`,
which seems to make sense in context.
Diffstat (limited to 'src')
-rw-r--r-- | src/Bot.hs | 2 | ||||
-rw-r--r-- | src/Plugin.hs | 46 |
2 files changed, 45 insertions, 3 deletions
@@ -17,3 +17,5 @@ newtype Bot a = Bot { runBot :: ReaderT Options IO a -- } deriving (Monad, Functor, Applicative, BotConfig, MonadIO) } deriving (Monad, Functor, Applicative, MonadReader Options, MonadIO) + +-- instance MonadPlus Bot diff --git a/src/Plugin.hs b/src/Plugin.hs index a79e815..04a8054 100644 --- a/src/Plugin.hs +++ b/src/Plugin.hs @@ -4,6 +4,7 @@ module Plugin , plugins ) where +import Control.Monad (mzero) import qualified Data.Text as T import Text.Regex.TDFA @@ -16,15 +17,54 @@ import Plugin.Help (help) -- | Get the first plugin that matches the given message text. matchPlugin :: M.Message -> Maybe (Bot Plugin) -matchPlugin message = firstPlugin $ matchPlugins message plugins +matchPlugin message = do + -- plugins' <- return $ sequence plugins + -- firstPlugin $ matchPlugins message plugins' + return $ firstPlugin $ matchBotPlugins message where - firstPlugin [] = Nothing - firstPlugin (p:ps) = Just p + firstPlugin :: Bot [Plugin] -> Bot Plugin + -- firstPlugin [] = Nothing + -- firstPlugin (p:ps) = Just p + firstPlugin plugins = do + plugins' <- plugins + case plugins' of + -- [] -> mzero + [] -> fail "Empty plugin list" + (p:ps) -> return p + + -- matchBotPlugins :: M.Message -> [Bot Plugin] + -- matchBotPlugins message = do + -- -- plugins' <- sequence plugins + -- -- return $ matchPlugins message plugins' + -- return $ matchPlugins message plugins + matchBotPlugins :: M.Message -> Bot [Plugin] + matchBotPlugins message = do + plugins' <- sequence plugins + return $ matchPlugins message plugins' -- | Filter the list of plugins to those that match the given message. matchPlugins :: M.Message -> [Plugin] -> [Plugin] matchPlugins message plugins = [p | p <- plugins, M.textStr message =~ matchRegex p] + -- where + -- matches :: M.Message -> Bot Plugin -> Bool + -- matches message pluginM = do + -- p <- pluginM + -- M.textStr message =~ matchRegex p +-- --- +-- matchPlugins :: M.Message -> [Bot Plugin] -> [Bot Plugin] +-- matchPlugins message plugins = +-- [p | p <- plugins, matches message p] +-- where +-- matches :: M.Message -> Bot Plugin -> Bool +-- matches message pluginM = do +-- p <- pluginM +-- M.textStr message =~ matchRegex p +-- --- +-- matchPlugins :: M.Message -> [Bot Plugin] -> [Bot Plugin] +-- matchPlugins message plugins = do +-- _ <- return $ sequence plugins +-- [return p | p <- plugins', M.textStr message =~ matchRegex p] -- | Run the action belonging to the plugin, stored in its `perform` field. performPlugin :: Plugin -> PluginAction |