Something went wrong. Try again.
A simple Discord bot framework built on discord-haskell
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120{-# LANGUAGE ExtendedDefaultRules #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE RankNTypes #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE MonoLocalBinds #-}
module Howdy.Comptime.Command ( Command , Permission , CommandDefinition (..) , CommandInput (..) , CommandReplyData (..) , alias , desc , hide , legacy , permission , run , (Howdy.Comptime.Command.>>) ) where
import Control.Monad.Identity (Identity)import Control.Monad.Trans.Except (ExceptT)import Control.Optics (Lensed (focus), WithLens (..))import Data.Default (Default (def))import Data.Text (Text)import Discord (DiscordHandler)import Discord.Types (ChannelId, GuildId, MessageReference, User, UserId)import Howdy.Internal.Discord (HowdyHandler, MonadReply)import Howdy.Internal.Error (HowdyException)import Howdy.Internal.Logging ( (<<), (<+) )import Lens.Micro (to)import Control.Monad.IO.Class ( MonadIO )import Data.Kind (Constraint, Type)import Howdy.Internal.Util (ToConstraints)
type Permission = CommandInput -> Bool
type Command = forall m. (MonadIO m, MonadReply m) => CommandInput -> m ()
data CommandReplyData = CommandReplyData { crdChannelId :: ChannelId , crdWhisperId :: UserId , crdMessageRef :: MessageReference } instance Lensed CommandReplyData ChannelId where focus = to crdChannelId
instance Lensed CommandReplyData MessageReference where focus = to crdMessageRef
instance Lensed CommandReplyData UserId where focus = to crdWhisperId
data CommandDefinition = CommandDefinition { cdAlias :: [Text] , cdDesc :: Maybe Text , cdHidden :: Bool , cdIdent :: Text , cdPermission :: Permission , cdRunner :: Command , cdDebug :: Bool -- TODO: change to debug flags and later to customizable flags }
data CommandInput = CommandInput { target :: Text , author :: User , guild :: Maybe GuildId , channel :: ChannelId , args :: Text , ref :: Maybe MessageReference } deriving (Show, Eq)
instance Default CommandDefinition where def = CommandDefinition { cdAlias = [] , cdDesc = Nothing , cdHidden = False , cdIdent = "" , cdPermission = const True , cdRunner = const $ pure () , cdDebug = False }
infixl 1 >>(>>) :: (b -> c) -> (a -> b) -> a -> c(>>) = (.)
alias :: [Text] -> CommandDefinition -> CommandDefinitionalias value cd = cd{ cdAlias = cd.cdAlias ++ value } << "Adding aliases: " <+ value
desc :: Text -> CommandDefinition -> CommandDefinitiondesc value cd = cd{ cdDesc = Just value } << "Setting desc: " <+ value
hide :: CommandDefinition -> CommandDefinitionhide cd = cd{ cdHidden = True } << "Hide: True"
legacy :: Text -> CommandDefinition -> CommandDefinitionlegacy value cd = cd{ cdIdent = value } << "Legacy id: " <+ value
permission :: Permission -> CommandDefinition -> CommandDefinitionpermission value cd = cd{ cdPermission = value } << "Permission updated"
run :: Command -> CommandDefinition -> CommandDefinitionrun x cd = cd{ cdRunner = x } << "Set run action."