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.

80 lines
2.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE RecordWildCards #-}
  3. module Main where
  4. import Control.Monad (when, void)
  5. import qualified Data.Text as T
  6. import qualified Data.Text.IO as TIO
  7. import UnliftIO (liftIO)
  8. import Discord
  9. import Discord.Types
  10. import Discord.Interactions
  11. import qualified Discord.Requests as R
  12. import Commands
  13. import qualified Data.ByteString as BS
  14. import qualified Data.Yaml as YAML
  15. import qualified Data.HashMap.Strict as Map
  16. testServer :: Snowflake
  17. testServer = 740862954454646814
  18. main :: IO ()
  19. main = do
  20. tok <- TIO.readFile "./auth.secret"
  21. conf <- YAML.decodeFileThrow "./conf.yaml" :: IO YAML.Value
  22. putStrLn $ show conf
  23. err <- runDiscord $ def { discordToken = tok
  24. , discordOnStart = onDiscordStart conf
  25. , discordOnEnd = liftIO $ putStrLn "Ended"
  26. , discordOnEvent = onDiscordEvent conf
  27. , discordOnLog =
  28. \s -> TIO.putStrLn s >> TIO.putStrLn ""
  29. }
  30. TIO.putStrLn err
  31. onDiscordStart :: YAML.Value -> DiscordHandler ()
  32. onDiscordStart conf = do
  33. let activity = Activity { activityName = "Doing stuff"
  34. , activityType = ActivityTypeGame
  35. , activityUrl = Nothing
  36. }
  37. let opts = UpdateStatusOpts { updateStatusOptsSince = Nothing
  38. , updateStatusOptsGame = Just activity
  39. , updateStatusOptsNewStatus = UpdateStatusOnline
  40. , updateStatusOptsAFK = False
  41. }
  42. sendCommand (UpdateStatus opts)
  43. onDiscordEvent :: YAML.Value -> Event -> DiscordHandler ()
  44. onDiscordEvent conf (Ready _ _ _ _ _ _ (PartialApplication i _)) =
  45. mapM_ (maybe ( return () )
  46. ( void
  47. . restCall
  48. . R.CreateGuildApplicationCommand i testServer
  49. )
  50. )
  51. [ Just pingCommand
  52. , Just edtCommand
  53. ]
  54. onDiscordEvent conf
  55. ( InteractionCreate InteractionApplicationCommand
  56. { interactionDataApplicationCommand =
  57. Just InteractionDataApplicationCommandChatInput
  58. { interactionDataApplicationCommandName = name
  59. , interactionDataApplicationCommandOptions = opts
  60. , ..
  61. }
  62. , ..
  63. }
  64. ) = do
  65. void $ restCall
  66. (R.CreateInteractionResponse interactionId interactionToken response)
  67. where
  68. response = case name of
  69. "ping" -> pingResponse
  70. "edt" -> edtResponse opts
  71. _ -> interactionResponseBasic $ "Unhandled Command: " `T.append` name
  72. onDiscordEvent _ _ = return ()