|
5 | 5 | module Bot.Dubtrack where |
6 | 6 |
|
7 | 7 | import Bot.Replies |
| 8 | +import Control.Monad |
8 | 9 | import Data.Aeson |
9 | 10 | import Data.Aeson.Types |
| 11 | +import qualified Data.Map as Map |
| 12 | +import Data.Maybe |
| 13 | +import qualified Data.Proxy as P |
10 | 14 | import qualified Data.Text as T |
11 | 15 | import Effect |
12 | | -import Network.HTTP.Simple |
| 16 | +import Entity |
| 17 | +import Property |
13 | 18 | import Reaction |
14 | 19 | import Text.InterpolatedString.QM |
15 | 20 | import Transport |
@@ -63,17 +68,40 @@ songLink (songType -> SongTypeSoundcloud) = |
63 | 68 | "Soundcloud links are not supported yet" |
64 | 69 | songLink _ = error "This should never happen Kappa" |
65 | 70 |
|
66 | | --- TODO(#221): Dubtrack room is hardcode |
| 71 | +newtype DubtrackRoom = DubtrackRoom |
| 72 | + { unName :: T.Text |
| 73 | + } |
| 74 | + |
| 75 | +instance IsEntity DubtrackRoom where |
| 76 | + nameOfEntity _ = "DubtrackRoom" |
| 77 | + toProperties reply = Map.fromList [("name", PropertyText $ unName reply)] |
| 78 | + fromProperties = fmap DubtrackRoom . extractProperty "name" |
| 79 | + |
| 80 | +getRoom :: Effect (Maybe (Entity DubtrackRoom)) |
| 81 | +getRoom = listToMaybe <$> selectEntities P.Proxy (Take 1 All) |
| 82 | + |
| 83 | +setDubtrackRoom :: Reaction Message T.Text |
| 84 | +setDubtrackRoom = |
| 85 | + liftR |
| 86 | + (\msg -> do |
| 87 | + mayReply <- getRoom |
| 88 | + case mayReply of |
| 89 | + Just reply -> |
| 90 | + void $ updateEntityById $ (\a -> a {unName = msg}) <$> reply |
| 91 | + Nothing -> void $ createEntity P.Proxy $ DubtrackRoom msg) $ |
| 92 | + cmapR (const "Updated room for dubtrack") $ Reaction replyMessage |
| 93 | + |
67 | 94 | currentSongCommand :: Reaction Message () |
68 | 95 | currentSongCommand = |
69 | | - Reaction $ \Message {messageSender = sender} -> do |
70 | | - request <- parseRequest "https://api.dubtrack.fm/room/tsoding" |
71 | | - response <- eitherDecode . getResponseBody <$> httpRequest request |
72 | | - case response of |
73 | | - Left message -> errorEff $ T.pack message |
74 | | - Right dubtrackResponse -> |
75 | | - maybe |
76 | | - (replyToSender sender "Nothing is playing right now") |
77 | | - (\song -> |
78 | | - replyToSender sender [qms|❝{songName song}❞: {songLink song}|]) |
79 | | - (roomCurrentSong $ drData dubtrackResponse) |
| 96 | + liftR (const getRoom) $ |
| 97 | + replyOnNothing |
| 98 | + [qms|Dubtrack room not set, a mod can run |
| 99 | + '!config dubtrack <room-name>' to set it|] $ |
| 100 | + cmapR (T.unpack . urlOfRoom . entityPayload) $ |
| 101 | + jsonHttpRequestReaction $ |
| 102 | + cmapR (roomCurrentSong . drData) $ |
| 103 | + replyOnNothing "Nothing is playing right now" $ |
| 104 | + cmapR (\song -> [qms|❝{songName song}❞: {songLink song}|]) $ |
| 105 | + Reaction replyMessage |
| 106 | + where |
| 107 | + urlOfRoom = ("https://api.dubtrack.fm/room/" <>) . unName |
0 commit comments