You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB

3 years ago
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Main where
  3. import Control.Monad
  4. import qualified Data.Text as T
  5. import qualified Data.Text.IO as TIO
  6. import UnliftIO (liftIO)
  7. import Discord
  8. import Discord.Types
  9. import qualified Discord.Requests as R
  10. cmdPrefix :: T.Text
  11. cmdPrefix = "%"
  12. main :: IO ()
  13. main = do
  14. tok <- TIO.readFile "./auth.secret"
  15. err <- runDiscord $ def { discordToken = tok
  16. , discordOnStart = onDiscordStart
  17. , discordOnEnd = liftIO $ putStrLn "Ended"
  18. , discordOnEvent = onDiscordEvent
  19. , discordOnLog =
  20. \s -> TIO.putStrLn s >> TIO.putStrLn ""
  21. }
  22. TIO.putStrLn err
  23. onDiscordStart :: DiscordHandler ()
  24. onDiscordStart = undefined
  25. onDiscordEvent :: Event -> DiscordHandler ()
  26. onDiscordEvent (MessageCreate m) = when (not (fromBot m) && isCommand m) $ do
  27. let content = messageContent m
  28. let elements = T.words content
  29. case head elements of
  30. _ -> unknownCommand m elements
  31. return ()
  32. onDiscordEvent _ = return ()
  33. fromBot :: Message -> Bool
  34. fromBot = userIsBot . messageAuthor
  35. unknownCommand :: Message -> [Text] -> DiscordHandler ()
  36. unknownCommand =
  37. isCommand :: Message ->Bool
  38. isCommand = (cmdPrefix `T.isPrefixOf`) . messageContent