Something went wrong. Try again.
A simple Discord bot framework built on discord-haskell
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115{-# LANGUAGE ExtendedDefaultRules #-}{-# LANGUAGE FlexibleInstances #-}
module Howdy.Comptime.Bot where
import Control.Monad.Reader (Reader)import Data.Default (Default (..))import Data.HashMap.Internal.Strict (HashMap, empty, insert)import Data.Text (Text)import Debug.Trace (trace)import Discord.Types (ChannelId, UserId)import Howdy.Comptime.Command (CommandDefinition (..))import Howdy.Comptime.Reaction (EmojiIdentifier, ReactionDefinition)import Howdy.Internal.Debug (DebugOptions (..))import Howdy.Internal.Logging (Fmt (..), (<+), (<<))import Discord (DiscordHandler)import Data.Dynamic (Dynamic, toDyn)
data BotMeta = BotMeta { bmAuthor :: Maybe UserId , bmRepo :: Maybe Text , bmNote :: Maybe Text , bmName :: Maybe Text } deriving (Show)
data BotDefinition = BotDefinition { bdPrefixes :: [Text] , bdCommands :: HashMap Text CommandDefinition , bdReactions :: HashMap EmojiIdentifier ReactionDefinition , bdAliases :: HashMap Text Text , bdEmojis :: HashMap EmojiIdentifier Text , bdTokens :: [String] , bdDebug :: DebugOptions , bdMeta :: BotMeta , bdOnStart :: DiscordHandler () , bdOnInit :: IO () }
instance Default BotDefinition where def = BotDefinition { bdPrefixes = [] , bdCommands = empty , bdReactions = empty , bdAliases = empty , bdEmojis = empty , bdTokens = [] , bdDebug = def , bdMeta = def , bdOnStart = pure () , bdOnInit = pure () }
instance Default BotMeta where def = BotMeta { bmAuthor = Nothing , bmRepo = Nothing , bmNote = Nothing , bmName = Nothing }
--loadDefaults :: BotData -> BotData--loadDefaults (m, p) = (m, p)--(m, p { commands = insert "help" undefined p.commands})
(>>) :: (b -> c) -> (a -> b) -> a -> c(>>) = (.)
--default (Text)
prefixes :: [Text] -> BotDefinition -> BotDefinitionprefixes x bd = bd{ bdPrefixes = x } << "Setting prefixes: " -- ++ show x
command :: Text -> (CommandDefinition -> CommandDefinition) -> BotDefinition -> BotDefinitioncommand name cd bd = bd { bdCommands = insert name cd' bd.bdCommands , bdAliases = registerAliases (name:cd'.cdAlias) name bd.bdAliases } << "Adding command: " <+ name where cd' = cd def
reaction :: EmojiIdentifier -> (ReactionDefinition -> ReactionDefinition) -> BotDefinition -> BotDefinitionreaction name rd bd = bd{ bdReactions = insert name rd' bd.bdReactions } << "Adding reaction: " <+ name where rd' = rd def
token :: String -> BotDefinition -> BotDefinitiontoken x bd = bd { bdTokens = x:bd.bdTokens } << "Setting token: ***"
author :: UserId -> BotDefinition -> BotDefinitionauthor x bd = bd{ bdMeta = bd.bdMeta { bmAuthor = Just x } } << "Setting author: " <+ x
repo :: Text -> BotDefinition -> BotDefinitionrepo x bd = bd{ bdMeta = bd.bdMeta { bmRepo = Just x } } << "Setting repo: " <+ x
note :: Text -> BotDefinition -> BotDefinitionnote x bd = bd{ bdMeta = bd.bdMeta { bmNote = Just x } } << "Setting note: " <+ x
name :: Text -> BotDefinition -> BotDefinitionname x bd = bd{ bdMeta = bd.bdMeta { bmName = Just x } } << "Setting name: " <+ x
onInit :: IO () -> BotDefinition -> BotDefinitiononInit x bd = bd{ bdOnInit = x }
onStart :: DiscordHandler () -> BotDefinition -> BotDefinitiononStart x bd = bd{ bdOnStart = x }
registerAliases :: [Text] -> Text -> HashMap Text Text -> HashMap Text TextregisterAliases as t = go as where go (x:xs) m = insert x t (go xs m) go [] m = m