|
5 | 5 | {-# LANGUAGE TupleSections #-} |
6 | 6 | {-# LANGUAGE LambdaCase #-} |
7 | 7 |
|
8 | | -module Bot.BttvFfz |
9 | | - ( ffzCommand |
10 | | - , bttvCommand |
| 8 | +module Bot.Ffz |
| 9 | + ( ffzUrlByName |
| 10 | + , ffzCommand |
11 | 11 | , updateFfzEmotesCommand |
12 | | - , updateBttvEmotesCommand |
13 | | - , ffzUrlByName |
14 | | - , bttvUrlByName |
15 | 12 | ) where |
16 | 13 |
|
17 | 14 | import Bot.Replies |
18 | | -import Control.Monad |
19 | | -import Data.Aeson |
20 | 15 | import Data.Aeson.Types |
| 16 | +import Data.Functor |
21 | 17 | import qualified Data.HashMap.Strict as HM |
22 | 18 | import Data.List |
23 | 19 | import qualified Data.Map as M |
@@ -51,40 +47,6 @@ instance IsEntity FfzEmote where |
51 | 47 | FfzEmote <$> extractProperty "name" properties <*> |
52 | 48 | pure (extractProperty "largestImageUrl" properties) |
53 | 49 |
|
54 | | -data BttvEmote = BttvEmote |
55 | | - { bttvName :: T.Text |
56 | | - , bttvLargestImageURL :: Maybe T.Text |
57 | | - } |
58 | | - |
59 | | -instance IsEntity BttvEmote where |
60 | | - nameOfEntity _ = "BttvEmote" |
61 | | - toProperties entity = |
62 | | - M.fromList $ |
63 | | - catMaybes |
64 | | - [ return ("name", PropertyText $ bttvName entity) |
65 | | - , ("largestImageUrl", ) . PropertyText <$> bttvLargestImageURL entity |
66 | | - ] |
67 | | - fromProperties properties = |
68 | | - BttvEmote <$> extractProperty "name" properties <*> |
69 | | - pure (extractProperty "largestImageUrl" properties) |
70 | | - |
71 | | -newtype BttvRes = BttvRes |
72 | | - { bttvResEmotes :: [BttvEmote] |
73 | | - } |
74 | | - |
75 | | -instance FromJSON BttvRes where |
76 | | - parseJSON (Object v) = BttvRes <$> v .: "emotes" |
77 | | - parseJSON invalid = typeMismatch "BttvRes" invalid |
78 | | - |
79 | | -instance FromJSON BttvEmote where |
80 | | - parseJSON (Object v) = BttvEmote <$> code <*> url |
81 | | - where |
82 | | - code = v .: "code" |
83 | | - url = |
84 | | - ((\id' -> "https://cdn.betterttv.net/emote/" <> id' <> "/3x") <$>) <$> |
85 | | - (v .: "id") |
86 | | - parseJSON invalid = typeMismatch "BttvEmote" invalid |
87 | | - |
88 | 50 | newtype FfzSet = FfzSet |
89 | 51 | { ffzSetEmotes :: [FfzEmote] |
90 | 52 | } |
@@ -132,35 +94,11 @@ ffzUrl channel = [qms|https://api.frankerfacez.com/v1/room/{encodedChannel}|] |
132 | 94 | where |
133 | 95 | encodedChannel = URI.encode $ T.unpack channel |
134 | 96 |
|
135 | | -bttvUrl :: T.Text -> String |
136 | | -bttvUrl channel = [qms|https://api.betterttv.net/2/channels/{encodedChannel}|] |
137 | | - where |
138 | | - encodedChannel = URI.encode $ T.unpack channel |
139 | | - |
140 | 97 | ffzCommand :: Reaction Message () |
141 | 98 | ffzCommand = |
142 | 99 | liftR (const $ selectEntities Proxy All) $ |
143 | 100 | cmapR (T.concat . intersperse " " . map (ffzName . entityPayload)) sayMessage |
144 | 101 |
|
145 | | -bttvCommand :: Reaction Message () |
146 | | -bttvCommand = |
147 | | - liftR (const $ selectEntities Proxy All) $ |
148 | | - cmapR (T.concat . intersperse " " . map (bttvName . entityPayload)) sayMessage |
149 | | - |
150 | | -updateBttvEmotesCommand :: Reaction Message () |
151 | | -updateBttvEmotesCommand = |
152 | | - transR duplicate $ |
153 | | - cmapR (twitchChannelName . senderChannel . messageSender) $ |
154 | | - replyOnNothing "Only works in Twitch channels" $ |
155 | | - cmapR bttvUrl $ |
156 | | - jsonHttpRequestReaction $ |
157 | | - cmapR bttvResEmotes $ |
158 | | - liftR |
159 | | - (\emotes -> do |
160 | | - void $ deleteEntities (Proxy :: Proxy BttvEmote) All |
161 | | - traverse (createEntity Proxy) emotes) $ |
162 | | - cmapR (T.concat . intersperse " " . map (bttvName . entityPayload)) sayMessage |
163 | | - |
164 | 102 | updateFfzGlobalEmotes :: Reaction Message a |
165 | 103 | updateFfzGlobalEmotes = |
166 | 104 | cmapR (const "https://api.frankerfacez.com/v1/set/global") $ |
@@ -200,12 +138,3 @@ ffzUrlByName name = do |
200 | 138 | Proxy |
201 | 139 | (Filter (PropertyEquals "name" (PropertyText name)) All) |
202 | 140 | pure (T.unpack <$> (ffzLargestImageURL =<< (entityPayload <$> emote))) |
203 | | - |
204 | | -bttvUrlByName :: T.Text -> Effect (Maybe String) |
205 | | -bttvUrlByName name = do |
206 | | - emote <- |
207 | | - listToMaybe <$> |
208 | | - selectEntities |
209 | | - Proxy |
210 | | - (Filter (PropertyEquals "name" (PropertyText name)) All) |
211 | | - pure (T.unpack <$> (bttvLargestImageURL =<< (entityPayload <$> emote))) |
0 commit comments