forked from discord-haskell/discord-haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-without-gateway.hs
50 lines (38 loc) · 1.59 KB
/
rest-without-gateway.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import Control.Monad (forever)
import Control.Concurrent -- Chans and Threads
import Discord.Types
import qualified Discord.Requests as R
import Discord.Internal.Rest (startRestThread, writeRestCall, RestCallInternalException(..))
import ExampleUtils (getToken, getGuildId)
{-
Peel back the `runDiscord` abstraction
Send an HTTP restcall without logging into the gateway
-}
main :: IO ()
main = do
tok <- getToken
testserverid <- getGuildId
-- SETUP LOG
printQueue <- newChan :: IO (Chan T.Text)
printThreadId <- forkIO $ forever $ readChan printQueue >>= TIO.putStrLn
-- START REST LOOP THREAD
(restChan, restThreadId) <- startRestThread (Auth tok) printQueue
-- a rest call to get the channels in which we will post a message
Right cs <- writeRestCall restChan (R.GetGuildChannels testserverid)
-- ONE REST CALL
resp <- writeRestCall restChan (R.CreateMessage (channelId (head $ filter isTextChannel cs)) "Message")
case resp of
Right msg -> print $ "created message: " <> show msg
Left (RestCallInternalErrorCode _code _status _body) -> print "4XX style http error code"
Left (RestCallInternalHttpException _err) -> print "http exception (likely no connection)"
Left (RestCallInternalNoParse _err _jsondata) -> print "can't parse return JSON"
-- CLEANUP
killThread printThreadId
killThread restThreadId
where
isTextChannel :: Channel -> Bool
isTextChannel ChannelText {} = True
isTextChannel _ = False