module Network.IRC.Fun.Bot.Internal.Failure
( defaultRespondToChan
, defaultRespondToUser
, failToChannel
, failToUser
)
where
import Data.Char (toLower)
import Data.List (intercalate)
import Network.IRC.Fun.Bot.Internal.Chat (sendToChannel, sendToUser)
import Network.IRC.Fun.Bot.Internal.State (askBehaviorS)
import Network.IRC.Fun.Bot.Internal.Types
import Text.Printf (printf)
defaultResponse :: Char
-> String
-> [CommandSet e s]
-> Maybe (CommandSet e s)
-> String
defaultResponse cpref _cname csets Nothing =
printf "Expected to find a command with prefix ‘%v’, but there’s none. \
\Available prefixes are: ‘%v’."
cpref $
intercalate "’, ‘" $ map ((: []) . prefix) csets
defaultResponse cpref cname _csets (Just cset) =
printf "The command ‘%v%v’ isn’t available. Available commands for prefix \
\‘%v’ are: ‘%v’."
cpref
cname
cpref $
intercalate "’, ‘" $ map (head . names) $ commands cset
defaultRespondToChan
:: String
-> Char
-> String
-> Maybe (CommandSet e s)
-> Session e s ()
defaultRespondToChan chan cpref cname cset = do
csets <- askBehaviorS commandSets
sendToChannel chan $ defaultResponse cpref cname csets cset
defaultRespondToUser
:: String
-> Char
-> String
-> Maybe (CommandSet e s)
-> Session e s ()
defaultRespondToUser nick cpref cname cset = do
csets <- askBehaviorS commandSets
sendToUser nick $ defaultResponse cpref cname csets cset
suffix :: Integral a => a -> String
suffix 1 = "st"
suffix 2 = "nd"
suffix 3 = "rd"
suffix _ = "th"
failureDescription :: Failure -> String
failureDescription failure =
case failure of
WrongNumArgs ->
"Wrong number of arguments"
WrongNumArgsN Nothing Nothing ->
failureDescription WrongNumArgs
WrongNumArgsN (Just given) Nothing ->
printf "Wrong number of arguments: %v given" given
WrongNumArgsN Nothing (Just expected) ->
printf "Wrong number of arguments: %v expected" expected
WrongNumArgsN (Just given) (Just expected) ->
printf "Wrong number of arguments: %v given, %v expected"
given
expected
InvalidArgs -> "Invalid arguments"
InvalidArg Nothing Nothing -> "Invalid argument found"
InvalidArg (Just pos) Nothing ->
printf "The %v%v argument is invalid" pos (suffix pos)
InvalidArg Nothing (Just param) ->
printf "Invalid argument: ‘%v’" param
InvalidArg (Just pos) (Just param) ->
printf "The %v%v argument is invalid: ‘%v’" pos (suffix pos) param
OtherFail s -> "Command failed: " ++ s
failToChannel :: String
-> String
-> Failure
-> Session e s ()
failToChannel chan nick failure =
let lowerc [] = []
lowerc (c:cs) = toLower c : cs
in sendToChannel chan $
nick ++ ", " ++ lowerc (failureDescription failure)
failToUser :: String
-> Failure
-> Session e s ()
failToUser nick failure =
sendToUser nick $ failureDescription failure