{-# OPTIONS_GHC -Wno-name-shadowing #-}

-- | The client
module Calamity.Client.Client (
  react,
  runBotIO,
  runBotIO',
  runBotIO'',
  stopBot,
  sendPresence,
  events,
  fire,
  waitUntil,
  waitUntilM,
  CalamityEvent (Dispatch, ShutDown),
  customEvt,
) where

import Calamity.Cache.Eff
import Calamity.Client.ShardManager
import Calamity.Client.Types
import Calamity.Gateway.DispatchEvents
import Calamity.Gateway.Intents
import Calamity.Gateway.Types
import Calamity.HTTP.Internal.Ratelimit
import Calamity.Types.TokenEff
import Calamity.Internal.ConstructorName
import Calamity.Internal.RunIntoIO
import qualified Calamity.Internal.SnowflakeMap as SM
import Calamity.Internal.UnixTimestamp
import Calamity.Internal.Updateable
import Calamity.Internal.Utils
import Calamity.Metrics.Eff
import Calamity.Types.LogEff
import Calamity.Types.Model.Channel
import Calamity.Types.Model.Guild
import Calamity.Types.Model.Presence (Presence (..))
import Calamity.Types.Model.User
import qualified Calamity.Types.Model.Voice as V
import Calamity.Types.Snowflake
import Calamity.Types.Token
import Control.Concurrent.Chan.Unagi
import Control.Concurrent.MVar
import Control.Concurrent.STM
import Control.Exception (SomeException)
import Control.Lens hiding ((<.>))
import Control.Monad
import Data.Default.Class
import Data.Dynamic
import Data.Foldable
import Data.Generics.Product.Subtype
import Data.IORef
import Data.Maybe
import Data.Proxy
import qualified Data.Text as T
import Data.Time.Clock.POSIX
import qualified Df1
import qualified Di.Core as DC
import qualified DiPolysemy as Di
import qualified Polysemy as P
import qualified Polysemy.Async as P
import qualified Polysemy.AtomicState as P
import qualified Polysemy.Error as P
import qualified Polysemy.Fail as P
import qualified Polysemy.Reader as P
import qualified Polysemy.Resource as P
import PyF
import TextShow (TextShow (showt))

timeA :: P.Member (P.Embed IO) r => P.Sem r a -> P.Sem r (Double, a)
timeA :: Sem r a -> Sem r (Double, a)
timeA Sem r a
m = do
  POSIXTime
start <- IO POSIXTime -> Sem r POSIXTime
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed IO POSIXTime
getPOSIXTime
  a
res <- Sem r a
m
  POSIXTime
end <- IO POSIXTime -> Sem r POSIXTime
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed IO POSIXTime
getPOSIXTime
  let duration :: Double
duration = Rational -> Double
forall a. Fractional a => Rational -> a
fromRational (Rational -> Double)
-> (POSIXTime -> Rational) -> POSIXTime -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. POSIXTime -> Rational
forall a. Real a => a -> Rational
toRational (POSIXTime -> Double) -> POSIXTime -> Double
forall a b. (a -> b) -> a -> b
$ POSIXTime
end POSIXTime -> POSIXTime -> POSIXTime
forall a. Num a => a -> a -> a
- POSIXTime
start
  (Double, a) -> Sem r (Double, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Double
duration, a
res)

newClient :: Token -> Maybe (DC.Di Df1.Level Df1.Path Df1.Message) -> IO Client
newClient :: Token -> Maybe (Di Level Path Message) -> IO Client
newClient Token
token Maybe (Di Level Path Message)
initialDi = do
  TVar [(InChan ControlMessage, Async (Maybe ()))]
shards' <- [(InChan ControlMessage, Async (Maybe ()))]
-> IO (TVar [(InChan ControlMessage, Async (Maybe ()))])
forall a. a -> IO (TVar a)
newTVarIO []
  MVar Int
numShards' <- IO (MVar Int)
forall a. IO (MVar a)
newEmptyMVar
  RateLimitState
rlState' <- IO RateLimitState
newRateLimitState
  (InChan CalamityEvent
inc, OutChan CalamityEvent
outc) <- IO (InChan CalamityEvent, OutChan CalamityEvent)
forall a. IO (InChan a, OutChan a)
newChan
  IORef Integer
ehidCounter <- Integer -> IO (IORef Integer)
forall a. a -> IO (IORef a)
newIORef Integer
0

  Client -> IO Client
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Client -> IO Client) -> Client -> IO Client
forall a b. (a -> b) -> a -> b
$
    TVar [(InChan ControlMessage, Async (Maybe ()))]
-> MVar Int
-> Token
-> RateLimitState
-> InChan CalamityEvent
-> OutChan CalamityEvent
-> IORef Integer
-> Maybe (Di Level Path Message)
-> Client
Client
      TVar [(InChan ControlMessage, Async (Maybe ()))]
shards'
      MVar Int
numShards'
      Token
token
      RateLimitState
rlState'
      InChan CalamityEvent
inc
      OutChan CalamityEvent
outc
      IORef Integer
ehidCounter
      Maybe (Di Level Path Message)
initialDi

-- | Create a bot, run your setup action, and then loop until the bot closes.
runBotIO ::
  forall r a.
  (P.Members '[P.Embed IO, P.Final IO, CacheEff, MetricEff, LogEff] r) =>
  Token ->
  -- | The intents the bot should use
  Intents ->
  P.Sem (SetupEff r) a ->
  P.Sem r (Maybe StartupError)
runBotIO :: Token
-> Intents -> Sem (SetupEff r) a -> Sem r (Maybe StartupError)
runBotIO Token
token Intents
intents = Token
-> Intents
-> Maybe StatusUpdateData
-> Sem (SetupEff r) a
-> Sem r (Maybe StartupError)
forall (r :: EffectRow) a.
Members '[Embed IO, Final IO, CacheEff, MetricEff, LogEff] r =>
Token
-> Intents
-> Maybe StatusUpdateData
-> Sem (SetupEff r) a
-> Sem r (Maybe StartupError)
runBotIO' Token
token Intents
intents Maybe StatusUpdateData
forall a. Maybe a
Nothing

resetDi :: BotC r => P.Sem r a -> P.Sem r a
resetDi :: Sem r a -> Sem r a
resetDi Sem r a
m = do
  Maybe (Di Level Path Message)
initialDi <- (Client -> Maybe (Di Level Path Message))
-> Sem r (Maybe (Di Level Path Message))
forall i j (r :: EffectRow).
Member (Reader i) r =>
(i -> j) -> Sem r j
P.asks (Client
-> Getting
     (Maybe (Di Level Path Message))
     Client
     (Maybe (Di Level Path Message))
-> Maybe (Di Level Path Message)
forall s a. s -> Getting a s a -> a
^. IsLabel
  "initialDi"
  (Getting
     (Maybe (Di Level Path Message))
     Client
     (Maybe (Di Level Path Message)))
Getting
  (Maybe (Di Level Path Message))
  Client
  (Maybe (Di Level Path Message))
#initialDi)
  (Di Level Path Message -> Di Level Path Message)
-> Sem r a -> Sem r a
forall level path msg (r :: EffectRow) a.
Member (Di level path msg) r =>
(Di level path msg -> Di level path msg) -> Sem r a -> Sem r a
Di.local (Di Level Path Message
-> Maybe (Di Level Path Message) -> Di Level Path Message
forall a. a -> Maybe a -> a
`fromMaybe` Maybe (Di Level Path Message)
initialDi) Sem r a
m

interpretRatelimitViaClient :: P.Member (P.Reader Client) r => P.Sem (RatelimitEff ': r) a -> P.Sem r a
interpretRatelimitViaClient :: Sem (RatelimitEff : r) a -> Sem r a
interpretRatelimitViaClient =
  (forall (rInitial :: EffectRow) x.
 RatelimitEff (Sem rInitial) x -> Sem r x)
-> Sem (RatelimitEff : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
P.interpret
    ( \case
        RatelimitEff (Sem rInitial) x
GetRatelimitState -> (Client -> x) -> Sem r x
forall i j (r :: EffectRow).
Member (Reader i) r =>
(i -> j) -> Sem r j
P.asks (Client -> Getting x Client x -> x
forall s a. s -> Getting a s a -> a
^. IsLabel "rlState" (Getting x Client x)
Getting x Client x
#rlState)
    )

interpretTokenViaClient :: P.Member (P.Reader Client) r => P.Sem (TokenEff ': r) a -> P.Sem r a
interpretTokenViaClient :: Sem (TokenEff : r) a -> Sem r a
interpretTokenViaClient =
  (forall (rInitial :: EffectRow) x.
 TokenEff (Sem rInitial) x -> Sem r x)
-> Sem (TokenEff : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
P.interpret
    ( \case
        TokenEff (Sem rInitial) x
GetBotToken -> (Client -> x) -> Sem r x
forall i j (r :: EffectRow).
Member (Reader i) r =>
(i -> j) -> Sem r j
P.asks (Client -> Getting x Client x -> x
forall s a. s -> Getting a s a -> a
^. IsLabel "token" (Getting x Client x)
Getting x Client x
#token)
    )

{- | Create a bot, run your setup action, and then loop until the bot closes.

 This version allows you to specify the initial status
-}
runBotIO' ::
  forall r a.
  (P.Members '[P.Embed IO, P.Final IO, CacheEff, MetricEff, LogEff] r) =>
  Token ->
  -- | The intents the bot should use
  Intents ->
  -- | The initial status to send to the gateway
  Maybe StatusUpdateData ->
  P.Sem (SetupEff r) a ->
  P.Sem r (Maybe StartupError)
runBotIO' :: Token
-> Intents
-> Maybe StatusUpdateData
-> Sem (SetupEff r) a
-> Sem r (Maybe StartupError)
runBotIO' Token
token Intents
intents Maybe StatusUpdateData
status Sem (SetupEff r) a
setup = do
  Maybe (Di Level Path Message)
initialDi <- Sem r (Maybe (Di Level Path Message))
forall level path msg (r :: EffectRow).
Member (Di level path msg) r =>
Sem r (Maybe (Di level path msg))
Di.fetch
  Client
client <- IO Client -> Sem r Client
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO Client -> Sem r Client) -> IO Client -> Sem r Client
forall a b. (a -> b) -> a -> b
$ Token -> Maybe (Di Level Path Message) -> IO Client
newClient Token
token Maybe (Di Level Path Message)
initialDi
  TVar EventHandlers
handlers <- IO (TVar EventHandlers) -> Sem r (TVar EventHandlers)
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO (TVar EventHandlers) -> Sem r (TVar EventHandlers))
-> IO (TVar EventHandlers) -> Sem r (TVar EventHandlers)
forall a b. (a -> b) -> a -> b
$ EventHandlers -> IO (TVar EventHandlers)
forall a. a -> IO (TVar a)
newTVarIO EventHandlers
forall a. Default a => a
def
  Sem (Async : r) (Maybe StartupError) -> Sem r (Maybe StartupError)
forall (r :: EffectRow) a.
Member (Final IO) r =>
Sem (Async : r) a -> Sem r a
P.asyncToIOFinal (Sem (Async : r) (Maybe StartupError)
 -> Sem r (Maybe StartupError))
-> (Sem (SetupEff r) (Maybe StartupError)
    -> Sem (Async : r) (Maybe StartupError))
-> Sem (SetupEff r) (Maybe StartupError)
-> Sem r (Maybe StartupError)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar EventHandlers
-> Sem (AtomicState EventHandlers : Async : r) (Maybe StartupError)
-> Sem (Async : r) (Maybe StartupError)
forall (r :: EffectRow) s a.
Member (Embed IO) r =>
TVar s -> Sem (AtomicState s : r) a -> Sem r a
P.runAtomicStateTVar TVar EventHandlers
handlers (Sem (AtomicState EventHandlers : Async : r) (Maybe StartupError)
 -> Sem (Async : r) (Maybe StartupError))
-> (Sem (SetupEff r) (Maybe StartupError)
    -> Sem
         (AtomicState EventHandlers : Async : r) (Maybe StartupError))
-> Sem (SetupEff r) (Maybe StartupError)
-> Sem (Async : r) (Maybe StartupError)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Client
-> Sem
     (Reader Client : AtomicState EventHandlers : Async : r)
     (Maybe StartupError)
-> Sem (AtomicState EventHandlers : Async : r) (Maybe StartupError)
forall i (r :: EffectRow) a. i -> Sem (Reader i : r) a -> Sem r a
P.runReader Client
client (Sem
   (Reader Client : AtomicState EventHandlers : Async : r)
   (Maybe StartupError)
 -> Sem
      (AtomicState EventHandlers : Async : r) (Maybe StartupError))
-> (Sem (SetupEff r) (Maybe StartupError)
    -> Sem
         (Reader Client : AtomicState EventHandlers : Async : r)
         (Maybe StartupError))
-> Sem (SetupEff r) (Maybe StartupError)
-> Sem (AtomicState EventHandlers : Async : r) (Maybe StartupError)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem
  (TokenEff : Reader Client : AtomicState EventHandlers : Async : r)
  (Maybe StartupError)
-> Sem
     (Reader Client : AtomicState EventHandlers : Async : r)
     (Maybe StartupError)
forall (r :: EffectRow) a.
Member (Reader Client) r =>
Sem (TokenEff : r) a -> Sem r a
interpretTokenViaClient (Sem
   (TokenEff : Reader Client : AtomicState EventHandlers : Async : r)
   (Maybe StartupError)
 -> Sem
      (Reader Client : AtomicState EventHandlers : Async : r)
      (Maybe StartupError))
-> (Sem (SetupEff r) (Maybe StartupError)
    -> Sem
         (TokenEff : Reader Client : AtomicState EventHandlers : Async : r)
         (Maybe StartupError))
-> Sem (SetupEff r) (Maybe StartupError)
-> Sem
     (Reader Client : AtomicState EventHandlers : Async : r)
     (Maybe StartupError)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem (SetupEff r) (Maybe StartupError)
-> Sem
     (TokenEff : Reader Client : AtomicState EventHandlers : Async : r)
     (Maybe StartupError)
forall (r :: EffectRow) a.
Member (Reader Client) r =>
Sem (RatelimitEff : r) a -> Sem r a
interpretRatelimitViaClient (Sem (SetupEff r) (Maybe StartupError)
 -> Sem
      (TokenEff : Reader Client : AtomicState EventHandlers : Async : r)
      (Maybe StartupError))
-> (Sem (SetupEff r) (Maybe StartupError)
    -> Sem (SetupEff r) (Maybe StartupError))
-> Sem (SetupEff r) (Maybe StartupError)
-> Sem
     (TokenEff : Reader Client : AtomicState EventHandlers : Async : r)
     (Maybe StartupError)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Segment
-> Sem (SetupEff r) (Maybe StartupError)
-> Sem (SetupEff r) (Maybe StartupError)
forall level msg (r :: EffectRow) a.
Member (Di level Path msg) r =>
Segment -> Sem r a -> Sem r a
Di.push Segment
"calamity" (Sem (SetupEff r) (Maybe StartupError)
 -> Sem r (Maybe StartupError))
-> Sem (SetupEff r) (Maybe StartupError)
-> Sem r (Maybe StartupError)
forall a b. (a -> b) -> a -> b
$ do
    Sem (SetupEff r) a -> Sem (SetupEff r) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Sem (SetupEff r) a -> Sem (SetupEff r) ())
-> Sem (SetupEff r) a -> Sem (SetupEff r) ()
forall a b. (a -> b) -> a -> b
$ Segment -> Sem (SetupEff r) a -> Sem (SetupEff r) a
forall level msg (r :: EffectRow) a.
Member (Di level Path msg) r =>
Segment -> Sem r a -> Sem r a
Di.push Segment
"calamity-setup" Sem (SetupEff r) a
setup
    Either StartupError ()
r <- Maybe StatusUpdateData
-> Intents -> Sem (SetupEff r) (Either StartupError ())
forall (r :: EffectRow).
BotC r =>
Maybe StatusUpdateData -> Intents -> Sem r (Either StartupError ())
shardBot Maybe StatusUpdateData
status Intents
intents
    case Either StartupError ()
r of
      Left StartupError
e -> Maybe StartupError -> Sem (SetupEff r) (Maybe StartupError)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StartupError -> Maybe StartupError
forall a. a -> Maybe a
Just StartupError
e)
      Right ()
_ -> do
        Segment -> Sem (SetupEff r) () -> Sem (SetupEff r) ()
forall level msg (r :: EffectRow) a.
Member (Di level Path msg) r =>
Segment -> Sem r a -> Sem r a
Di.push Segment
"calamity-loop" Sem (SetupEff r) ()
forall (r :: EffectRow). BotC r => Sem r ()
clientLoop
        Segment -> Sem (SetupEff r) () -> Sem (SetupEff r) ()
forall level msg (r :: EffectRow) a.
Member (Di level Path msg) r =>
Segment -> Sem r a -> Sem r a
Di.push Segment
"calamity-stop" Sem (SetupEff r) ()
forall (r :: EffectRow). BotC r => Sem r ()
finishUp
        Maybe StartupError -> Sem (SetupEff r) (Maybe StartupError)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe StartupError
forall a. Maybe a
Nothing

{- | Create a bot, run your setup action, and then loop until the bot closes.

 This version only handles the @'P.Reader' 'Client'@ effect, allowing you to
 handle the @'P.AtomicState' 'EventHandlers'@ yourself.
-}
runBotIO'' ::
  forall r a.
  ( P.Members
      '[ LogEff
       , MetricEff
       , CacheEff
       , P.Reader Client
       , P.AtomicState EventHandlers
       , P.Embed IO
       , P.Final IO
       , P.Async
       ]
      r
  ) =>
  Token ->
  -- | The intents the bot should use
  Intents ->
  -- | The initial status to send to the gateway
  Maybe StatusUpdateData ->
  P.Sem (RatelimitEff ': TokenEff ': P.Reader Client ': r) a ->
  P.Sem r (Maybe StartupError)
runBotIO'' :: Token
-> Intents
-> Maybe StatusUpdateData
-> Sem (RatelimitEff : TokenEff : Reader Client : r) a
-> Sem r (Maybe StartupError)
runBotIO'' Token
token Intents
intents Maybe StatusUpdateData
status Sem (RatelimitEff : TokenEff : Reader Client : r) a
setup = do
  Maybe (Di Level Path Message)
initialDi <- Sem r (Maybe (Di Level Path Message))
forall level path msg (r :: EffectRow).
Member (Di level path msg) r =>
Sem r (Maybe (Di level path msg))
Di.fetch
  Client
client <- IO Client -> Sem r Client
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO Client -> Sem r Client) -> IO Client -> Sem r Client
forall a b. (a -> b) -> a -> b
$ Token -> Maybe (Di Level Path Message) -> IO Client
newClient Token
token Maybe (Di Level Path Message)
initialDi
  Client
-> Sem (Reader Client : r) (Maybe StartupError)
-> Sem r (Maybe StartupError)
forall i (r :: EffectRow) a. i -> Sem (Reader i : r) a -> Sem r a
P.runReader Client
client (Sem (Reader Client : r) (Maybe StartupError)
 -> Sem r (Maybe StartupError))
-> (Sem
      (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
    -> Sem (Reader Client : r) (Maybe StartupError))
-> Sem
     (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
-> Sem r (Maybe StartupError)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem (TokenEff : Reader Client : r) (Maybe StartupError)
-> Sem (Reader Client : r) (Maybe StartupError)
forall (r :: EffectRow) a.
Member (Reader Client) r =>
Sem (TokenEff : r) a -> Sem r a
interpretTokenViaClient (Sem (TokenEff : Reader Client : r) (Maybe StartupError)
 -> Sem (Reader Client : r) (Maybe StartupError))
-> (Sem
      (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
    -> Sem (TokenEff : Reader Client : r) (Maybe StartupError))
-> Sem
     (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
-> Sem (Reader Client : r) (Maybe StartupError)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem
  (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
-> Sem (TokenEff : Reader Client : r) (Maybe StartupError)
forall (r :: EffectRow) a.
Member (Reader Client) r =>
Sem (RatelimitEff : r) a -> Sem r a
interpretRatelimitViaClient (Sem
   (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
 -> Sem (TokenEff : Reader Client : r) (Maybe StartupError))
-> (Sem
      (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
    -> Sem
         (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError))
-> Sem
     (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
-> Sem (TokenEff : Reader Client : r) (Maybe StartupError)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Segment
-> Sem
     (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
-> Sem
     (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
forall level msg (r :: EffectRow) a.
Member (Di level Path msg) r =>
Segment -> Sem r a -> Sem r a
Di.push Segment
"calamity" (Sem
   (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
 -> Sem r (Maybe StartupError))
-> Sem
     (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
-> Sem r (Maybe StartupError)
forall a b. (a -> b) -> a -> b
$ do
    Sem (RatelimitEff : TokenEff : Reader Client : r) a
-> Sem (RatelimitEff : TokenEff : Reader Client : r) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Sem (RatelimitEff : TokenEff : Reader Client : r) a
 -> Sem (RatelimitEff : TokenEff : Reader Client : r) ())
-> Sem (RatelimitEff : TokenEff : Reader Client : r) a
-> Sem (RatelimitEff : TokenEff : Reader Client : r) ()
forall a b. (a -> b) -> a -> b
$ Segment
-> Sem (RatelimitEff : TokenEff : Reader Client : r) a
-> Sem (RatelimitEff : TokenEff : Reader Client : r) a
forall level msg (r :: EffectRow) a.
Member (Di level Path msg) r =>
Segment -> Sem r a -> Sem r a
Di.push Segment
"calamity-setup" Sem (RatelimitEff : TokenEff : Reader Client : r) a
setup
    Either StartupError ()
r <- Maybe StatusUpdateData
-> Intents
-> Sem
     (RatelimitEff : TokenEff : Reader Client : r)
     (Either StartupError ())
forall (r :: EffectRow).
BotC r =>
Maybe StatusUpdateData -> Intents -> Sem r (Either StartupError ())
shardBot Maybe StatusUpdateData
status Intents
intents
    case Either StartupError ()
r of
      Left StartupError
e -> Maybe StartupError
-> Sem
     (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StartupError -> Maybe StartupError
forall a. a -> Maybe a
Just StartupError
e)
      Right ()
_ -> do
        Segment
-> Sem (RatelimitEff : TokenEff : Reader Client : r) ()
-> Sem (RatelimitEff : TokenEff : Reader Client : r) ()
forall level msg (r :: EffectRow) a.
Member (Di level Path msg) r =>
Segment -> Sem r a -> Sem r a
Di.push Segment
"calamity-loop" Sem (RatelimitEff : TokenEff : Reader Client : r) ()
forall (r :: EffectRow). BotC r => Sem r ()
clientLoop
        Segment
-> Sem (RatelimitEff : TokenEff : Reader Client : r) ()
-> Sem (RatelimitEff : TokenEff : Reader Client : r) ()
forall level msg (r :: EffectRow) a.
Member (Di level Path msg) r =>
Segment -> Sem r a -> Sem r a
Di.push Segment
"calamity-stop" Sem (RatelimitEff : TokenEff : Reader Client : r) ()
forall (r :: EffectRow). BotC r => Sem r ()
finishUp
        Maybe StartupError
-> Sem
     (RatelimitEff : TokenEff : Reader Client : r) (Maybe StartupError)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe StartupError
forall a. Maybe a
Nothing

{- | Register an event handler, returning an action that removes the event handler from the bot.

 Refer to 'EventType' for what events you can register, and 'EHType' for the
 parameters the event handlers they receive.

 You'll probably want @TypeApplications@ and need @DataKinds@ enabled to
 specify the type of @s@.

 ==== Examples

 Reacting to every message:

 @
 'react' @\''MessageCreateEvt' '$' \msg -> 'print' '$' "Got message: " '<>' 'show' msg
 @

 Reacting to a custom event:

 @
 data MyCustomEvt = MyCustomEvt 'Data.Text.Text' 'Message'

 'react' @(\''CustomEvt' MyCustomEvt) $ \\(MyCustomEvt s m) ->
    'void' $ 'Calamity.Types.Tellable.tell' @'Data.Text.Text' m ("Somebody told me to tell you about: " '<>' s)
 @

 ==== Notes

 This function is pretty bad for giving nasty type errors,
 since if something doesn't match then 'EHType' might not get substituted,
 which will result in errors about parameter counts mismatching.
-}
react ::
  forall (s :: EventType) r.
  (BotC r, ReactConstraints s) =>
  (EHType s -> (P.Sem r) ()) ->
  P.Sem r (P.Sem r ())
react :: (EHType s -> Sem r ()) -> Sem r (Sem r ())
react EHType s -> Sem r ()
handler = do
  EHType s -> IO (Maybe ())
handler' <- (EHType s -> Sem r ()) -> Sem r (EHType s -> IO (Maybe ()))
forall (r :: EffectRow) p a.
Member (Final IO) r =>
(p -> Sem r a) -> Sem r (p -> IO (Maybe a))
bindSemToIO EHType s -> Sem r ()
handler
  IORef Integer
ehidC <- (Client -> IORef Integer) -> Sem r (IORef Integer)
forall i j (r :: EffectRow).
Member (Reader i) r =>
(i -> j) -> Sem r j
P.asks (Client
-> Getting (IORef Integer) Client (IORef Integer) -> IORef Integer
forall s a. s -> Getting a s a -> a
^. IsLabel
  "ehidCounter" (Getting (IORef Integer) Client (IORef Integer))
Getting (IORef Integer) Client (IORef Integer)
#ehidCounter)
  Integer
id' <- IO Integer -> Sem r Integer
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO Integer -> Sem r Integer) -> IO Integer -> Sem r Integer
forall a b. (a -> b) -> a -> b
$ IORef Integer -> (Integer -> (Integer, Integer)) -> IO Integer
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef IORef Integer
ehidC (\Integer
i -> (Integer
i Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1, Integer
i))
  let handlers :: EventHandlers
handlers = Proxy s -> Integer -> StoredEHType s -> EventHandlers
forall (a :: EventType).
InsertEventHandler a =>
Proxy a -> Integer -> StoredEHType a -> EventHandlers
makeEventHandlers (Proxy s
forall k (t :: k). Proxy t
Proxy @s) Integer
id' (() -> Maybe () -> ()
forall a b. a -> b -> a
const () (Maybe () -> ()) -> (EHType s -> IO (Maybe ())) -> StoredEHType s
forall (f :: * -> *) a b c.
Functor f =>
(a -> b) -> (c -> f a) -> c -> f b
<.> EHType s -> IO (Maybe ())
handler')
  (EventHandlers -> EventHandlers) -> Sem r ()
forall s (r :: EffectRow).
Member (AtomicState s) r =>
(s -> s) -> Sem r ()
P.atomicModify (EventHandlers
handlers EventHandlers -> EventHandlers -> EventHandlers
forall a. Semigroup a => a -> a -> a
<>)
  Sem r () -> Sem r (Sem r ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Sem r () -> Sem r (Sem r ())) -> Sem r () -> Sem r (Sem r ())
forall a b. (a -> b) -> a -> b
$ Integer -> Sem r ()
forall (s :: EventType) (r :: EffectRow).
(BotC r, RemoveEventHandler s) =>
Integer -> Sem r ()
removeHandler @s Integer
id'

removeHandler :: forall (s :: EventType) r. (BotC r, RemoveEventHandler s) => Integer -> P.Sem r ()
removeHandler :: Integer -> Sem r ()
removeHandler Integer
id' = (EventHandlers -> EventHandlers) -> Sem r ()
forall s (r :: EffectRow).
Member (AtomicState s) r =>
(s -> s) -> Sem r ()
P.atomicModify (Proxy s -> Integer -> EventHandlers -> EventHandlers
forall k (a :: k).
RemoveEventHandler a =>
Proxy a -> Integer -> EventHandlers -> EventHandlers
removeEventHandler (Proxy s
forall k (t :: k). Proxy t
Proxy @s) Integer
id')

{- | Fire an event that the bot will then handle.

 ==== Examples

 Firing an event named \"my-event\":

 @
 'fire' '$' 'customEvt' @"my-event" ("aha" :: 'Data.Text.Text', msg)
 @
-}
fire :: BotC r => CalamityEvent -> P.Sem r ()
fire :: CalamityEvent -> Sem r ()
fire CalamityEvent
e = do
  InChan CalamityEvent
inc <- (Client -> InChan CalamityEvent) -> Sem r (InChan CalamityEvent)
forall i j (r :: EffectRow).
Member (Reader i) r =>
(i -> j) -> Sem r j
P.asks (Client
-> Getting (InChan CalamityEvent) Client (InChan CalamityEvent)
-> InChan CalamityEvent
forall s a. s -> Getting a s a -> a
^. IsLabel
  "eventsIn"
  (Getting (InChan CalamityEvent) Client (InChan CalamityEvent))
Getting (InChan CalamityEvent) Client (InChan CalamityEvent)
#eventsIn)
  IO () -> Sem r ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO () -> Sem r ()) -> IO () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ InChan CalamityEvent -> CalamityEvent -> IO ()
forall a. InChan a -> a -> IO ()
writeChan InChan CalamityEvent
inc CalamityEvent
e

{- | Build a Custom CalamityEvent

 The type of @a@ must match up with the event handler you want to receive it.

 ==== Examples

 @
 'customEvt' (MyCustomEvent "lol")
 @
-}
customEvt :: forall a. Typeable a => a -> CalamityEvent
customEvt :: a -> CalamityEvent
customEvt = a -> CalamityEvent
forall a. Typeable a => a -> CalamityEvent
Custom

-- | Get a copy of the event stream.
events :: BotC r => P.Sem r (OutChan CalamityEvent)
events :: Sem r (OutChan CalamityEvent)
events = do
  InChan CalamityEvent
inc <- (Client -> InChan CalamityEvent) -> Sem r (InChan CalamityEvent)
forall i j (r :: EffectRow).
Member (Reader i) r =>
(i -> j) -> Sem r j
P.asks (Client
-> Getting (InChan CalamityEvent) Client (InChan CalamityEvent)
-> InChan CalamityEvent
forall s a. s -> Getting a s a -> a
^. IsLabel
  "eventsIn"
  (Getting (InChan CalamityEvent) Client (InChan CalamityEvent))
Getting (InChan CalamityEvent) Client (InChan CalamityEvent)
#eventsIn)
  IO (OutChan CalamityEvent) -> Sem r (OutChan CalamityEvent)
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO (OutChan CalamityEvent) -> Sem r (OutChan CalamityEvent))
-> IO (OutChan CalamityEvent) -> Sem r (OutChan CalamityEvent)
forall a b. (a -> b) -> a -> b
$ InChan CalamityEvent -> IO (OutChan CalamityEvent)
forall a. InChan a -> IO (OutChan a)
dupChan InChan CalamityEvent
inc

{- | Wait until an event satisfying a condition happens, then returns its
 parameters.

 The check function for this command is pure unlike 'waitUntilM'

 This is what it would look like with @s ~ \''MessageCreateEvt'@:

 @
 'waitUntil' :: ('Message' -> 'Bool') -> 'P.Sem' r 'Message'
 @

 And for @s ~ \''MessageUpdateEvt'@:

 @
 'waitUntil' :: (('Message', 'Message') -> 'Bool') -> 'P.Sem' r ('Message', 'Message')
 @

 ==== Examples

 Waiting for a message containing the text \"hi\":

 @
 f = do msg \<\- 'waitUntil' @\''MessageCreateEvt' (\\m -> 'Data.Text.isInfixOf' "hi" $ m ^. #content)
        print $ msg ^. #content
 @
-}
waitUntil ::
  forall (s :: EventType) r.
  (BotC r, ReactConstraints s) =>
  (EHType s -> Bool) ->
  P.Sem r (EHType s)
waitUntil :: (EHType s -> Bool) -> Sem r (EHType s)
waitUntil EHType s -> Bool
f = Sem (Resource : r) (EHType s) -> Sem r (EHType s)
forall (r :: EffectRow) a.
Member (Final IO) r =>
Sem (Resource : r) a -> Sem r a
P.resourceToIOFinal (Sem (Resource : r) (EHType s) -> Sem r (EHType s))
-> Sem (Resource : r) (EHType s) -> Sem r (EHType s)
forall a b. (a -> b) -> a -> b
$ do
  MVar (EHType s)
result <- IO (MVar (EHType s)) -> Sem (Resource : r) (MVar (EHType s))
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed IO (MVar (EHType s))
forall a. IO (MVar a)
newEmptyMVar
  Sem (Resource : r) (Sem r ())
-> (Sem r () -> Sem (Resource : r) ())
-> (Sem r () -> Sem (Resource : r) (EHType s))
-> Sem (Resource : r) (EHType s)
forall (r :: EffectRow) a c b.
Member Resource r =>
Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b
P.bracket
    (Sem r (Sem r ()) -> Sem (Resource : r) (Sem r ())
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
P.raise (Sem r (Sem r ()) -> Sem (Resource : r) (Sem r ()))
-> Sem r (Sem r ()) -> Sem (Resource : r) (Sem r ())
forall a b. (a -> b) -> a -> b
$ (EHType s -> Sem r ()) -> Sem r (Sem r ())
forall (s :: EventType) (r :: EffectRow).
(BotC r, ReactConstraints s) =>
(EHType s -> Sem r ()) -> Sem r (Sem r ())
react @s (MVar (EHType s) -> EHType s -> Sem r ()
checker MVar (EHType s)
result))
    Sem r () -> Sem (Resource : r) ()
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
P.raise
    (Sem (Resource : r) (EHType s)
-> Sem r () -> Sem (Resource : r) (EHType s)
forall a b. a -> b -> a
const (Sem (Resource : r) (EHType s)
 -> Sem r () -> Sem (Resource : r) (EHType s))
-> (IO (EHType s) -> Sem (Resource : r) (EHType s))
-> IO (EHType s)
-> Sem r ()
-> Sem (Resource : r) (EHType s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (EHType s) -> Sem (Resource : r) (EHType s)
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO (EHType s) -> Sem r () -> Sem (Resource : r) (EHType s))
-> IO (EHType s) -> Sem r () -> Sem (Resource : r) (EHType s)
forall a b. (a -> b) -> a -> b
$ MVar (EHType s) -> IO (EHType s)
forall a. MVar a -> IO a
takeMVar MVar (EHType s)
result)
  where
    checker :: MVar (EHType s) -> EHType s -> P.Sem r ()
    checker :: MVar (EHType s) -> EHType s -> Sem r ()
checker MVar (EHType s)
result EHType s
args = do
      Bool -> Sem r () -> Sem r ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (EHType s -> Bool
f EHType s
args) (Sem r () -> Sem r ()) -> Sem r () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ do
        IO () -> Sem r ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO () -> Sem r ()) -> IO () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ MVar (EHType s) -> EHType s -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (EHType s)
result EHType s
args

{- | Wait until an event satisfying a condition happens, then returns its
 parameters

 This is what it would look like with @s ~ \''MessageCreateEvt'@:

 @
 'waitUntilM' :: ('Message' -> 'P.Sem' r 'Bool') -> 'P.Sem' r 'Message'
 @

 And for @s ~ \''MessageUpdateEvt'@:

 @
 'waitUntilM' :: (('Message', 'Message') -> 'P.Sem' r 'Bool') -> 'P.Sem' r ('Message', 'Message')
 @

 ==== Examples

 Waiting for a message containing the text \"hi\":

 @
 f = do msg \<\- 'waitUntilM' @\''MessageCreateEvt' (\\m -> ('debug' $ "got message: " <> 'showt' msg) >> ('pure' $ 'Data.Text.isInfixOf' "hi" $ m ^. #content))
        print $ msg ^. #content
 @
-}
waitUntilM ::
  forall (s :: EventType) r.
  (BotC r, ReactConstraints s) =>
  (EHType s -> P.Sem r Bool) ->
  P.Sem r (EHType s)
waitUntilM :: (EHType s -> Sem r Bool) -> Sem r (EHType s)
waitUntilM EHType s -> Sem r Bool
f = Sem (Resource : r) (EHType s) -> Sem r (EHType s)
forall (r :: EffectRow) a.
Member (Final IO) r =>
Sem (Resource : r) a -> Sem r a
P.resourceToIOFinal (Sem (Resource : r) (EHType s) -> Sem r (EHType s))
-> Sem (Resource : r) (EHType s) -> Sem r (EHType s)
forall a b. (a -> b) -> a -> b
$ do
  MVar (EHType s)
result <- IO (MVar (EHType s)) -> Sem (Resource : r) (MVar (EHType s))
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed IO (MVar (EHType s))
forall a. IO (MVar a)
newEmptyMVar
  Sem (Resource : r) (Sem r ())
-> (Sem r () -> Sem (Resource : r) ())
-> (Sem r () -> Sem (Resource : r) (EHType s))
-> Sem (Resource : r) (EHType s)
forall (r :: EffectRow) a c b.
Member Resource r =>
Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b
P.bracket
    (Sem r (Sem r ()) -> Sem (Resource : r) (Sem r ())
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
P.raise (Sem r (Sem r ()) -> Sem (Resource : r) (Sem r ()))
-> Sem r (Sem r ()) -> Sem (Resource : r) (Sem r ())
forall a b. (a -> b) -> a -> b
$ (EHType s -> Sem r ()) -> Sem r (Sem r ())
forall (s :: EventType) (r :: EffectRow).
(BotC r, ReactConstraints s) =>
(EHType s -> Sem r ()) -> Sem r (Sem r ())
react @s (MVar (EHType s) -> EHType s -> Sem r ()
checker MVar (EHType s)
result))
    Sem r () -> Sem (Resource : r) ()
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
P.raise
    (Sem (Resource : r) (EHType s)
-> Sem r () -> Sem (Resource : r) (EHType s)
forall a b. a -> b -> a
const (Sem (Resource : r) (EHType s)
 -> Sem r () -> Sem (Resource : r) (EHType s))
-> (IO (EHType s) -> Sem (Resource : r) (EHType s))
-> IO (EHType s)
-> Sem r ()
-> Sem (Resource : r) (EHType s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (EHType s) -> Sem (Resource : r) (EHType s)
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO (EHType s) -> Sem r () -> Sem (Resource : r) (EHType s))
-> IO (EHType s) -> Sem r () -> Sem (Resource : r) (EHType s)
forall a b. (a -> b) -> a -> b
$ MVar (EHType s) -> IO (EHType s)
forall a. MVar a -> IO a
takeMVar MVar (EHType s)
result)
  where
    checker :: MVar (EHType s) -> EHType s -> P.Sem r ()
    checker :: MVar (EHType s) -> EHType s -> Sem r ()
checker MVar (EHType s)
result EHType s
args = do
      Bool
res <- EHType s -> Sem r Bool
f EHType s
args
      Bool -> Sem r () -> Sem r ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
res (Sem r () -> Sem r ()) -> Sem r () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ do
        IO () -> Sem r ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO () -> Sem r ()) -> IO () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ MVar (EHType s) -> EHType s -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (EHType s)
result EHType s
args

-- | Set the bot's presence on all shards.
sendPresence :: BotC r => StatusUpdateData -> P.Sem r ()
sendPresence :: StatusUpdateData -> Sem r ()
sendPresence StatusUpdateData
s = do
  [(InChan ControlMessage, Async (Maybe ()))]
shards <- (Client -> TVar [(InChan ControlMessage, Async (Maybe ()))])
-> Sem r (TVar [(InChan ControlMessage, Async (Maybe ()))])
forall i j (r :: EffectRow).
Member (Reader i) r =>
(i -> j) -> Sem r j
P.asks (Client
-> Getting
     (TVar [(InChan ControlMessage, Async (Maybe ()))])
     Client
     (TVar [(InChan ControlMessage, Async (Maybe ()))])
-> TVar [(InChan ControlMessage, Async (Maybe ()))]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "shards"
  (Getting
     (TVar [(InChan ControlMessage, Async (Maybe ()))])
     Client
     (TVar [(InChan ControlMessage, Async (Maybe ()))]))
Getting
  (TVar [(InChan ControlMessage, Async (Maybe ()))])
  Client
  (TVar [(InChan ControlMessage, Async (Maybe ()))])
#shards) Sem r (TVar [(InChan ControlMessage, Async (Maybe ()))])
-> (TVar [(InChan ControlMessage, Async (Maybe ()))]
    -> Sem r [(InChan ControlMessage, Async (Maybe ()))])
-> Sem r [(InChan ControlMessage, Async (Maybe ()))]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO [(InChan ControlMessage, Async (Maybe ()))]
-> Sem r [(InChan ControlMessage, Async (Maybe ()))]
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO [(InChan ControlMessage, Async (Maybe ()))]
 -> Sem r [(InChan ControlMessage, Async (Maybe ()))])
-> (TVar [(InChan ControlMessage, Async (Maybe ()))]
    -> IO [(InChan ControlMessage, Async (Maybe ()))])
-> TVar [(InChan ControlMessage, Async (Maybe ()))]
-> Sem r [(InChan ControlMessage, Async (Maybe ()))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar [(InChan ControlMessage, Async (Maybe ()))]
-> IO [(InChan ControlMessage, Async (Maybe ()))]
forall a. TVar a -> IO a
readTVarIO
  [(InChan ControlMessage, Async (Maybe ()))]
-> ((InChan ControlMessage, Async (Maybe ())) -> Sem r ())
-> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [(InChan ControlMessage, Async (Maybe ()))]
shards (((InChan ControlMessage, Async (Maybe ())) -> Sem r ())
 -> Sem r ())
-> ((InChan ControlMessage, Async (Maybe ())) -> Sem r ())
-> Sem r ()
forall a b. (a -> b) -> a -> b
$ \(InChan ControlMessage
inc, Async (Maybe ())
_) ->
    IO () -> Sem r ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO () -> Sem r ()) -> IO () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ InChan ControlMessage -> ControlMessage -> IO ()
forall a. InChan a -> a -> IO ()
writeChan InChan ControlMessage
inc (StatusUpdateData -> ControlMessage
SendPresence StatusUpdateData
s)

-- | Initiate shutting down the bot.
stopBot :: BotC r => P.Sem r ()
stopBot :: Sem r ()
stopBot = do
  Text -> Sem r ()
forall (r :: EffectRow). Member LogEff r => Text -> Sem r ()
debug Text
"stopping bot"
  InChan CalamityEvent
inc <- (Client -> InChan CalamityEvent) -> Sem r (InChan CalamityEvent)
forall i j (r :: EffectRow).
Member (Reader i) r =>
(i -> j) -> Sem r j
P.asks (Client
-> Getting (InChan CalamityEvent) Client (InChan CalamityEvent)
-> InChan CalamityEvent
forall s a. s -> Getting a s a -> a
^. IsLabel
  "eventsIn"
  (Getting (InChan CalamityEvent) Client (InChan CalamityEvent))
Getting (InChan CalamityEvent) Client (InChan CalamityEvent)
#eventsIn)
  IO () -> Sem r ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO () -> Sem r ()) -> IO () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ InChan CalamityEvent -> CalamityEvent -> IO ()
forall a. InChan a -> a -> IO ()
writeChan InChan CalamityEvent
inc CalamityEvent
ShutDown

finishUp :: BotC r => P.Sem r ()
finishUp :: Sem r ()
finishUp = do
  Text -> Sem r ()
forall (r :: EffectRow). Member LogEff r => Text -> Sem r ()
debug Text
"finishing up"
  [(InChan ControlMessage, Async (Maybe ()))]
shards <- (Client -> TVar [(InChan ControlMessage, Async (Maybe ()))])
-> Sem r (TVar [(InChan ControlMessage, Async (Maybe ()))])
forall i j (r :: EffectRow).
Member (Reader i) r =>
(i -> j) -> Sem r j
P.asks (Client
-> Getting
     (TVar [(InChan ControlMessage, Async (Maybe ()))])
     Client
     (TVar [(InChan ControlMessage, Async (Maybe ()))])
-> TVar [(InChan ControlMessage, Async (Maybe ()))]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "shards"
  (Getting
     (TVar [(InChan ControlMessage, Async (Maybe ()))])
     Client
     (TVar [(InChan ControlMessage, Async (Maybe ()))]))
Getting
  (TVar [(InChan ControlMessage, Async (Maybe ()))])
  Client
  (TVar [(InChan ControlMessage, Async (Maybe ()))])
#shards) Sem r (TVar [(InChan ControlMessage, Async (Maybe ()))])
-> (TVar [(InChan ControlMessage, Async (Maybe ()))]
    -> Sem r [(InChan ControlMessage, Async (Maybe ()))])
-> Sem r [(InChan ControlMessage, Async (Maybe ()))]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO [(InChan ControlMessage, Async (Maybe ()))]
-> Sem r [(InChan ControlMessage, Async (Maybe ()))]
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO [(InChan ControlMessage, Async (Maybe ()))]
 -> Sem r [(InChan ControlMessage, Async (Maybe ()))])
-> (TVar [(InChan ControlMessage, Async (Maybe ()))]
    -> IO [(InChan ControlMessage, Async (Maybe ()))])
-> TVar [(InChan ControlMessage, Async (Maybe ()))]
-> Sem r [(InChan ControlMessage, Async (Maybe ()))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar [(InChan ControlMessage, Async (Maybe ()))]
-> IO [(InChan ControlMessage, Async (Maybe ()))]
forall a. TVar a -> IO a
readTVarIO
  [(InChan ControlMessage, Async (Maybe ()))]
-> ((InChan ControlMessage, Async (Maybe ())) -> Sem r ())
-> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [(InChan ControlMessage, Async (Maybe ()))]
shards (((InChan ControlMessage, Async (Maybe ())) -> Sem r ())
 -> Sem r ())
-> ((InChan ControlMessage, Async (Maybe ())) -> Sem r ())
-> Sem r ()
forall a b. (a -> b) -> a -> b
$ \(InChan ControlMessage
inc, Async (Maybe ())
_) ->
    IO () -> Sem r ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO () -> Sem r ()) -> IO () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ InChan ControlMessage -> ControlMessage -> IO ()
forall a. InChan a -> a -> IO ()
writeChan InChan ControlMessage
inc ControlMessage
ShutDownShard
  [(InChan ControlMessage, Async (Maybe ()))]
-> ((InChan ControlMessage, Async (Maybe ())) -> Sem r (Maybe ()))
-> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [(InChan ControlMessage, Async (Maybe ()))]
shards (((InChan ControlMessage, Async (Maybe ())) -> Sem r (Maybe ()))
 -> Sem r ())
-> ((InChan ControlMessage, Async (Maybe ())) -> Sem r (Maybe ()))
-> Sem r ()
forall a b. (a -> b) -> a -> b
$ \(InChan ControlMessage
_, Async (Maybe ())
shardThread) -> Async (Maybe ()) -> Sem r (Maybe ())
forall (r :: EffectRow) a. Member Async r => Async a -> Sem r a
P.await Async (Maybe ())
shardThread
  Text -> Sem r ()
forall (r :: EffectRow). Member LogEff r => Text -> Sem r ()
debug Text
"bot has stopped"

{- | main loop of the client, handles fetching the next event, processing the
 event and invoking its handler functions
-}
clientLoop :: BotC r => P.Sem r ()
clientLoop :: Sem r ()
clientLoop = do
  OutChan CalamityEvent
outc <- (Client -> OutChan CalamityEvent) -> Sem r (OutChan CalamityEvent)
forall i j (r :: EffectRow).
Member (Reader i) r =>
(i -> j) -> Sem r j
P.asks (Client
-> Getting (OutChan CalamityEvent) Client (OutChan CalamityEvent)
-> OutChan CalamityEvent
forall s a. s -> Getting a s a -> a
^. IsLabel
  "eventsOut"
  (Getting (OutChan CalamityEvent) Client (OutChan CalamityEvent))
Getting (OutChan CalamityEvent) Client (OutChan CalamityEvent)
#eventsOut)
  Sem r Bool -> Sem r ()
forall (r :: EffectRow). Sem r Bool -> Sem r ()
whileMFinalIO (Sem r Bool -> Sem r ()) -> Sem r Bool -> Sem r ()
forall a b. (a -> b) -> a -> b
$ do
    !CalamityEvent
evt' <- IO CalamityEvent -> Sem r CalamityEvent
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO CalamityEvent -> Sem r CalamityEvent)
-> IO CalamityEvent -> Sem r CalamityEvent
forall a b. (a -> b) -> a -> b
$ OutChan CalamityEvent -> IO CalamityEvent
forall a. OutChan a -> IO a
readChan OutChan CalamityEvent
outc
    case CalamityEvent
evt' of
      Dispatch !Int
sid !DispatchData
evt -> Int -> DispatchData -> Sem r ()
forall (r :: EffectRow). BotC r => Int -> DispatchData -> Sem r ()
handleEvent Int
sid DispatchData
evt Sem r () -> Sem r Bool -> Sem r Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> Sem r Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
      Custom a
d -> a -> Sem r ()
forall a (r :: EffectRow). (Typeable a, BotC r) => a -> Sem r ()
handleCustomEvent a
d Sem r () -> Sem r Bool -> Sem r Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> Sem r Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
      CalamityEvent
ShutDown -> Bool -> Sem r Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
  Text -> Sem r ()
forall (r :: EffectRow). Member LogEff r => Text -> Sem r ()
debug Text
"leaving client loop"

handleCustomEvent :: forall a r. (Typeable a, BotC r) => a -> P.Sem r ()
handleCustomEvent :: a -> Sem r ()
handleCustomEvent a
d = do
  EventHandlers
eventHandlers <- Sem r EventHandlers
forall s (r :: EffectRow). Member (AtomicState s) r => Sem r s
P.atomicGet

  let handlers :: [a -> IO ()]
handlers = EventHandlers -> [a -> IO ()]
forall a. Typeable a => EventHandlers -> [a -> IO ()]
getCustomEventHandlers @a EventHandlers
eventHandlers

  [a -> IO ()]
-> ((a -> IO ()) -> Sem r (Async (Maybe ()))) -> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [a -> IO ()]
handlers (\a -> IO ()
h -> Sem r () -> Sem r (Async (Maybe ()))
forall (r :: EffectRow) a.
Member Async r =>
Sem r a -> Sem r (Async (Maybe a))
P.async (Sem r () -> Sem r (Async (Maybe ())))
-> (IO () -> Sem r ()) -> IO () -> Sem r (Async (Maybe ()))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO () -> Sem r ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO () -> Sem r (Async (Maybe ())))
-> IO () -> Sem r (Async (Maybe ()))
forall a b. (a -> b) -> a -> b
$ a -> IO ()
h a
d)

catchAllLogging :: BotC r => P.Sem r () -> P.Sem r ()
catchAllLogging :: Sem r () -> Sem r ()
catchAllLogging Sem r ()
m = do
  Either SomeException ()
r <- Sem (Error SomeException : r) () -> Sem r (Either SomeException ())
forall e (r :: EffectRow) a.
(Typeable e, Member (Final IO) r) =>
Sem (Error e : r) a -> Sem r (Either e a)
P.errorToIOFinal (Sem (Error SomeException : r) ()
 -> Sem r (Either SomeException ()))
-> (Sem (Error SomeException : r) ()
    -> Sem (Error SomeException : r) ())
-> Sem (Error SomeException : r) ()
-> Sem r (Either SomeException ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (r :: EffectRow) a.
(Exception SomeException, Member (Error SomeException) r,
 Member (Final IO) r) =>
Sem r a -> Sem r a
forall e (r :: EffectRow) a.
(Exception e, Member (Error e) r, Member (Final IO) r) =>
Sem r a -> Sem r a
P.fromExceptionSem @SomeException (Sem (Error SomeException : r) ()
 -> Sem r (Either SomeException ()))
-> Sem (Error SomeException : r) ()
-> Sem r (Either SomeException ())
forall a b. (a -> b) -> a -> b
$ Sem r () -> Sem (Error SomeException : r) ()
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
P.raise Sem r ()
m
  case Either SomeException ()
r of
    Right ()
_ -> () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    Left SomeException
e -> Text -> Sem r ()
forall (r :: EffectRow). Member LogEff r => Text -> Sem r ()
debug [fmt|got exception: {e:s}|]

handleEvent :: BotC r => Int -> DispatchData -> P.Sem r ()
handleEvent :: Int -> DispatchData -> Sem r ()
handleEvent Int
shardID DispatchData
data' = do
  Text -> Sem r ()
forall (r :: EffectRow). Member LogEff r => Text -> Sem r ()
debug [fmt|"handling an event: {ctorName data'}"|]
  EventHandlers
eventHandlers <- Sem r EventHandlers
forall s (r :: EffectRow). Member (AtomicState s) r => Sem r s
P.atomicGet
  Either String [IO ()]
actions <- Sem (Fail : r) [IO ()] -> Sem r (Either String [IO ()])
forall (r :: EffectRow) a.
Sem (Fail : r) a -> Sem r (Either String a)
P.runFail (Sem (Fail : r) [IO ()] -> Sem r (Either String [IO ()]))
-> Sem (Fail : r) [IO ()] -> Sem r (Either String [IO ()])
forall a b. (a -> b) -> a -> b
$ do
    Counter
evtCounter <- Text -> [(Text, Text)] -> Sem (Fail : r) Counter
forall (r :: EffectRow).
Member MetricEff r =>
Text -> [(Text, Text)] -> Sem r Counter
registerCounter Text
"events_received" [(Text
"type", String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ DispatchData -> String
forall a. CtorName a => a -> String
ctorName DispatchData
data'), (Text
"shard", Int -> Text
forall a. TextShow a => a -> Text
showt Int
shardID)]
    Sem (Fail : r) Int -> Sem (Fail : r) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Sem (Fail : r) Int -> Sem (Fail : r) ())
-> Sem (Fail : r) Int -> Sem (Fail : r) ()
forall a b. (a -> b) -> a -> b
$ Int -> Counter -> Sem (Fail : r) Int
forall (r :: EffectRow).
Member MetricEff r =>
Int -> Counter -> Sem r Int
addCounter Int
1 Counter
evtCounter
    Histogram
cacheUpdateHisto <- Text -> [(Text, Text)] -> [Double] -> Sem (Fail : r) Histogram
forall (r :: EffectRow).
Member MetricEff r =>
Text -> [(Text, Text)] -> [Double] -> Sem r Histogram
registerHistogram Text
"cache_update" [(Text, Text)]
forall a. Monoid a => a
mempty [Double
10, Double
20 .. Double
100]
    (Double
time, [IO ()]
res) <- Sem (Fail : r) [IO ()] -> Sem (Fail : r) (Double, [IO ()])
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Sem r a -> Sem r (Double, a)
timeA (Sem (Fail : r) [IO ()] -> Sem (Fail : r) (Double, [IO ()]))
-> (Sem (Fail : r) [IO ()] -> Sem (Fail : r) [IO ()])
-> Sem (Fail : r) [IO ()]
-> Sem (Fail : r) (Double, [IO ()])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem (Fail : r) [IO ()] -> Sem (Fail : r) [IO ()]
forall (r :: EffectRow) a. BotC r => Sem r a -> Sem r a
resetDi (Sem (Fail : r) [IO ()] -> Sem (Fail : r) (Double, [IO ()]))
-> Sem (Fail : r) [IO ()] -> Sem (Fail : r) (Double, [IO ()])
forall a b. (a -> b) -> a -> b
$ EventHandlers -> DispatchData -> Sem (Fail : r) [IO ()]
forall (r :: EffectRow).
BotC r =>
EventHandlers -> DispatchData -> Sem (Fail : r) [IO ()]
handleEvent' EventHandlers
eventHandlers DispatchData
data'
    Sem (Fail : r) HistogramSample -> Sem (Fail : r) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Sem (Fail : r) HistogramSample -> Sem (Fail : r) ())
-> Sem (Fail : r) HistogramSample -> Sem (Fail : r) ()
forall a b. (a -> b) -> a -> b
$ Double -> Histogram -> Sem (Fail : r) HistogramSample
forall (r :: EffectRow).
Member MetricEff r =>
Double -> Histogram -> Sem r HistogramSample
observeHistogram Double
time Histogram
cacheUpdateHisto
    [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [IO ()]
res

  Histogram
eventHandleHisto <- Text -> [(Text, Text)] -> [Double] -> Sem r Histogram
forall (r :: EffectRow).
Member MetricEff r =>
Text -> [(Text, Text)] -> [Double] -> Sem r Histogram
registerHistogram Text
"event_handle" [(Text, Text)]
forall a. Monoid a => a
mempty [Double
10, Double
20 .. Double
100]

  case Either String [IO ()]
actions of
    Right [IO ()]
actions -> [IO ()] -> (IO () -> Sem r (Async (Maybe ()))) -> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [IO ()]
actions ((IO () -> Sem r (Async (Maybe ()))) -> Sem r ())
-> (IO () -> Sem r (Async (Maybe ()))) -> Sem r ()
forall a b. (a -> b) -> a -> b
$ \IO ()
action -> Sem r () -> Sem r (Async (Maybe ()))
forall (r :: EffectRow) a.
Member Async r =>
Sem r a -> Sem r (Async (Maybe a))
P.async (Sem r () -> Sem r (Async (Maybe ())))
-> Sem r () -> Sem r (Async (Maybe ()))
forall a b. (a -> b) -> a -> b
$ do
      (Double
time, ()
_) <- Sem r () -> Sem r (Double, ())
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Sem r a -> Sem r (Double, a)
timeA (Sem r () -> Sem r (Double, ()))
-> (Sem r () -> Sem r ()) -> Sem r () -> Sem r (Double, ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem r () -> Sem r ()
forall (r :: EffectRow). BotC r => Sem r () -> Sem r ()
catchAllLogging (Sem r () -> Sem r (Double, ())) -> Sem r () -> Sem r (Double, ())
forall a b. (a -> b) -> a -> b
$ IO () -> Sem r ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed IO ()
action
      Sem r HistogramSample -> Sem r ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Sem r HistogramSample -> Sem r ())
-> Sem r HistogramSample -> Sem r ()
forall a b. (a -> b) -> a -> b
$ Double -> Histogram -> Sem r HistogramSample
forall (r :: EffectRow).
Member MetricEff r =>
Double -> Histogram -> Sem r HistogramSample
observeHistogram Double
time Histogram
eventHandleHisto
    -- pattern match failures are usually stuff like events for uncached guilds, etc
    Left String
err -> Text -> Sem r ()
forall (r :: EffectRow). Member LogEff r => Text -> Sem r ()
debug [fmt|Failed handling actions for event: {err:s}|]

handleEvent' ::
  BotC r =>
  EventHandlers ->
  DispatchData ->
  P.Sem (P.Fail ': r) [IO ()]
handleEvent' :: EventHandlers -> DispatchData -> Sem (Fail : r) [IO ()]
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(Ready rd :: ReadyData
rd@ReadyData {}) = do
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((ReadyData -> IO ()) -> IO ()) -> [ReadyData -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((ReadyData -> IO ()) -> ReadyData -> IO ()
forall a b. (a -> b) -> a -> b
$ ReadyData
rd) (EventHandlers -> [StoredEHType 'ReadyEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'ReadyEvt EventHandlers
eh)
handleEvent' EventHandlers
_ DispatchData
Resumed = [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(ChannelCreate (DMChannel' DMChannel
chan)) = do
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Channel
newChan <- DMChannel -> Channel
DMChannel' (DMChannel -> Channel)
-> Sem (Fail : r) (Maybe DMChannel)
-> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> Snowflake DMChannel -> Sem (Fail : r) (Maybe DMChannel)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake DMChannel -> Sem r (Maybe DMChannel)
getDM (DMChannel -> Snowflake DMChannel
forall b a. HasID b a => a -> Snowflake b
getID DMChannel
chan)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((Channel -> IO ()) -> IO ()) -> [Channel -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Channel -> IO ()) -> Channel -> IO ()
forall a b. (a -> b) -> a -> b
$ Channel
newChan) (EventHandlers -> [StoredEHType 'ChannelCreateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'ChannelCreateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(ChannelCreate (GuildChannel' GuildChannel
chan)) = do
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild (GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan)
  Just Channel
newChan <- Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Channel -> Sem (Fail : r) (Maybe Channel))
-> Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall a b. (a -> b) -> a -> b
$ GuildChannel -> Channel
GuildChannel' (GuildChannel -> Channel) -> Maybe GuildChannel -> Maybe Channel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Guild
guild Guild
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
-> Maybe GuildChannel
forall s a. s -> Getting a s a -> a
^. IsLabel
  "channels"
  ((SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
   -> Guild -> Const (Maybe GuildChannel) Guild)
(SnowflakeMap GuildChannel
 -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Guild -> Const (Maybe GuildChannel) Guild
#channels ((SnowflakeMap GuildChannel
  -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
 -> Guild -> Const (Maybe GuildChannel) Guild)
-> ((Maybe GuildChannel
     -> Const (Maybe GuildChannel) (Maybe GuildChannel))
    -> SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap GuildChannel)
-> Lens'
     (SnowflakeMap GuildChannel)
     (Maybe (IxValue (SnowflakeMap GuildChannel)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (GuildChannel -> Snowflake GuildChannel
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((Channel -> IO ()) -> IO ()) -> [Channel -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Channel -> IO ()) -> Channel -> IO ()
forall a b. (a -> b) -> a -> b
$ Channel
newChan) (EventHandlers -> [StoredEHType 'ChannelCreateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'ChannelCreateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(ChannelUpdate (DMChannel' DMChannel
chan)) = do
  Just Channel
oldChan <- DMChannel -> Channel
DMChannel' (DMChannel -> Channel)
-> Sem (Fail : r) (Maybe DMChannel)
-> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> Snowflake DMChannel -> Sem (Fail : r) (Maybe DMChannel)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake DMChannel -> Sem r (Maybe DMChannel)
getDM (DMChannel -> Snowflake DMChannel
forall b a. HasID b a => a -> Snowflake b
getID DMChannel
chan)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Channel
newChan <- DMChannel -> Channel
DMChannel' (DMChannel -> Channel)
-> Sem (Fail : r) (Maybe DMChannel)
-> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> Snowflake DMChannel -> Sem (Fail : r) (Maybe DMChannel)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake DMChannel -> Sem r (Maybe DMChannel)
getDM (DMChannel -> Snowflake DMChannel
forall b a. HasID b a => a -> Snowflake b
getID DMChannel
chan)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Channel, Channel) -> IO ()) -> IO ())
-> [(Channel, Channel) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Channel, Channel) -> IO ()) -> (Channel, Channel) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Channel
oldChan, Channel
newChan)) (EventHandlers -> [StoredEHType 'ChannelUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'ChannelUpdateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(ChannelUpdate (GuildChannel' GuildChannel
chan)) = do
  Just Guild
oldGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild (GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan)
  Just Channel
oldChan <- Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Channel -> Sem (Fail : r) (Maybe Channel))
-> Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall a b. (a -> b) -> a -> b
$ GuildChannel -> Channel
GuildChannel' (GuildChannel -> Channel) -> Maybe GuildChannel -> Maybe Channel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Guild
oldGuild Guild
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
-> Maybe GuildChannel
forall s a. s -> Getting a s a -> a
^. IsLabel
  "channels"
  ((SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
   -> Guild -> Const (Maybe GuildChannel) Guild)
(SnowflakeMap GuildChannel
 -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Guild -> Const (Maybe GuildChannel) Guild
#channels ((SnowflakeMap GuildChannel
  -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
 -> Guild -> Const (Maybe GuildChannel) Guild)
-> ((Maybe GuildChannel
     -> Const (Maybe GuildChannel) (Maybe GuildChannel))
    -> SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap GuildChannel)
-> Lens'
     (SnowflakeMap GuildChannel)
     (Maybe (IxValue (SnowflakeMap GuildChannel)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (GuildChannel -> Snowflake GuildChannel
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
newGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild (GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan)
  Just Channel
newChan <- Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Channel -> Sem (Fail : r) (Maybe Channel))
-> Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall a b. (a -> b) -> a -> b
$ GuildChannel -> Channel
GuildChannel' (GuildChannel -> Channel) -> Maybe GuildChannel -> Maybe Channel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Guild
newGuild Guild
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
-> Maybe GuildChannel
forall s a. s -> Getting a s a -> a
^. IsLabel
  "channels"
  ((SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
   -> Guild -> Const (Maybe GuildChannel) Guild)
(SnowflakeMap GuildChannel
 -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Guild -> Const (Maybe GuildChannel) Guild
#channels ((SnowflakeMap GuildChannel
  -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
 -> Guild -> Const (Maybe GuildChannel) Guild)
-> ((Maybe GuildChannel
     -> Const (Maybe GuildChannel) (Maybe GuildChannel))
    -> SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap GuildChannel)
-> Lens'
     (SnowflakeMap GuildChannel)
     (Maybe (IxValue (SnowflakeMap GuildChannel)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (GuildChannel -> Snowflake GuildChannel
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Channel, Channel) -> IO ()) -> IO ())
-> [(Channel, Channel) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Channel, Channel) -> IO ()) -> (Channel, Channel) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Channel
oldChan, Channel
newChan)) (EventHandlers -> [StoredEHType 'ChannelUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'ChannelUpdateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(ChannelDelete (GuildChannel' GuildChannel
chan)) = do
  Just Guild
oldGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild (GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan)
  Just Channel
oldChan <- Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Channel -> Sem (Fail : r) (Maybe Channel))
-> Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall a b. (a -> b) -> a -> b
$ GuildChannel -> Channel
GuildChannel' (GuildChannel -> Channel) -> Maybe GuildChannel -> Maybe Channel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Guild
oldGuild Guild
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
-> Maybe GuildChannel
forall s a. s -> Getting a s a -> a
^. IsLabel
  "channels"
  ((SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
   -> Guild -> Const (Maybe GuildChannel) Guild)
(SnowflakeMap GuildChannel
 -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Guild -> Const (Maybe GuildChannel) Guild
#channels ((SnowflakeMap GuildChannel
  -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
 -> Guild -> Const (Maybe GuildChannel) Guild)
-> ((Maybe GuildChannel
     -> Const (Maybe GuildChannel) (Maybe GuildChannel))
    -> SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap GuildChannel)
-> Lens'
     (SnowflakeMap GuildChannel)
     (Maybe (IxValue (SnowflakeMap GuildChannel)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (GuildChannel -> Snowflake GuildChannel
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((Channel -> IO ()) -> IO ()) -> [Channel -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Channel -> IO ()) -> Channel -> IO ()
forall a b. (a -> b) -> a -> b
$ Channel
oldChan) (EventHandlers -> [StoredEHType 'ChannelDeleteEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'ChannelDeleteEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(ChannelDelete (DMChannel' DMChannel
chan)) = do
  Just Channel
oldChan <- DMChannel -> Channel
DMChannel' (DMChannel -> Channel)
-> Sem (Fail : r) (Maybe DMChannel)
-> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> Snowflake DMChannel -> Sem (Fail : r) (Maybe DMChannel)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake DMChannel -> Sem r (Maybe DMChannel)
getDM (DMChannel -> Snowflake DMChannel
forall b a. HasID b a => a -> Snowflake b
getID DMChannel
chan)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((Channel -> IO ()) -> IO ()) -> [Channel -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Channel -> IO ()) -> Channel -> IO ()
forall a b. (a -> b) -> a -> b
$ Channel
oldChan) (EventHandlers -> [StoredEHType 'ChannelDeleteEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'ChannelDeleteEvt EventHandlers
eh)

-- handleEvent' eh evt@(ChannelPinsUpdate ChannelPinsUpdateData { channelID, lastPinTimestamp }) = do
--   chan <- (GuildChannel' <$> os ^? #channels . at (coerceSnowflake channelID) . _Just)
--     <|> (DMChannel' <$> os ^? #dms . at (coerceSnowflake channelID) . _Just)
--   pure $ map (\f -> f chan lastPinTimestamp) (getEventHandlers @"channelpinsupdate" eh)

handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildCreate Guild
guild) = do
  Bool
isNew <- Bool -> Bool
not (Bool -> Bool) -> Sem (Fail : r) Bool -> Sem (Fail : r) Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Snowflake Guild -> Sem (Fail : r) Bool
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r Bool
isUnavailableGuild (Guild -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID Guild
guild)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild (Guild -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID Guild
guild)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$
    (((Guild, GuildCreateStatus) -> IO ()) -> IO ())
-> [(Guild, GuildCreateStatus) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map
      (((Guild, GuildCreateStatus) -> IO ())
-> (Guild, GuildCreateStatus) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Guild
guild, if Bool
isNew then GuildCreateStatus
GuildCreateNew else GuildCreateStatus
GuildCreateAvailable))
      (EventHandlers -> [StoredEHType 'GuildCreateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildCreateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildUpdate UpdatedGuild
guild) = do
  Just Guild
oldGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild (UpdatedGuild -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID UpdatedGuild
guild)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
newGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild (UpdatedGuild -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID UpdatedGuild
guild)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Guild, Guild) -> IO ()) -> IO ())
-> [(Guild, Guild) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Guild, Guild) -> IO ()) -> (Guild, Guild) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Guild
oldGuild, Guild
newGuild)) (EventHandlers -> [StoredEHType 'GuildUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildUpdateEvt EventHandlers
eh)

-- NOTE: Guild will be deleted in the new cache if unavailable was false
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildDelete UnavailableGuild {Snowflake Guild
$sel:id:UnavailableGuild :: UnavailableGuild -> Snowflake Guild
id :: Snowflake Guild
id, Bool
$sel:unavailable:UnavailableGuild :: UnavailableGuild -> Bool
unavailable :: Bool
unavailable}) = do
  Just Guild
oldGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
id
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$
    (((Guild, GuildDeleteStatus) -> IO ()) -> IO ())
-> [(Guild, GuildDeleteStatus) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map
      (((Guild, GuildDeleteStatus) -> IO ())
-> (Guild, GuildDeleteStatus) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Guild
oldGuild, if Bool
unavailable then GuildDeleteStatus
GuildDeleteUnavailable else GuildDeleteStatus
GuildDeleteRemoved))
      (EventHandlers -> [StoredEHType 'GuildDeleteEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildDeleteEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildBanAdd BanData {Snowflake Guild
$sel:guildID:BanData :: BanData -> Snowflake Guild
guildID :: Snowflake Guild
guildID, User
$sel:user:BanData :: BanData -> User
user :: User
user}) = do
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Guild, User) -> IO ()) -> IO ())
-> [(Guild, User) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Guild, User) -> IO ()) -> (Guild, User) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Guild
guild, User
user)) (EventHandlers -> [StoredEHType 'GuildBanAddEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildBanAddEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildBanRemove BanData {Snowflake Guild
guildID :: Snowflake Guild
$sel:guildID:BanData :: BanData -> Snowflake Guild
guildID, User
user :: User
$sel:user:BanData :: BanData -> User
user}) = do
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Guild, User) -> IO ()) -> IO ())
-> [(Guild, User) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Guild, User) -> IO ()) -> (Guild, User) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Guild
guild, User
user)) (EventHandlers -> [StoredEHType 'GuildBanRemoveEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildBanRemoveEvt EventHandlers
eh)

-- NOTE: we fire this event using the guild data with old emojis
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildEmojisUpdate GuildEmojisUpdateData {Snowflake Guild
$sel:guildID:GuildEmojisUpdateData :: GuildEmojisUpdateData -> Snowflake Guild
guildID :: Snowflake Guild
guildID, [Emoji]
$sel:emojis:GuildEmojisUpdateData :: GuildEmojisUpdateData -> [Emoji]
emojis :: [Emoji]
emojis}) = do
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Guild, [Emoji]) -> IO ()) -> IO ())
-> [(Guild, [Emoji]) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Guild, [Emoji]) -> IO ()) -> (Guild, [Emoji]) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Guild
guild, [Emoji]
emojis)) (EventHandlers -> [StoredEHType 'GuildEmojisUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildEmojisUpdateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildIntegrationsUpdate GuildIntegrationsUpdateData {Snowflake Guild
$sel:guildID:GuildIntegrationsUpdateData :: GuildIntegrationsUpdateData -> Snowflake Guild
guildID :: Snowflake Guild
guildID}) = do
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((Guild -> IO ()) -> IO ()) -> [Guild -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Guild -> IO ()) -> Guild -> IO ()
forall a b. (a -> b) -> a -> b
$ Guild
guild) (EventHandlers -> [StoredEHType 'GuildIntegrationsUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildIntegrationsUpdateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildMemberAdd Snowflake Guild
gid Member
member) = do
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
gid
  Just Member
member <- Maybe Member -> Sem (Fail : r) (Maybe Member)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Member -> Sem (Fail : r) (Maybe Member))
-> Maybe Member -> Sem (Fail : r) (Maybe Member)
forall a b. (a -> b) -> a -> b
$ Guild
guild Guild
-> Getting (Maybe Member) Guild (Maybe Member) -> Maybe Member
forall s a. s -> Getting a s a -> a
^. IsLabel
  "members"
  ((SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
   -> Guild -> Const (Maybe Member) Guild)
(SnowflakeMap Member -> Const (Maybe Member) (SnowflakeMap Member))
-> Guild -> Const (Maybe Member) Guild
#members ((SnowflakeMap Member
  -> Const (Maybe Member) (SnowflakeMap Member))
 -> Guild -> Const (Maybe Member) Guild)
-> ((Maybe Member -> Const (Maybe Member) (Maybe Member))
    -> SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
-> Getting (Maybe Member) Guild (Maybe Member)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Lens'
     (SnowflakeMap Member) (Maybe (IxValue (SnowflakeMap Member)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Member -> Snowflake Member
forall b a. HasID b a => a -> Snowflake b
getID Member
member)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((Member -> IO ()) -> IO ()) -> [Member -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Member -> IO ()) -> Member -> IO ()
forall a b. (a -> b) -> a -> b
$ Member
member) (EventHandlers -> [StoredEHType 'GuildMemberAddEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildMemberAddEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildMemberRemove GuildMemberRemoveData {User
$sel:user:GuildMemberRemoveData :: GuildMemberRemoveData -> User
user :: User
user, Snowflake Guild
$sel:guildID:GuildMemberRemoveData :: GuildMemberRemoveData -> Snowflake Guild
guildID :: Snowflake Guild
guildID}) = do
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  Just Member
member <- Maybe Member -> Sem (Fail : r) (Maybe Member)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Member -> Sem (Fail : r) (Maybe Member))
-> Maybe Member -> Sem (Fail : r) (Maybe Member)
forall a b. (a -> b) -> a -> b
$ Guild
guild Guild
-> Getting (Maybe Member) Guild (Maybe Member) -> Maybe Member
forall s a. s -> Getting a s a -> a
^. IsLabel
  "members"
  ((SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
   -> Guild -> Const (Maybe Member) Guild)
(SnowflakeMap Member -> Const (Maybe Member) (SnowflakeMap Member))
-> Guild -> Const (Maybe Member) Guild
#members ((SnowflakeMap Member
  -> Const (Maybe Member) (SnowflakeMap Member))
 -> Guild -> Const (Maybe Member) Guild)
-> ((Maybe Member -> Const (Maybe Member) (Maybe Member))
    -> SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
-> Getting (Maybe Member) Guild (Maybe Member)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Lens'
     (SnowflakeMap Member) (Maybe (IxValue (SnowflakeMap Member)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (User -> Snowflake Member
forall b a. HasID b a => a -> Snowflake b
getID User
user)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((Member -> IO ()) -> IO ()) -> [Member -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Member -> IO ()) -> Member -> IO ()
forall a b. (a -> b) -> a -> b
$ Member
member) (EventHandlers -> [StoredEHType 'GuildMemberRemoveEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildMemberRemoveEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildMemberUpdate GuildMemberUpdateData {User
$sel:user:GuildMemberUpdateData :: GuildMemberUpdateData -> User
user :: User
user, Snowflake Guild
$sel:guildID:GuildMemberUpdateData :: GuildMemberUpdateData -> Snowflake Guild
guildID :: Snowflake Guild
guildID}) = do
  Just Guild
oldGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  Just Member
oldMember <- Maybe Member -> Sem (Fail : r) (Maybe Member)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Member -> Sem (Fail : r) (Maybe Member))
-> Maybe Member -> Sem (Fail : r) (Maybe Member)
forall a b. (a -> b) -> a -> b
$ Guild
oldGuild Guild
-> Getting (Maybe Member) Guild (Maybe Member) -> Maybe Member
forall s a. s -> Getting a s a -> a
^. IsLabel
  "members"
  ((SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
   -> Guild -> Const (Maybe Member) Guild)
(SnowflakeMap Member -> Const (Maybe Member) (SnowflakeMap Member))
-> Guild -> Const (Maybe Member) Guild
#members ((SnowflakeMap Member
  -> Const (Maybe Member) (SnowflakeMap Member))
 -> Guild -> Const (Maybe Member) Guild)
-> ((Maybe Member -> Const (Maybe Member) (Maybe Member))
    -> SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
-> Getting (Maybe Member) Guild (Maybe Member)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Lens'
     (SnowflakeMap Member) (Maybe (IxValue (SnowflakeMap Member)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (User -> Snowflake Member
forall b a. HasID b a => a -> Snowflake b
getID User
user)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
newGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  Just Member
newMember <- Maybe Member -> Sem (Fail : r) (Maybe Member)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Member -> Sem (Fail : r) (Maybe Member))
-> Maybe Member -> Sem (Fail : r) (Maybe Member)
forall a b. (a -> b) -> a -> b
$ Guild
newGuild Guild
-> Getting (Maybe Member) Guild (Maybe Member) -> Maybe Member
forall s a. s -> Getting a s a -> a
^. IsLabel
  "members"
  ((SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
   -> Guild -> Const (Maybe Member) Guild)
(SnowflakeMap Member -> Const (Maybe Member) (SnowflakeMap Member))
-> Guild -> Const (Maybe Member) Guild
#members ((SnowflakeMap Member
  -> Const (Maybe Member) (SnowflakeMap Member))
 -> Guild -> Const (Maybe Member) Guild)
-> ((Maybe Member -> Const (Maybe Member) (Maybe Member))
    -> SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
-> Getting (Maybe Member) Guild (Maybe Member)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Lens'
     (SnowflakeMap Member) (Maybe (IxValue (SnowflakeMap Member)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (User -> Snowflake Member
forall b a. HasID b a => a -> Snowflake b
getID User
user)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Member, Member) -> IO ()) -> IO ())
-> [(Member, Member) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Member, Member) -> IO ()) -> (Member, Member) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Member
oldMember, Member
newMember)) (EventHandlers -> [StoredEHType 'GuildMemberUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildMemberUpdateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildMembersChunk GuildMembersChunkData {[Member]
$sel:members:GuildMembersChunkData :: GuildMembersChunkData -> [Member]
members :: [Member]
members, Snowflake Guild
$sel:guildID:GuildMembersChunkData :: GuildMembersChunkData -> Snowflake Guild
guildID :: Snowflake Guild
guildID}) = do
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  let memberIDs :: [Snowflake Member]
memberIDs = (Member -> Snowflake Member) -> [Member] -> [Snowflake Member]
forall a b. (a -> b) -> [a] -> [b]
map (forall a. HasID Member a => a -> Snowflake Member
forall b a. HasID b a => a -> Snowflake b
getID @Member) [Member]
members
  let members' :: [Member]
members' = (Snowflake Member -> Maybe Member)
-> [Snowflake Member] -> [Member]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (\Snowflake Member
mid -> Guild
guild Guild
-> Getting (Maybe Member) Guild (Maybe Member) -> Maybe Member
forall s a. s -> Getting a s a -> a
^. IsLabel
  "members"
  ((SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
   -> Guild -> Const (Maybe Member) Guild)
(SnowflakeMap Member -> Const (Maybe Member) (SnowflakeMap Member))
-> Guild -> Const (Maybe Member) Guild
#members ((SnowflakeMap Member
  -> Const (Maybe Member) (SnowflakeMap Member))
 -> Guild -> Const (Maybe Member) Guild)
-> ((Maybe Member -> Const (Maybe Member) (Maybe Member))
    -> SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
-> Getting (Maybe Member) Guild (Maybe Member)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Lens'
     (SnowflakeMap Member) (Maybe (IxValue (SnowflakeMap Member)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (SnowflakeMap Member)
Snowflake Member
mid) [Snowflake Member]
memberIDs
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Guild, [Member]) -> IO ()) -> IO ())
-> [(Guild, [Member]) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Guild, [Member]) -> IO ()) -> (Guild, [Member]) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Guild
guild, [Member]
members')) (EventHandlers -> [StoredEHType 'GuildMembersChunkEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildMembersChunkEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildRoleCreate GuildRoleData {Snowflake Guild
$sel:guildID:GuildRoleData :: GuildRoleData -> Snowflake Guild
guildID :: Snowflake Guild
guildID, Role
$sel:role:GuildRoleData :: GuildRoleData -> Role
role :: Role
role}) = do
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  Just Role
role' <- Maybe Role -> Sem (Fail : r) (Maybe Role)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Role -> Sem (Fail : r) (Maybe Role))
-> Maybe Role -> Sem (Fail : r) (Maybe Role)
forall a b. (a -> b) -> a -> b
$ Guild
guild Guild -> Getting (Maybe Role) Guild (Maybe Role) -> Maybe Role
forall s a. s -> Getting a s a -> a
^. IsLabel
  "roles"
  ((SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
   -> Guild -> Const (Maybe Role) Guild)
(SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
-> Guild -> Const (Maybe Role) Guild
#roles ((SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
 -> Guild -> Const (Maybe Role) Guild)
-> ((Maybe Role -> Const (Maybe Role) (Maybe Role))
    -> SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
-> Getting (Maybe Role) Guild (Maybe Role)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Role)
-> Lens' (SnowflakeMap Role) (Maybe (IxValue (SnowflakeMap Role)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Role -> Snowflake Role
forall b a. HasID b a => a -> Snowflake b
getID Role
role)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Guild, Role) -> IO ()) -> IO ())
-> [(Guild, Role) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Guild, Role) -> IO ()) -> (Guild, Role) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Guild
guild, Role
role')) (EventHandlers -> [StoredEHType 'GuildRoleCreateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildRoleCreateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildRoleUpdate GuildRoleData {Snowflake Guild
guildID :: Snowflake Guild
$sel:guildID:GuildRoleData :: GuildRoleData -> Snowflake Guild
guildID, Role
role :: Role
$sel:role:GuildRoleData :: GuildRoleData -> Role
role}) = do
  Just Guild
oldGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  Just Role
oldRole <- Maybe Role -> Sem (Fail : r) (Maybe Role)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Role -> Sem (Fail : r) (Maybe Role))
-> Maybe Role -> Sem (Fail : r) (Maybe Role)
forall a b. (a -> b) -> a -> b
$ Guild
oldGuild Guild -> Getting (Maybe Role) Guild (Maybe Role) -> Maybe Role
forall s a. s -> Getting a s a -> a
^. IsLabel
  "roles"
  ((SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
   -> Guild -> Const (Maybe Role) Guild)
(SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
-> Guild -> Const (Maybe Role) Guild
#roles ((SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
 -> Guild -> Const (Maybe Role) Guild)
-> ((Maybe Role -> Const (Maybe Role) (Maybe Role))
    -> SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
-> Getting (Maybe Role) Guild (Maybe Role)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Role)
-> Lens' (SnowflakeMap Role) (Maybe (IxValue (SnowflakeMap Role)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Role -> Snowflake Role
forall b a. HasID b a => a -> Snowflake b
getID Role
role)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
newGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  Just Role
newRole <- Maybe Role -> Sem (Fail : r) (Maybe Role)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Role -> Sem (Fail : r) (Maybe Role))
-> Maybe Role -> Sem (Fail : r) (Maybe Role)
forall a b. (a -> b) -> a -> b
$ Guild
newGuild Guild -> Getting (Maybe Role) Guild (Maybe Role) -> Maybe Role
forall s a. s -> Getting a s a -> a
^. IsLabel
  "roles"
  ((SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
   -> Guild -> Const (Maybe Role) Guild)
(SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
-> Guild -> Const (Maybe Role) Guild
#roles ((SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
 -> Guild -> Const (Maybe Role) Guild)
-> ((Maybe Role -> Const (Maybe Role) (Maybe Role))
    -> SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
-> Getting (Maybe Role) Guild (Maybe Role)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Role)
-> Lens' (SnowflakeMap Role) (Maybe (IxValue (SnowflakeMap Role)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Role -> Snowflake Role
forall b a. HasID b a => a -> Snowflake b
getID Role
role)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Guild, Role, Role) -> IO ()) -> IO ())
-> [(Guild, Role, Role) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Guild, Role, Role) -> IO ()) -> (Guild, Role, Role) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Guild
newGuild, Role
oldRole, Role
newRole)) (EventHandlers -> [StoredEHType 'GuildRoleUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildRoleUpdateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(GuildRoleDelete GuildRoleDeleteData {Snowflake Guild
$sel:guildID:GuildRoleDeleteData :: GuildRoleDeleteData -> Snowflake Guild
guildID :: Snowflake Guild
guildID, Snowflake Role
$sel:roleID:GuildRoleDeleteData :: GuildRoleDeleteData -> Snowflake Role
roleID :: Snowflake Role
roleID}) = do
  Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  Just Role
role <- Maybe Role -> Sem (Fail : r) (Maybe Role)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Role -> Sem (Fail : r) (Maybe Role))
-> Maybe Role -> Sem (Fail : r) (Maybe Role)
forall a b. (a -> b) -> a -> b
$ Guild
guild Guild -> Getting (Maybe Role) Guild (Maybe Role) -> Maybe Role
forall s a. s -> Getting a s a -> a
^. IsLabel
  "roles"
  ((SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
   -> Guild -> Const (Maybe Role) Guild)
(SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
-> Guild -> Const (Maybe Role) Guild
#roles ((SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
 -> Guild -> Const (Maybe Role) Guild)
-> ((Maybe Role -> Const (Maybe Role) (Maybe Role))
    -> SnowflakeMap Role -> Const (Maybe Role) (SnowflakeMap Role))
-> Getting (Maybe Role) Guild (Maybe Role)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Role)
-> Lens' (SnowflakeMap Role) (Maybe (IxValue (SnowflakeMap Role)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (SnowflakeMap Role)
Snowflake Role
roleID
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Guild, Role) -> IO ()) -> IO ())
-> [(Guild, Role) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Guild, Role) -> IO ()) -> (Guild, Role) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Guild
guild, Role
role)) (EventHandlers -> [StoredEHType 'GuildRoleDeleteEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildRoleDeleteEvt EventHandlers
eh)
handleEvent' EventHandlers
eh (InviteCreate InviteCreateData
d) = do
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((InviteCreateData -> IO ()) -> IO ())
-> [InviteCreateData -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((InviteCreateData -> IO ()) -> InviteCreateData -> IO ()
forall a b. (a -> b) -> a -> b
$ InviteCreateData
d) (EventHandlers -> [StoredEHType 'InviteCreateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'InviteCreateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh (InviteDelete InviteDeleteData
d) = do
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((InviteDeleteData -> IO ()) -> IO ())
-> [InviteDeleteData -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((InviteDeleteData -> IO ()) -> InviteDeleteData -> IO ()
forall a b. (a -> b) -> a -> b
$ InviteDeleteData
d) (EventHandlers -> [StoredEHType 'InviteDeleteEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'InviteDeleteEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(MessageCreate Message
msg Maybe User
user Maybe Member
member) = do
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Message, Maybe User, Maybe Member) -> IO ()) -> IO ())
-> [(Message, Maybe User, Maybe Member) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Message, Maybe User, Maybe Member) -> IO ())
-> (Message, Maybe User, Maybe Member) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Message
msg, Maybe User
user, Maybe Member
member)) (EventHandlers -> [StoredEHType 'MessageCreateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'MessageCreateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(MessageUpdate UpdatedMessage
msg Maybe User
user Maybe Member
member) = do
  Maybe Message
oldMsg <- Snowflake Message -> Sem (Fail : r) (Maybe Message)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> Sem r (Maybe Message)
getMessage (UpdatedMessage -> Snowflake Message
forall b a. HasID b a => a -> Snowflake b
getID UpdatedMessage
msg)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Maybe Message
newMsg <- Snowflake Message -> Sem (Fail : r) (Maybe Message)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> Sem r (Maybe Message)
getMessage (UpdatedMessage -> Snowflake Message
forall b a. HasID b a => a -> Snowflake b
getID UpdatedMessage
msg)
  let rawActions :: [IO ()]
rawActions = (((UpdatedMessage, Maybe User, Maybe Member) -> IO ()) -> IO ())
-> [(UpdatedMessage, Maybe User, Maybe Member) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((UpdatedMessage, Maybe User, Maybe Member) -> IO ())
-> (UpdatedMessage, Maybe User, Maybe Member) -> IO ()
forall a b. (a -> b) -> a -> b
$ (UpdatedMessage
msg, Maybe User
user, Maybe Member
member)) (EventHandlers -> [StoredEHType 'RawMessageUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'RawMessageUpdateEvt EventHandlers
eh)
  let actions :: [IO ()]
actions = case (Maybe Message
oldMsg, Maybe Message
newMsg) of
        (Just Message
oldMsg', Just Message
newMsg') ->
          (((Message, Message, Maybe User, Maybe Member) -> IO ()) -> IO ())
-> [(Message, Message, Maybe User, Maybe Member) -> IO ()]
-> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Message, Message, Maybe User, Maybe Member) -> IO ())
-> (Message, Message, Maybe User, Maybe Member) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Message
oldMsg', Message
newMsg', Maybe User
user, Maybe Member
member)) (EventHandlers -> [StoredEHType 'MessageUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'MessageUpdateEvt EventHandlers
eh)
        (Maybe Message, Maybe Message)
_ -> []
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ [IO ()]
rawActions [IO ()] -> [IO ()] -> [IO ()]
forall a. Semigroup a => a -> a -> a
<> [IO ()]
actions
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(MessageDelete MessageDeleteData {Snowflake Message
$sel:id:MessageDeleteData :: MessageDeleteData -> Snowflake Message
id :: Snowflake Message
id}) = do
  Maybe Message
oldMsg <- Snowflake Message -> Sem (Fail : r) (Maybe Message)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> Sem r (Maybe Message)
getMessage Snowflake Message
id
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  let rawActions :: [IO ()]
rawActions = ((Snowflake Message -> IO ()) -> IO ())
-> [Snowflake Message -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Snowflake Message -> IO ()) -> Snowflake Message -> IO ()
forall a b. (a -> b) -> a -> b
$ Snowflake Message
id) (EventHandlers -> [StoredEHType 'RawMessageDeleteEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'RawMessageDeleteEvt EventHandlers
eh)
  let actions :: [IO ()]
actions = case Maybe Message
oldMsg of
        Just Message
oldMsg' ->
          ((Message -> IO ()) -> IO ()) -> [Message -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Message -> IO ()) -> Message -> IO ()
forall a b. (a -> b) -> a -> b
$ Message
oldMsg') (EventHandlers -> [StoredEHType 'MessageDeleteEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'MessageDeleteEvt EventHandlers
eh)
        Maybe Message
_ -> []
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ [IO ()]
rawActions [IO ()] -> [IO ()] -> [IO ()]
forall a. Semigroup a => a -> a -> a
<> [IO ()]
actions
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(MessageDeleteBulk MessageDeleteBulkData {[Snowflake Message]
$sel:ids:MessageDeleteBulkData :: MessageDeleteBulkData -> [Snowflake Message]
ids :: [Snowflake Message]
ids}) = do
  [Message]
messages <- [Maybe Message] -> [Message]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Message] -> [Message])
-> Sem (Fail : r) [Maybe Message] -> Sem (Fail : r) [Message]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Snowflake Message -> Sem (Fail : r) (Maybe Message))
-> [Snowflake Message] -> Sem (Fail : r) [Maybe Message]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Snowflake Message -> Sem (Fail : r) (Maybe Message)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> Sem r (Maybe Message)
getMessage [Snowflake Message]
ids
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  let rawActions :: [IO ()]
rawActions = (([Snowflake Message] -> IO ()) -> IO ())
-> [[Snowflake Message] -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (([Snowflake Message] -> IO ()) -> [Snowflake Message] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Snowflake Message]
ids) (EventHandlers -> [StoredEHType 'RawMessageDeleteBulkEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'RawMessageDeleteBulkEvt EventHandlers
eh)
  let actions :: [IO ()]
actions = (([Message] -> IO ()) -> IO ()) -> [[Message] -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (([Message] -> IO ()) -> [Message] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Message]
messages) (EventHandlers -> [StoredEHType 'MessageDeleteBulkEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'MessageDeleteBulkEvt EventHandlers
eh)
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ [IO ()]
rawActions [IO ()] -> [IO ()] -> [IO ()]
forall a. Semigroup a => a -> a -> a
<> [IO ()]
actions
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(MessageReactionAdd ReactionEvtData
reaction) = do
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Maybe Message
msg <- Snowflake Message -> Sem (Fail : r) (Maybe Message)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> Sem r (Maybe Message)
getMessage (ReactionEvtData -> Snowflake Message
forall b a. HasID b a => a -> Snowflake b
getID ReactionEvtData
reaction)
  Maybe User
user <- Snowflake User -> Sem (Fail : r) (Maybe User)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake User -> Sem r (Maybe User)
getUser (ReactionEvtData -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID ReactionEvtData
reaction)
  Maybe Channel
chan <- case ReactionEvtData
reaction ReactionEvtData
-> Getting
     (Maybe (Snowflake Guild)) ReactionEvtData (Maybe (Snowflake Guild))
-> Maybe (Snowflake Guild)
forall s a. s -> Getting a s a -> a
^. IsLabel
  "guildID"
  (Getting
     (Maybe (Snowflake Guild))
     ReactionEvtData
     (Maybe (Snowflake Guild)))
Getting
  (Maybe (Snowflake Guild)) ReactionEvtData (Maybe (Snowflake Guild))
#guildID of
    Just Snowflake Guild
_ -> do
      Maybe GuildChannel
chan <- Snowflake GuildChannel -> Sem (Fail : r) (Maybe GuildChannel)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake GuildChannel -> Sem r (Maybe GuildChannel)
getGuildChannel (Snowflake Channel -> Snowflake GuildChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake Channel -> Snowflake GuildChannel)
-> Snowflake Channel -> Snowflake GuildChannel
forall a b. (a -> b) -> a -> b
$ ReactionEvtData -> Snowflake Channel
forall b a. HasID b a => a -> Snowflake b
getID @Channel ReactionEvtData
reaction)
      Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GuildChannel -> Channel
GuildChannel' (GuildChannel -> Channel) -> Maybe GuildChannel -> Maybe Channel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe GuildChannel
chan)
    Maybe (Snowflake Guild)
Nothing -> do
      Maybe DMChannel
chan <- Snowflake DMChannel -> Sem (Fail : r) (Maybe DMChannel)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake DMChannel -> Sem r (Maybe DMChannel)
getDM (Snowflake Channel -> Snowflake DMChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake Channel -> Snowflake DMChannel)
-> Snowflake Channel -> Snowflake DMChannel
forall a b. (a -> b) -> a -> b
$ ReactionEvtData -> Snowflake Channel
forall b a. HasID b a => a -> Snowflake b
getID @Channel ReactionEvtData
reaction)
      Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DMChannel -> Channel
DMChannel' (DMChannel -> Channel) -> Maybe DMChannel -> Maybe Channel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe DMChannel
chan)
  let rawActions :: [IO ()]
rawActions = ((ReactionEvtData -> IO ()) -> IO ())
-> [ReactionEvtData -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((ReactionEvtData -> IO ()) -> ReactionEvtData -> IO ()
forall a b. (a -> b) -> a -> b
$ ReactionEvtData
reaction) (EventHandlers -> [StoredEHType 'RawMessageReactionAddEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'RawMessageReactionAddEvt EventHandlers
eh)
  let actions :: [IO ()]
actions = case (Maybe Message
msg, Maybe User
user, Maybe Channel
chan) of
        (Just Message
msg', Just User
user', Just Channel
chan') ->
          (((Message, User, Channel, RawEmoji) -> IO ()) -> IO ())
-> [(Message, User, Channel, RawEmoji) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Message, User, Channel, RawEmoji) -> IO ())
-> (Message, User, Channel, RawEmoji) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Message
msg', User
user', Channel
chan', ReactionEvtData
reaction ReactionEvtData
-> Getting RawEmoji ReactionEvtData RawEmoji -> RawEmoji
forall s a. s -> Getting a s a -> a
^. IsLabel "emoji" (Getting RawEmoji ReactionEvtData RawEmoji)
Getting RawEmoji ReactionEvtData RawEmoji
#emoji)) (EventHandlers -> [StoredEHType 'MessageReactionAddEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'MessageReactionAddEvt EventHandlers
eh)
        (Maybe Message, Maybe User, Maybe Channel)
_ -> []
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ [IO ()]
rawActions [IO ()] -> [IO ()] -> [IO ()]
forall a. Semigroup a => a -> a -> a
<> [IO ()]
actions
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(MessageReactionRemove ReactionEvtData
reaction) = do
  Maybe Message
msg <- Snowflake Message -> Sem (Fail : r) (Maybe Message)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> Sem r (Maybe Message)
getMessage (ReactionEvtData -> Snowflake Message
forall b a. HasID b a => a -> Snowflake b
getID ReactionEvtData
reaction)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Maybe User
user <- Snowflake User -> Sem (Fail : r) (Maybe User)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake User -> Sem r (Maybe User)
getUser (ReactionEvtData -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID ReactionEvtData
reaction)
  Maybe Channel
chan <- case ReactionEvtData
reaction ReactionEvtData
-> Getting
     (Maybe (Snowflake Guild)) ReactionEvtData (Maybe (Snowflake Guild))
-> Maybe (Snowflake Guild)
forall s a. s -> Getting a s a -> a
^. IsLabel
  "guildID"
  (Getting
     (Maybe (Snowflake Guild))
     ReactionEvtData
     (Maybe (Snowflake Guild)))
Getting
  (Maybe (Snowflake Guild)) ReactionEvtData (Maybe (Snowflake Guild))
#guildID of
    Just Snowflake Guild
_ -> do
      Maybe GuildChannel
chan <- Snowflake GuildChannel -> Sem (Fail : r) (Maybe GuildChannel)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake GuildChannel -> Sem r (Maybe GuildChannel)
getGuildChannel (Snowflake Channel -> Snowflake GuildChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake Channel -> Snowflake GuildChannel)
-> Snowflake Channel -> Snowflake GuildChannel
forall a b. (a -> b) -> a -> b
$ ReactionEvtData -> Snowflake Channel
forall b a. HasID b a => a -> Snowflake b
getID @Channel ReactionEvtData
reaction)
      Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GuildChannel -> Channel
GuildChannel' (GuildChannel -> Channel) -> Maybe GuildChannel -> Maybe Channel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe GuildChannel
chan)
    Maybe (Snowflake Guild)
Nothing -> do
      Maybe DMChannel
chan <- Snowflake DMChannel -> Sem (Fail : r) (Maybe DMChannel)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake DMChannel -> Sem r (Maybe DMChannel)
getDM (Snowflake Channel -> Snowflake DMChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake (Snowflake Channel -> Snowflake DMChannel)
-> Snowflake Channel -> Snowflake DMChannel
forall a b. (a -> b) -> a -> b
$ ReactionEvtData -> Snowflake Channel
forall b a. HasID b a => a -> Snowflake b
getID @Channel ReactionEvtData
reaction)
      Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DMChannel -> Channel
DMChannel' (DMChannel -> Channel) -> Maybe DMChannel -> Maybe Channel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe DMChannel
chan)
  let rawActions :: [IO ()]
rawActions = ((ReactionEvtData -> IO ()) -> IO ())
-> [ReactionEvtData -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((ReactionEvtData -> IO ()) -> ReactionEvtData -> IO ()
forall a b. (a -> b) -> a -> b
$ ReactionEvtData
reaction) (EventHandlers -> [StoredEHType 'RawMessageReactionRemoveEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'RawMessageReactionRemoveEvt EventHandlers
eh)
  let actions :: [IO ()]
actions = case (Maybe Message
msg, Maybe User
user, Maybe Channel
chan) of
        (Just Message
msg', Just User
user', Just Channel
chan') ->
          (((Message, User, Channel, RawEmoji) -> IO ()) -> IO ())
-> [(Message, User, Channel, RawEmoji) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Message, User, Channel, RawEmoji) -> IO ())
-> (Message, User, Channel, RawEmoji) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Message
msg', User
user', Channel
chan', ReactionEvtData
reaction ReactionEvtData
-> Getting RawEmoji ReactionEvtData RawEmoji -> RawEmoji
forall s a. s -> Getting a s a -> a
^. IsLabel "emoji" (Getting RawEmoji ReactionEvtData RawEmoji)
Getting RawEmoji ReactionEvtData RawEmoji
#emoji)) (EventHandlers -> [StoredEHType 'MessageReactionRemoveEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'MessageReactionRemoveEvt EventHandlers
eh)
        (Maybe Message, Maybe User, Maybe Channel)
_ -> []
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ [IO ()]
rawActions [IO ()] -> [IO ()] -> [IO ()]
forall a. Semigroup a => a -> a -> a
<> [IO ()]
actions
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(MessageReactionRemoveAll MessageReactionRemoveAllData {Snowflake Message
$sel:messageID:MessageReactionRemoveAllData :: MessageReactionRemoveAllData -> Snowflake Message
messageID :: Snowflake Message
messageID}) = do
  Maybe Message
msg <- Snowflake Message -> Sem (Fail : r) (Maybe Message)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> Sem r (Maybe Message)
getMessage Snowflake Message
messageID
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  let rawActions :: [IO ()]
rawActions = ((Snowflake Message -> IO ()) -> IO ())
-> [Snowflake Message -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Snowflake Message -> IO ()) -> Snowflake Message -> IO ()
forall a b. (a -> b) -> a -> b
$ Snowflake Message
messageID) (EventHandlers -> [StoredEHType 'RawMessageReactionRemoveAllEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'RawMessageReactionRemoveAllEvt EventHandlers
eh)
  let actions :: [IO ()]
actions = case Maybe Message
msg of
        Just Message
msg' ->
          ((Message -> IO ()) -> IO ()) -> [Message -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Message -> IO ()) -> Message -> IO ()
forall a b. (a -> b) -> a -> b
$ Message
msg') (EventHandlers -> [StoredEHType 'MessageReactionRemoveAllEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'MessageReactionRemoveAllEvt EventHandlers
eh)
        Maybe Message
_ -> []
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ [IO ()]
rawActions [IO ()] -> [IO ()] -> [IO ()]
forall a. Semigroup a => a -> a -> a
<> [IO ()]
actions
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(PresenceUpdate PresenceUpdateData {Snowflake User
$sel:userID:PresenceUpdateData :: PresenceUpdateData -> Snowflake User
userID :: Snowflake User
userID, $sel:presence:PresenceUpdateData :: PresenceUpdateData -> Presence
presence = Presence {Snowflake Guild
$sel:guildID:Presence :: Presence -> Snowflake Guild
guildID :: Snowflake Guild
guildID}}) = do
  Just Guild
oldGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  Just Member
oldMember <- Maybe Member -> Sem (Fail : r) (Maybe Member)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Member -> Sem (Fail : r) (Maybe Member))
-> Maybe Member -> Sem (Fail : r) (Maybe Member)
forall a b. (a -> b) -> a -> b
$ Guild
oldGuild Guild
-> Getting (Maybe Member) Guild (Maybe Member) -> Maybe Member
forall s a. s -> Getting a s a -> a
^. IsLabel
  "members"
  ((SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
   -> Guild -> Const (Maybe Member) Guild)
(SnowflakeMap Member -> Const (Maybe Member) (SnowflakeMap Member))
-> Guild -> Const (Maybe Member) Guild
#members ((SnowflakeMap Member
  -> Const (Maybe Member) (SnowflakeMap Member))
 -> Guild -> Const (Maybe Member) Guild)
-> ((Maybe Member -> Const (Maybe Member) (Maybe Member))
    -> SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
-> Getting (Maybe Member) Guild (Maybe Member)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Lens'
     (SnowflakeMap Member) (Maybe (IxValue (SnowflakeMap Member)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Snowflake User -> Snowflake Member
forall a b. Snowflake a -> Snowflake b
coerceSnowflake Snowflake User
userID)
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just Guild
newGuild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  Just Member
newMember <- Maybe Member -> Sem (Fail : r) (Maybe Member)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Member -> Sem (Fail : r) (Maybe Member))
-> Maybe Member -> Sem (Fail : r) (Maybe Member)
forall a b. (a -> b) -> a -> b
$ Guild
newGuild Guild
-> Getting (Maybe Member) Guild (Maybe Member) -> Maybe Member
forall s a. s -> Getting a s a -> a
^. IsLabel
  "members"
  ((SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
   -> Guild -> Const (Maybe Member) Guild)
(SnowflakeMap Member -> Const (Maybe Member) (SnowflakeMap Member))
-> Guild -> Const (Maybe Member) Guild
#members ((SnowflakeMap Member
  -> Const (Maybe Member) (SnowflakeMap Member))
 -> Guild -> Const (Maybe Member) Guild)
-> ((Maybe Member -> Const (Maybe Member) (Maybe Member))
    -> SnowflakeMap Member
    -> Const (Maybe Member) (SnowflakeMap Member))
-> Getting (Maybe Member) Guild (Maybe Member)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Lens'
     (SnowflakeMap Member) (Maybe (IxValue (SnowflakeMap Member)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Snowflake User -> Snowflake Member
forall a b. Snowflake a -> Snowflake b
coerceSnowflake Snowflake User
userID)
  let User
oldUser :: User = Member -> User
forall sup sub. Subtype sup sub => sub -> sup
upcast Member
oldMember
      User
newUser :: User = Member -> User
forall sup sub. Subtype sup sub => sub -> sup
upcast Member
newMember
      userUpdates :: [IO ()]
userUpdates =
        if User
oldUser User -> User -> Bool
forall a. Eq a => a -> a -> Bool
/= User
newUser
          then (((User, User) -> IO ()) -> IO ())
-> [(User, User) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((User, User) -> IO ()) -> (User, User) -> IO ()
forall a b. (a -> b) -> a -> b
$ (User
oldUser, User
newUser)) (EventHandlers -> [StoredEHType 'UserUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'UserUpdateEvt EventHandlers
eh)
          else [IO ()]
forall a. Monoid a => a
mempty
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ [IO ()]
userUpdates [IO ()] -> [IO ()] -> [IO ()]
forall a. Semigroup a => a -> a -> a
<> (((Member, Member) -> IO ()) -> IO ())
-> [(Member, Member) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Member, Member) -> IO ()) -> (Member, Member) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Member
oldMember, Member
newMember)) (EventHandlers -> [StoredEHType 'GuildMemberUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'GuildMemberUpdateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh (TypingStart TypingStartData {Snowflake Channel
$sel:channelID:TypingStartData :: TypingStartData -> Snowflake Channel
channelID :: Snowflake Channel
channelID, Maybe (Snowflake Guild)
$sel:guildID:TypingStartData :: TypingStartData -> Maybe (Snowflake Guild)
guildID :: Maybe (Snowflake Guild)
guildID, Snowflake User
$sel:userID:TypingStartData :: TypingStartData -> Snowflake User
userID :: Snowflake User
userID, $sel:timestamp:TypingStartData :: TypingStartData -> UnixTimestamp
timestamp = UnixTimestamp UTCTime
timestamp}) =
  case Maybe (Snowflake Guild)
guildID of
    Just Snowflake Guild
gid -> do
      Just Guild
guild <- Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
gid
      Just Channel
chan <- Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Channel -> Sem (Fail : r) (Maybe Channel))
-> Maybe Channel -> Sem (Fail : r) (Maybe Channel)
forall a b. (a -> b) -> a -> b
$ GuildChannel -> Channel
GuildChannel' (GuildChannel -> Channel) -> Maybe GuildChannel -> Maybe Channel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Guild
guild Guild
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
-> Maybe GuildChannel
forall s a. s -> Getting a s a -> a
^. IsLabel
  "channels"
  ((SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
   -> Guild -> Const (Maybe GuildChannel) Guild)
(SnowflakeMap GuildChannel
 -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Guild -> Const (Maybe GuildChannel) Guild
#channels ((SnowflakeMap GuildChannel
  -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
 -> Guild -> Const (Maybe GuildChannel) Guild)
-> ((Maybe GuildChannel
     -> Const (Maybe GuildChannel) (Maybe GuildChannel))
    -> SnowflakeMap GuildChannel
    -> Const (Maybe GuildChannel) (SnowflakeMap GuildChannel))
-> Getting (Maybe GuildChannel) Guild (Maybe GuildChannel)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap GuildChannel)
-> Lens'
     (SnowflakeMap GuildChannel)
     (Maybe (IxValue (SnowflakeMap GuildChannel)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Snowflake Channel -> Snowflake GuildChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake Snowflake Channel
channelID)
      [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Channel, Snowflake User, UTCTime) -> IO ()) -> IO ())
-> [(Channel, Snowflake User, UTCTime) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Channel, Snowflake User, UTCTime) -> IO ())
-> (Channel, Snowflake User, UTCTime) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Channel
chan, Snowflake User
userID, UTCTime
timestamp)) (EventHandlers -> [StoredEHType 'TypingStartEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'TypingStartEvt EventHandlers
eh)
    Maybe (Snowflake Guild)
Nothing -> do
      Just Channel
chan <- DMChannel -> Channel
DMChannel' (DMChannel -> Channel)
-> Sem (Fail : r) (Maybe DMChannel)
-> Sem (Fail : r) (Maybe Channel)
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> Snowflake DMChannel -> Sem (Fail : r) (Maybe DMChannel)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake DMChannel -> Sem r (Maybe DMChannel)
getDM (Snowflake Channel -> Snowflake DMChannel
forall a b. Snowflake a -> Snowflake b
coerceSnowflake Snowflake Channel
channelID)
      [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Channel, Snowflake User, UTCTime) -> IO ()) -> IO ())
-> [(Channel, Snowflake User, UTCTime) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Channel, Snowflake User, UTCTime) -> IO ())
-> (Channel, Snowflake User, UTCTime) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Channel
chan, Snowflake User
userID, UTCTime
timestamp)) (EventHandlers -> [StoredEHType 'TypingStartEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'TypingStartEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(UserUpdate User
_) = do
  Just User
oldUser <- Sem (Fail : r) (Maybe User)
forall (r :: EffectRow). Member CacheEff r => Sem r (Maybe User)
getBotUser
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  Just User
newUser <- Sem (Fail : r) (Maybe User)
forall (r :: EffectRow). Member CacheEff r => Sem r (Maybe User)
getBotUser
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((User, User) -> IO ()) -> IO ())
-> [(User, User) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((User, User) -> IO ()) -> (User, User) -> IO ()
forall a b. (a -> b) -> a -> b
$ (User
oldUser, User
newUser)) (EventHandlers -> [StoredEHType 'UserUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'UserUpdateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(VoiceStateUpdate newVoiceState :: VoiceState
newVoiceState@V.VoiceState {$sel:guildID:VoiceState :: VoiceState -> Maybe (Snowflake Guild)
guildID = Just Snowflake Guild
guildID}) = do
  Maybe VoiceState
oldVoiceState <- (((VoiceState -> Bool) -> [VoiceState] -> Maybe VoiceState
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== VoiceState -> Text
V.sessionID VoiceState
newVoiceState) (Text -> Bool) -> (VoiceState -> Text) -> VoiceState -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VoiceState -> Text
V.sessionID) ([VoiceState] -> Maybe VoiceState)
-> (Guild -> [VoiceState]) -> Guild -> Maybe VoiceState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Guild -> [VoiceState]
voiceStates) (Guild -> Maybe VoiceState) -> Maybe Guild -> Maybe VoiceState
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Maybe Guild -> Maybe VoiceState)
-> Sem (Fail : r) (Maybe Guild)
-> Sem (Fail : r) (Maybe VoiceState)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Snowflake Guild -> Sem (Fail : r) (Maybe Guild)
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r (Maybe Guild)
getGuild Snowflake Guild
guildID
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ (((Maybe VoiceState, VoiceState) -> IO ()) -> IO ())
-> [(Maybe VoiceState, VoiceState) -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (((Maybe VoiceState, VoiceState) -> IO ())
-> (Maybe VoiceState, VoiceState) -> IO ()
forall a b. (a -> b) -> a -> b
$ (Maybe VoiceState
oldVoiceState, VoiceState
newVoiceState)) (EventHandlers -> [StoredEHType 'VoiceStateUpdateEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'VoiceStateUpdateEvt EventHandlers
eh)
handleEvent' EventHandlers
eh evt :: DispatchData
evt@(InteractionCreate Interaction
interaction) = do
  DispatchData -> Sem (Fail : r) ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache DispatchData
evt
  [IO ()] -> Sem (Fail : r) [IO ()]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([IO ()] -> Sem (Fail : r) [IO ()])
-> [IO ()] -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ ((Interaction -> IO ()) -> IO ())
-> [Interaction -> IO ()] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map ((Interaction -> IO ()) -> Interaction -> IO ()
forall a b. (a -> b) -> a -> b
$ Interaction
interaction) (EventHandlers -> [StoredEHType 'InteractionEvt]
forall (a :: EventType).
GetEventHandlers a =>
EventHandlers -> [StoredEHType a]
getEventHandlers @'InteractionEvt EventHandlers
eh)
handleEvent' EventHandlers
_ DispatchData
e = String -> Sem (Fail : r) [IO ()]
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Sem (Fail : r) [IO ()])
-> String -> Sem (Fail : r) [IO ()]
forall a b. (a -> b) -> a -> b
$ String
"Unhandled event: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> DispatchData -> String
forall a. Show a => a -> String
show DispatchData
e

updateCache :: P.Members '[CacheEff, P.Fail] r => DispatchData -> P.Sem r ()
updateCache :: DispatchData -> Sem r ()
updateCache (Ready ReadyData {User
$sel:user:ReadyData :: ReadyData -> User
user :: User
user, [UnavailableGuild]
$sel:guilds:ReadyData :: ReadyData -> [UnavailableGuild]
guilds :: [UnavailableGuild]
guilds}) = do
  User -> Sem r ()
forall (r :: EffectRow). Member CacheEff r => User -> Sem r ()
setBotUser User
user
  [Snowflake Guild] -> (Snowflake Guild -> Sem r ()) -> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ ((UnavailableGuild -> Snowflake Guild)
-> [UnavailableGuild] -> [Snowflake Guild]
forall a b. (a -> b) -> [a] -> [b]
map UnavailableGuild -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID [UnavailableGuild]
guilds) Snowflake Guild -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r ()
setUnavailableGuild
updateCache DispatchData
Resumed = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (ChannelCreate (DMChannel' DMChannel
chan)) =
  DMChannel -> Sem r ()
forall (r :: EffectRow). Member CacheEff r => DMChannel -> Sem r ()
setDM DMChannel
chan
updateCache (ChannelCreate (GuildChannel' GuildChannel
chan)) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild (GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan) (IsLabel
  "channels"
  (ASetter
     Guild
     Guild
     (SnowflakeMap GuildChannel)
     (SnowflakeMap GuildChannel))
ASetter
  Guild Guild (SnowflakeMap GuildChannel) (SnowflakeMap GuildChannel)
#channels ASetter
  Guild Guild (SnowflakeMap GuildChannel) (SnowflakeMap GuildChannel)
-> (SnowflakeMap GuildChannel -> SnowflakeMap GuildChannel)
-> Guild
-> Guild
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ GuildChannel
-> SnowflakeMap GuildChannel -> SnowflakeMap GuildChannel
forall a. HasID' a => a -> SnowflakeMap a -> SnowflakeMap a
SM.insert GuildChannel
chan)
updateCache (ChannelUpdate (DMChannel' DMChannel
chan)) =
  Snowflake DMChannel -> (DMChannel -> DMChannel) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake DMChannel -> (DMChannel -> DMChannel) -> Sem r ()
updateDM (DMChannel -> Snowflake DMChannel
forall b a. HasID b a => a -> Snowflake b
getID DMChannel
chan) (Updated DMChannel -> DMChannel -> DMChannel
forall a. Updateable a => Updated a -> a -> a
update DMChannel
Updated DMChannel
chan)
updateCache (ChannelUpdate (GuildChannel' GuildChannel
chan)) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild (GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan) (IsLabel
  "channels"
  (ASetter
     Guild
     Guild
     (SnowflakeMap GuildChannel)
     (SnowflakeMap GuildChannel))
ASetter
  Guild Guild (SnowflakeMap GuildChannel) (SnowflakeMap GuildChannel)
#channels ASetter
  Guild Guild (SnowflakeMap GuildChannel) (SnowflakeMap GuildChannel)
-> ((GuildChannel -> Identity GuildChannel)
    -> SnowflakeMap GuildChannel
    -> Identity (SnowflakeMap GuildChannel))
-> (GuildChannel -> Identity GuildChannel)
-> Guild
-> Identity Guild
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap GuildChannel)
-> Traversal'
     (SnowflakeMap GuildChannel) (IxValue (SnowflakeMap GuildChannel))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix (GuildChannel -> Snowflake GuildChannel
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan) ((GuildChannel -> Identity GuildChannel)
 -> Guild -> Identity Guild)
-> (GuildChannel -> GuildChannel) -> Guild -> Guild
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Updated GuildChannel -> GuildChannel -> GuildChannel
forall a. Updateable a => Updated a -> a -> a
update GuildChannel
Updated GuildChannel
chan)
updateCache (ChannelDelete (DMChannel' DMChannel
chan)) =
  Snowflake DMChannel -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake DMChannel -> Sem r ()
delDM (DMChannel -> Snowflake DMChannel
forall b a. HasID b a => a -> Snowflake b
getID DMChannel
chan)
updateCache (ChannelDelete (GuildChannel' GuildChannel
chan)) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild (GuildChannel -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan) (IsLabel
  "channels"
  (ASetter
     Guild
     Guild
     (SnowflakeMap GuildChannel)
     (SnowflakeMap GuildChannel))
ASetter
  Guild Guild (SnowflakeMap GuildChannel) (SnowflakeMap GuildChannel)
#channels ASetter
  Guild Guild (SnowflakeMap GuildChannel) (SnowflakeMap GuildChannel)
-> (SnowflakeMap GuildChannel -> SnowflakeMap GuildChannel)
-> Guild
-> Guild
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Index (SnowflakeMap GuildChannel)
-> SnowflakeMap GuildChannel -> SnowflakeMap GuildChannel
forall m. At m => Index m -> m -> m
sans (GuildChannel -> Snowflake GuildChannel
forall b a. HasID b a => a -> Snowflake b
getID GuildChannel
chan))
updateCache (GuildCreate Guild
guild) = do
  Bool
isNew <- Snowflake Guild -> Sem r Bool
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r Bool
isUnavailableGuild (Guild -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID Guild
guild)
  Bool -> Sem r () -> Sem r ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
isNew (Sem r () -> Sem r ()) -> Sem r () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ Snowflake Guild -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r ()
delUnavailableGuild (Guild -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID Guild
guild)
  Guild -> Sem r ()
forall (r :: EffectRow). Member CacheEff r => Guild -> Sem r ()
setGuild Guild
guild
  SnowflakeMap User -> (User -> Sem r ()) -> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ ([User] -> SnowflakeMap User
forall a. HasID' a => [a] -> SnowflakeMap a
SM.fromList (Guild
guild Guild -> Getting (Endo [User]) Guild User -> [User]
forall s a. s -> Getting (Endo [a]) s a -> [a]
^.. IsLabel
  "members"
  ((SnowflakeMap Member -> Const (Endo [User]) (SnowflakeMap Member))
   -> Guild -> Const (Endo [User]) Guild)
(SnowflakeMap Member -> Const (Endo [User]) (SnowflakeMap Member))
-> Guild -> Const (Endo [User]) Guild
#members ((SnowflakeMap Member -> Const (Endo [User]) (SnowflakeMap Member))
 -> Guild -> Const (Endo [User]) Guild)
-> ((User -> Const (Endo [User]) User)
    -> SnowflakeMap Member
    -> Const (Endo [User]) (SnowflakeMap Member))
-> Getting (Endo [User]) Guild User
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Member -> Const (Endo [User]) Member)
-> SnowflakeMap Member -> Const (Endo [User]) (SnowflakeMap Member)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((Member -> Const (Endo [User]) Member)
 -> SnowflakeMap Member
 -> Const (Endo [User]) (SnowflakeMap Member))
-> ((User -> Const (Endo [User]) User)
    -> Member -> Const (Endo [User]) Member)
-> (User -> Const (Endo [User]) User)
-> SnowflakeMap Member
-> Const (Endo [User]) (SnowflakeMap Member)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (User -> Const (Endo [User]) User)
-> Member -> Const (Endo [User]) Member
forall sup sub. Subtype sup sub => Lens sub sub sup sup
super)) User -> Sem r ()
forall (r :: EffectRow). Member CacheEff r => User -> Sem r ()
setUser
updateCache (GuildUpdate UpdatedGuild
guild) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild (UpdatedGuild -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID UpdatedGuild
guild) (Updated Guild -> Guild -> Guild
forall a. Updateable a => Updated a -> a -> a
update UpdatedGuild
Updated Guild
guild)
updateCache (GuildDelete UnavailableGuild {Snowflake Guild
id :: Snowflake Guild
$sel:id:UnavailableGuild :: UnavailableGuild -> Snowflake Guild
id, Bool
unavailable :: Bool
$sel:unavailable:UnavailableGuild :: UnavailableGuild -> Bool
unavailable}) =
  if Bool
unavailable
    then Snowflake Guild -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r ()
setUnavailableGuild Snowflake Guild
id
    else Snowflake Guild -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> Sem r ()
delGuild Snowflake Guild
id
updateCache (GuildEmojisUpdate GuildEmojisUpdateData {Snowflake Guild
guildID :: Snowflake Guild
$sel:guildID:GuildEmojisUpdateData :: GuildEmojisUpdateData -> Snowflake Guild
guildID, [Emoji]
emojis :: [Emoji]
$sel:emojis:GuildEmojisUpdateData :: GuildEmojisUpdateData -> [Emoji]
emojis}) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild Snowflake Guild
guildID (IsLabel
  "emojis"
  (ASetter Guild Guild (SnowflakeMap Emoji) (SnowflakeMap Emoji))
ASetter Guild Guild (SnowflakeMap Emoji) (SnowflakeMap Emoji)
#emojis ASetter Guild Guild (SnowflakeMap Emoji) (SnowflakeMap Emoji)
-> SnowflakeMap Emoji -> Guild -> Guild
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Emoji] -> SnowflakeMap Emoji
forall a. HasID' a => [a] -> SnowflakeMap a
SM.fromList [Emoji]
emojis)
updateCache (GuildMemberAdd Snowflake Guild
gid Member
member) = do
  User -> Sem r ()
forall (r :: EffectRow). Member CacheEff r => User -> Sem r ()
setUser (Member
member Member -> Getting User Member User -> User
forall s a. s -> Getting a s a -> a
^. Getting User Member User
forall sup sub. Subtype sup sub => Lens sub sub sup sup
super)
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild Snowflake Guild
gid (IsLabel
  "members"
  ((SnowflakeMap Member -> Identity (SnowflakeMap Member))
   -> Guild -> Identity Guild)
(SnowflakeMap Member -> Identity (SnowflakeMap Member))
-> Guild -> Identity Guild
#members ((SnowflakeMap Member -> Identity (SnowflakeMap Member))
 -> Guild -> Identity Guild)
-> ((Maybe Member -> Identity (Maybe Member))
    -> SnowflakeMap Member -> Identity (SnowflakeMap Member))
-> (Maybe Member -> Identity (Maybe Member))
-> Guild
-> Identity Guild
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Lens'
     (SnowflakeMap Member) (Maybe (IxValue (SnowflakeMap Member)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Member -> Snowflake Member
forall b a. HasID b a => a -> Snowflake b
getID Member
member) ((Maybe Member -> Identity (Maybe Member))
 -> Guild -> Identity Guild)
-> Member -> Guild -> Guild
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Member
member)
updateCache (GuildMemberRemove GuildMemberRemoveData {Snowflake Guild
guildID :: Snowflake Guild
$sel:guildID:GuildMemberRemoveData :: GuildMemberRemoveData -> Snowflake Guild
guildID, User
user :: User
$sel:user:GuildMemberRemoveData :: GuildMemberRemoveData -> User
user}) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild Snowflake Guild
guildID (IsLabel
  "members"
  ((SnowflakeMap Member -> Identity (SnowflakeMap Member))
   -> Guild -> Identity Guild)
(SnowflakeMap Member -> Identity (SnowflakeMap Member))
-> Guild -> Identity Guild
#members ((SnowflakeMap Member -> Identity (SnowflakeMap Member))
 -> Guild -> Identity Guild)
-> (SnowflakeMap Member -> SnowflakeMap Member) -> Guild -> Guild
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Index (SnowflakeMap Member)
-> SnowflakeMap Member -> SnowflakeMap Member
forall m. At m => Index m -> m -> m
sans (User -> Snowflake Member
forall b a. HasID b a => a -> Snowflake b
getID User
user))
updateCache (GuildMemberUpdate GuildMemberUpdateData {Snowflake Guild
guildID :: Snowflake Guild
$sel:guildID:GuildMemberUpdateData :: GuildMemberUpdateData -> Snowflake Guild
guildID, $sel:roles:GuildMemberUpdateData :: GuildMemberUpdateData -> AesonVector (Snowflake Role)
roles = AesonVector Vector (Snowflake Role)
roles, User
user :: User
$sel:user:GuildMemberUpdateData :: GuildMemberUpdateData -> User
user, Maybe Text
$sel:nick:GuildMemberUpdateData :: GuildMemberUpdateData -> Maybe Text
nick :: Maybe Text
nick}) = do
  User -> Sem r ()
forall (r :: EffectRow). Member CacheEff r => User -> Sem r ()
setUser User
user
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild Snowflake Guild
guildID (IsLabel
  "members"
  ((SnowflakeMap Member -> Identity (SnowflakeMap Member))
   -> Guild -> Identity Guild)
(SnowflakeMap Member -> Identity (SnowflakeMap Member))
-> Guild -> Identity Guild
#members ((SnowflakeMap Member -> Identity (SnowflakeMap Member))
 -> Guild -> Identity Guild)
-> ((Member -> Identity Member)
    -> SnowflakeMap Member -> Identity (SnowflakeMap Member))
-> (Member -> Identity Member)
-> Guild
-> Identity Guild
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (SnowflakeMap Member)
-> Traversal' (SnowflakeMap Member) (IxValue (SnowflakeMap Member))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix (User -> Snowflake Member
forall b a. HasID b a => a -> Snowflake b
getID User
user) ((Member -> Identity Member) -> Guild -> Identity Guild)
-> (Member -> Member) -> Guild -> Guild
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (IsLabel
  "roles"
  (ASetter
     Member Member (Vector (Snowflake Role)) (Vector (Snowflake Role)))
ASetter
  Member Member (Vector (Snowflake Role)) (Vector (Snowflake Role))
#roles ASetter
  Member Member (Vector (Snowflake Role)) (Vector (Snowflake Role))
-> Vector (Snowflake Role) -> Member -> Member
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Vector (Snowflake Role)
roles) (Member -> Member) -> (Member -> Member) -> Member -> Member
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (IsLabel "nick" (ASetter Member Member (Maybe Text) (Maybe Text))
ASetter Member Member (Maybe Text) (Maybe Text)
#nick ASetter Member Member (Maybe Text) (Maybe Text)
-> Maybe Text -> Member -> Member
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe Text
nick))
updateCache (GuildMembersChunk GuildMembersChunkData {Snowflake Guild
guildID :: Snowflake Guild
$sel:guildID:GuildMembersChunkData :: GuildMembersChunkData -> Snowflake Guild
guildID, [Member]
members :: [Member]
$sel:members:GuildMembersChunkData :: GuildMembersChunkData -> [Member]
members}) =
  (Member -> Sem r ()) -> [Member] -> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (DispatchData -> Sem r ()
forall (r :: EffectRow).
Members '[CacheEff, Fail] r =>
DispatchData -> Sem r ()
updateCache (DispatchData -> Sem r ())
-> (Member -> DispatchData) -> Member -> Sem r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Snowflake Guild -> Member -> DispatchData
GuildMemberAdd Snowflake Guild
guildID) [Member]
members
updateCache (GuildRoleCreate GuildRoleData {Snowflake Guild
guildID :: Snowflake Guild
$sel:guildID:GuildRoleData :: GuildRoleData -> Snowflake Guild
guildID, Role
role :: Role
$sel:role:GuildRoleData :: GuildRoleData -> Role
role}) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild Snowflake Guild
guildID (IsLabel
  "roles"
  (ASetter Guild Guild (SnowflakeMap Role) (SnowflakeMap Role))
ASetter Guild Guild (SnowflakeMap Role) (SnowflakeMap Role)
#roles ASetter Guild Guild (SnowflakeMap Role) (SnowflakeMap Role)
-> (SnowflakeMap Role -> SnowflakeMap Role) -> Guild -> Guild
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Role -> SnowflakeMap Role -> SnowflakeMap Role
forall a. HasID' a => a -> SnowflakeMap a -> SnowflakeMap a
SM.insert Role
role)
updateCache (GuildRoleUpdate GuildRoleData {Snowflake Guild
guildID :: Snowflake Guild
$sel:guildID:GuildRoleData :: GuildRoleData -> Snowflake Guild
guildID, Role
role :: Role
$sel:role:GuildRoleData :: GuildRoleData -> Role
role}) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild Snowflake Guild
guildID (IsLabel
  "roles"
  (ASetter Guild Guild (SnowflakeMap Role) (SnowflakeMap Role))
ASetter Guild Guild (SnowflakeMap Role) (SnowflakeMap Role)
#roles ASetter Guild Guild (SnowflakeMap Role) (SnowflakeMap Role)
-> (SnowflakeMap Role -> SnowflakeMap Role) -> Guild -> Guild
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Role -> SnowflakeMap Role -> SnowflakeMap Role
forall a. HasID' a => a -> SnowflakeMap a -> SnowflakeMap a
SM.insert Role
role)
updateCache (GuildRoleDelete GuildRoleDeleteData {Snowflake Guild
guildID :: Snowflake Guild
$sel:guildID:GuildRoleDeleteData :: GuildRoleDeleteData -> Snowflake Guild
guildID, Snowflake Role
roleID :: Snowflake Role
$sel:roleID:GuildRoleDeleteData :: GuildRoleDeleteData -> Snowflake Role
roleID}) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild Snowflake Guild
guildID (IsLabel
  "roles"
  (ASetter Guild Guild (SnowflakeMap Role) (SnowflakeMap Role))
ASetter Guild Guild (SnowflakeMap Role) (SnowflakeMap Role)
#roles ASetter Guild Guild (SnowflakeMap Role) (SnowflakeMap Role)
-> (SnowflakeMap Role -> SnowflakeMap Role) -> Guild -> Guild
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Index (SnowflakeMap Role) -> SnowflakeMap Role -> SnowflakeMap Role
forall m. At m => Index m -> m -> m
sans Index (SnowflakeMap Role)
Snowflake Role
roleID)
updateCache (MessageCreate !Message
msg !Maybe User
_ !Maybe Member
_) =
  Message -> Sem r ()
forall (r :: EffectRow). Member CacheEff r => Message -> Sem r ()
setMessage Message
msg
-- I think it's for the best not to cache things here, instead the end user
-- can just cache manually which users and members they want
updateCache (MessageUpdate UpdatedMessage
msg !Maybe User
_ !Maybe Member
_) =
  Snowflake Message -> (Message -> Message) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> (Message -> Message) -> Sem r ()
updateMessage (UpdatedMessage -> Snowflake Message
forall b a. HasID b a => a -> Snowflake b
getID UpdatedMessage
msg) (Updated Message -> Message -> Message
forall a. Updateable a => Updated a -> a -> a
update UpdatedMessage
Updated Message
msg)
updateCache (MessageDelete MessageDeleteData {Snowflake Message
id :: Snowflake Message
$sel:id:MessageDeleteData :: MessageDeleteData -> Snowflake Message
id}) = Snowflake Message -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> Sem r ()
delMessage Snowflake Message
id
updateCache (MessageDeleteBulk MessageDeleteBulkData {[Snowflake Message]
ids :: [Snowflake Message]
$sel:ids:MessageDeleteBulkData :: MessageDeleteBulkData -> [Snowflake Message]
ids}) =
  [Snowflake Message] -> (Snowflake Message -> Sem r ()) -> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Snowflake Message]
ids Snowflake Message -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> Sem r ()
delMessage
updateCache (MessageReactionAdd ReactionEvtData
reaction) = do
  Bool
isMe <- (\Maybe User
u -> Snowflake User -> Maybe (Snowflake User)
forall a. a -> Maybe a
Just (ReactionEvtData -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID @User ReactionEvtData
reaction) Maybe (Snowflake User) -> Maybe (Snowflake User) -> Bool
forall a. Eq a => a -> a -> Bool
== (forall a. HasID User a => a -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID @User (User -> Snowflake User) -> Maybe User -> Maybe (Snowflake User)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe User
u)) (Maybe User -> Bool) -> Sem r (Maybe User) -> Sem r Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sem r (Maybe User)
forall (r :: EffectRow). Member CacheEff r => Sem r (Maybe User)
getBotUser
  Snowflake Message -> (Message -> Message) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> (Message -> Message) -> Sem r ()
updateMessage
    (ReactionEvtData -> Snowflake Message
forall b a. HasID b a => a -> Snowflake b
getID ReactionEvtData
reaction)
    ( \Message
m ->
        case Message
m Message -> Getting [Reaction] Message [Reaction] -> [Reaction]
forall s a. s -> Getting a s a -> a
^. IsLabel "reactions" (Getting [Reaction] Message [Reaction])
Getting [Reaction] Message [Reaction]
#reactions [Reaction] -> ([Reaction] -> [Reaction]) -> [Reaction]
forall a b. a -> (a -> b) -> b
& (Reaction -> Bool) -> [Reaction] -> [Reaction]
forall a. (a -> Bool) -> [a] -> [a]
filter ((RawEmoji -> RawEmoji -> Bool
forall a. Eq a => a -> a -> Bool
== (ReactionEvtData
reaction ReactionEvtData
-> Getting RawEmoji ReactionEvtData RawEmoji -> RawEmoji
forall s a. s -> Getting a s a -> a
^. IsLabel "emoji" (Getting RawEmoji ReactionEvtData RawEmoji)
Getting RawEmoji ReactionEvtData RawEmoji
#emoji)) (RawEmoji -> Bool) -> (Reaction -> RawEmoji) -> Reaction -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reaction -> Getting RawEmoji Reaction RawEmoji -> RawEmoji
forall s a. s -> Getting a s a -> a
^. IsLabel "emoji" (Getting RawEmoji Reaction RawEmoji)
Getting RawEmoji Reaction RawEmoji
#emoji)) of
          [] -> Message
m Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& IsLabel "reactions" (ASetter Message Message [Reaction] [Reaction])
ASetter Message Message [Reaction] [Reaction]
#reactions ASetter Message Message [Reaction] [Reaction]
-> [Reaction] -> Message -> Message
forall a s t. Semigroup a => ASetter s t a a -> a -> s -> t
<>~ [Integer -> Bool -> RawEmoji -> Reaction
Reaction Integer
1 Bool
isMe (ReactionEvtData
reaction ReactionEvtData
-> Getting RawEmoji ReactionEvtData RawEmoji -> RawEmoji
forall s a. s -> Getting a s a -> a
^. IsLabel "emoji" (Getting RawEmoji ReactionEvtData RawEmoji)
Getting RawEmoji ReactionEvtData RawEmoji
#emoji)]
          [Reaction]
_ ->
            Message
m Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& IsLabel "reactions" (ASetter Message Message [Reaction] [Reaction])
ASetter Message Message [Reaction] [Reaction]
#reactions ASetter Message Message [Reaction] [Reaction]
-> ((Reaction -> Identity Reaction)
    -> [Reaction] -> Identity [Reaction])
-> (Reaction -> Identity Reaction)
-> Message
-> Identity Message
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reaction -> Identity Reaction)
-> [Reaction] -> Identity [Reaction]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((Reaction -> Identity Reaction)
 -> [Reaction] -> Identity [Reaction])
-> ((Reaction -> Identity Reaction)
    -> Reaction -> Identity Reaction)
-> (Reaction -> Identity Reaction)
-> [Reaction]
-> Identity [Reaction]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reaction -> Bool)
-> (Reaction -> Identity Reaction) -> Reaction -> Identity Reaction
forall (p :: * -> * -> *) (f :: * -> *) a.
(Choice p, Applicative f) =>
(a -> Bool) -> Optic' p f a a
filtered ((RawEmoji -> RawEmoji -> Bool
forall a. Eq a => a -> a -> Bool
== (ReactionEvtData
reaction ReactionEvtData
-> Getting RawEmoji ReactionEvtData RawEmoji -> RawEmoji
forall s a. s -> Getting a s a -> a
^. IsLabel "emoji" (Getting RawEmoji ReactionEvtData RawEmoji)
Getting RawEmoji ReactionEvtData RawEmoji
#emoji)) (RawEmoji -> Bool) -> (Reaction -> RawEmoji) -> Reaction -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reaction -> Getting RawEmoji Reaction RawEmoji -> RawEmoji
forall s a. s -> Getting a s a -> a
^. IsLabel "emoji" (Getting RawEmoji Reaction RawEmoji)
Getting RawEmoji Reaction RawEmoji
#emoji))
              ((Reaction -> Identity Reaction) -> Message -> Identity Message)
-> (Reaction -> Reaction) -> Message -> Message
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (IsLabel "count" (ASetter Reaction Reaction Integer Integer)
ASetter Reaction Reaction Integer Integer
#count ASetter Reaction Reaction Integer Integer
-> Integer -> Reaction -> Reaction
forall a s t. Num a => ASetter s t a a -> a -> s -> t
+~ Integer
1) (Reaction -> Reaction)
-> (Reaction -> Reaction) -> Reaction -> Reaction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (IsLabel "me" (ASetter Reaction Reaction Bool Bool)
ASetter Reaction Reaction Bool Bool
#me ASetter Reaction Reaction Bool Bool -> Bool -> Reaction -> Reaction
forall s t. ASetter s t Bool Bool -> Bool -> s -> t
||~ Bool
isMe)
    )
updateCache (MessageReactionRemove ReactionEvtData
reaction) = do
  Bool
isMe <- (\Maybe User
u -> Snowflake User -> Maybe (Snowflake User)
forall a. a -> Maybe a
Just (ReactionEvtData -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID @User ReactionEvtData
reaction) Maybe (Snowflake User) -> Maybe (Snowflake User) -> Bool
forall a. Eq a => a -> a -> Bool
== (forall a. HasID User a => a -> Snowflake User
forall b a. HasID b a => a -> Snowflake b
getID @User (User -> Snowflake User) -> Maybe User -> Maybe (Snowflake User)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe User
u)) (Maybe User -> Bool) -> Sem r (Maybe User) -> Sem r Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sem r (Maybe User)
forall (r :: EffectRow). Member CacheEff r => Sem r (Maybe User)
getBotUser
  Snowflake Message -> (Message -> Message) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> (Message -> Message) -> Sem r ()
updateMessage
    (ReactionEvtData -> Snowflake Message
forall b a. HasID b a => a -> Snowflake b
getID ReactionEvtData
reaction)
    ( \Message
m ->
        Message
m
          Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& IsLabel "reactions" (ASetter Message Message [Reaction] [Reaction])
ASetter Message Message [Reaction] [Reaction]
#reactions ASetter Message Message [Reaction] [Reaction]
-> ((Reaction -> Identity Reaction)
    -> [Reaction] -> Identity [Reaction])
-> (Reaction -> Identity Reaction)
-> Message
-> Identity Message
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reaction -> Identity Reaction)
-> [Reaction] -> Identity [Reaction]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((Reaction -> Identity Reaction)
 -> [Reaction] -> Identity [Reaction])
-> ((Reaction -> Identity Reaction)
    -> Reaction -> Identity Reaction)
-> (Reaction -> Identity Reaction)
-> [Reaction]
-> Identity [Reaction]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reaction -> Bool)
-> (Reaction -> Identity Reaction) -> Reaction -> Identity Reaction
forall (p :: * -> * -> *) (f :: * -> *) a.
(Choice p, Applicative f) =>
(a -> Bool) -> Optic' p f a a
filtered ((RawEmoji -> RawEmoji -> Bool
forall a. Eq a => a -> a -> Bool
== (ReactionEvtData
reaction ReactionEvtData
-> Getting RawEmoji ReactionEvtData RawEmoji -> RawEmoji
forall s a. s -> Getting a s a -> a
^. IsLabel "emoji" (Getting RawEmoji ReactionEvtData RawEmoji)
Getting RawEmoji ReactionEvtData RawEmoji
#emoji)) (RawEmoji -> Bool) -> (Reaction -> RawEmoji) -> Reaction -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reaction -> Getting RawEmoji Reaction RawEmoji -> RawEmoji
forall s a. s -> Getting a s a -> a
^. IsLabel "emoji" (Getting RawEmoji Reaction RawEmoji)
Getting RawEmoji Reaction RawEmoji
#emoji))
            ((Reaction -> Identity Reaction) -> Message -> Identity Message)
-> (Reaction -> Reaction) -> Message -> Message
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (IsLabel "count" (ASetter Reaction Reaction Integer Integer)
ASetter Reaction Reaction Integer Integer
#count ASetter Reaction Reaction Integer Integer
-> Integer -> Reaction -> Reaction
forall a s t. Num a => ASetter s t a a -> a -> s -> t
-~ Integer
1) (Reaction -> Reaction)
-> (Reaction -> Reaction) -> Reaction -> Reaction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (IsLabel "me" (ASetter Reaction Reaction Bool Bool)
ASetter Reaction Reaction Bool Bool
#me ASetter Reaction Reaction Bool Bool -> Bool -> Reaction -> Reaction
forall s t. ASetter s t Bool Bool -> Bool -> s -> t
&&~ Bool -> Bool
not Bool
isMe)
          Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& IsLabel "reactions" (ASetter Message Message [Reaction] [Reaction])
ASetter Message Message [Reaction] [Reaction]
#reactions ASetter Message Message [Reaction] [Reaction]
-> ([Reaction] -> [Reaction]) -> Message -> Message
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (Reaction -> Bool) -> [Reaction] -> [Reaction]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Reaction
r -> Reaction
r Reaction -> Getting Integer Reaction Integer -> Integer
forall s a. s -> Getting a s a -> a
^. IsLabel "count" (Getting Integer Reaction Integer)
Getting Integer Reaction Integer
#count Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0)
    )
updateCache (MessageReactionRemoveAll MessageReactionRemoveAllData {Snowflake Message
messageID :: Snowflake Message
$sel:messageID:MessageReactionRemoveAllData :: MessageReactionRemoveAllData -> Snowflake Message
messageID}) =
  Snowflake Message -> (Message -> Message) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Message -> (Message -> Message) -> Sem r ()
updateMessage Snowflake Message
messageID (IsLabel "reactions" (ASetter Message Message [Reaction] [Reaction])
ASetter Message Message [Reaction] [Reaction]
#reactions ASetter Message Message [Reaction] [Reaction]
-> [Reaction] -> Message -> Message
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Reaction]
forall a. Monoid a => a
mempty)
updateCache (PresenceUpdate PresenceUpdateData {Snowflake User
userID :: Snowflake User
$sel:userID:PresenceUpdateData :: PresenceUpdateData -> Snowflake User
userID, Presence
presence :: Presence
$sel:presence:PresenceUpdateData :: PresenceUpdateData -> Presence
presence}) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild (Presence -> Snowflake Guild
forall b a. HasID b a => a -> Snowflake b
getID Presence
presence) (IsLabel
  "presences"
  ((HashMap (Snowflake User) Presence
    -> Identity (HashMap (Snowflake User) Presence))
   -> Guild -> Identity Guild)
(HashMap (Snowflake User) Presence
 -> Identity (HashMap (Snowflake User) Presence))
-> Guild -> Identity Guild
#presences ((HashMap (Snowflake User) Presence
  -> Identity (HashMap (Snowflake User) Presence))
 -> Guild -> Identity Guild)
-> ((Maybe Presence -> Identity (Maybe Presence))
    -> HashMap (Snowflake User) Presence
    -> Identity (HashMap (Snowflake User) Presence))
-> (Maybe Presence -> Identity (Maybe Presence))
-> Guild
-> Identity Guild
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (HashMap (Snowflake User) Presence)
-> Lens'
     (HashMap (Snowflake User) Presence)
     (Maybe (IxValue (HashMap (Snowflake User) Presence)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (HashMap (Snowflake User) Presence)
Snowflake User
userID ((Maybe Presence -> Identity (Maybe Presence))
 -> Guild -> Identity Guild)
-> Presence -> Guild -> Guild
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Presence
presence)
updateCache (UserUpdate User
user) = User -> Sem r ()
forall (r :: EffectRow). Member CacheEff r => User -> Sem r ()
setBotUser User
user
-- we don't handle group channels currently
updateCache (ChannelCreate (GroupChannel' GroupChannel
_)) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (ChannelUpdate (GroupChannel' GroupChannel
_)) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (ChannelDelete (GroupChannel' GroupChannel
_)) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
-- these don't modify state
updateCache (GuildBanAdd BanData
_) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (GuildBanRemove BanData
_) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (GuildIntegrationsUpdate GuildIntegrationsUpdateData
_) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (TypingStart TypingStartData
_) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (ChannelPinsUpdate ChannelPinsUpdateData
_) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (WebhooksUpdate WebhooksUpdateData
_) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (InviteCreate InviteCreateData
_) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (InviteDelete InviteDeleteData
_) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (VoiceStateUpdate voiceState :: VoiceState
voiceState@V.VoiceState {$sel:guildID:VoiceState :: VoiceState -> Maybe (Snowflake Guild)
guildID = Just Snowflake Guild
guildID}) =
  Snowflake Guild -> (Guild -> Guild) -> Sem r ()
forall (r :: EffectRow).
Member CacheEff r =>
Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild Snowflake Guild
guildID (IsLabel
  "voiceStates" (ASetter Guild Guild [VoiceState] [VoiceState])
ASetter Guild Guild [VoiceState] [VoiceState]
#voiceStates ASetter Guild Guild [VoiceState] [VoiceState]
-> ([VoiceState] -> [VoiceState]) -> Guild -> Guild
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ [VoiceState] -> [VoiceState]
updateVoiceStates)
  where
    updateVoiceStates :: [VoiceState] -> [VoiceState]
updateVoiceStates [] = [VoiceState
voiceState]
    updateVoiceStates (VoiceState
x : [VoiceState]
xs)
      | VoiceState -> Text
V.sessionID VoiceState
x Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== VoiceState -> Text
V.sessionID VoiceState
voiceState = VoiceState
voiceState VoiceState -> [VoiceState] -> [VoiceState]
forall a. a -> [a] -> [a]
: [VoiceState]
xs
      | Bool
otherwise = VoiceState
x VoiceState -> [VoiceState] -> [VoiceState]
forall a. a -> [a] -> [a]
: [VoiceState] -> [VoiceState]
updateVoiceStates [VoiceState]
xs

-- we don't handle voice server update and direct voice connections currently
updateCache (VoiceStateUpdate V.VoiceState {$sel:guildID:VoiceState :: VoiceState -> Maybe (Snowflake Guild)
guildID = Maybe (Snowflake Guild)
Nothing}) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateCache (VoiceServerUpdate VoiceServerUpdateData
_) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
-- we don't update the cache from interactions
-- TODO: should we?
updateCache (InteractionCreate Interaction
_) = () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()