-- | Interaction related utilities
module Calamity.Interactions.Utils (
  respond,
  respondEphemeral,
  followUp,
  followUpEphemeral,
  edit,
  defer,
  deferEphemeral,
  deferComponent,
  pushModal,
  userLocalState,
) where

import Calamity.HTTP
import Calamity.Interactions.Eff (InteractionEff, getInteractionID, getInteractionToken, getInteractionUser, getApplicationID)
import Calamity.Metrics.Eff (MetricEff)
import Calamity.Types.LogEff (LogEff)
import Calamity.Types.Model.Channel.Component (Component (ActionRow'))
import Calamity.Types.Model.User (User)
import Calamity.Types.Snowflake (Snowflake)
import Calamity.Types.Tellable
import Control.Lens ((^.))
import qualified Data.HashMap.Strict as H
import Data.Text (Text)
import qualified Polysemy as P
import qualified Polysemy.State as P
import System.Random (getStdRandom, uniform)

{- | Provide local state semantics to a running view, the state is keyed to the
 user invoking the interaction
-}
userLocalState ::
  forall r s a.
  P.Member InteractionEff r =>
  -- | Initial state
  s ->
  P.Sem (P.State s ': r) a ->
  P.Sem r a
userLocalState :: s -> Sem (State s : r) a -> Sem r a
userLocalState s
s =
  HashMap (Snowflake User) s
-> Sem (State (HashMap (Snowflake User) s) : r) a -> Sem r a
forall s (r :: [(* -> *) -> * -> *]) a.
s -> Sem (State s : r) a -> Sem r a
P.evalState HashMap (Snowflake User) s
forall k v. HashMap k v
H.empty
    (Sem (State (HashMap (Snowflake User) s) : r) a -> Sem r a)
-> (Sem (State s : r) a
    -> Sem (State (HashMap (Snowflake User) s) : r) a)
-> Sem (State s : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (rInitial :: [(* -> *) -> * -> *]) x.
 State s (Sem rInitial) x
 -> Sem (State (HashMap (Snowflake User) s) : r) x)
-> Sem (State s : r) a
-> Sem (State (HashMap (Snowflake User) s) : r) a
forall (e1 :: (* -> *) -> * -> *) (e2 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
FirstOrder e1 "reinterpret" =>
(forall (rInitial :: [(* -> *) -> * -> *]) x.
 e1 (Sem rInitial) x -> Sem (e2 : r) x)
-> Sem (e1 : r) a -> Sem (e2 : r) a
P.reinterpret @(P.State s) @(P.State (H.HashMap (Snowflake User) s))
      ( \case
          State s (Sem rInitial) x
P.Get -> do
            Snowflake User
uid <- Sem (State (HashMap (Snowflake User) s) : r) (Snowflake User)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake User)
getInteractionUser
            (HashMap (Snowflake User) s -> s)
-> Sem (State (HashMap (Snowflake User) s) : r) s
forall s a (r :: [(* -> *) -> * -> *]).
Member (State s) r =>
(s -> a) -> Sem r a
P.gets (s -> Snowflake User -> HashMap (Snowflake User) s -> s
forall k v. (Eq k, Hashable k) => v -> k -> HashMap k v -> v
H.lookupDefault s
s Snowflake User
uid)
          P.Put s
s -> do
            Snowflake User
uid <- Sem (State (HashMap (Snowflake User) s) : r) (Snowflake User)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake User)
getInteractionUser
            (HashMap (Snowflake User) s -> HashMap (Snowflake User) s)
-> Sem (State (HashMap (Snowflake User) s) : r) ()
forall s (r :: [(* -> *) -> * -> *]).
Member (State s) r =>
(s -> s) -> Sem r ()
P.modify' (Snowflake User
-> s -> HashMap (Snowflake User) s -> HashMap (Snowflake User) s
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
H.insert Snowflake User
uid s
s)
      )

-- | Respond to an interaction with a globally visible message
respond :: forall t r.
  (P.Members '[InteractionEff, RatelimitEff, TokenEff, LogEff, MetricEff, P.Embed IO] r, ToMessage t) =>
  t ->
  P.Sem r (Either RestError ())
respond :: t -> Sem r (Either RestError ())
respond (t -> CreateMessageOptions
forall a. ToMessage a => a -> CreateMessageOptions
runToMessage -> CreateMessageOptions
msg) =
  let opts :: InteractionCallbackMessageOptions
opts =
        InteractionCallbackMessageOptions :: Maybe Bool
-> Maybe Text
-> Maybe [Embed]
-> Maybe AllowedMentions
-> Maybe Bool
-> Maybe Bool
-> Maybe [Component]
-> Maybe [CreateMessageAttachment]
-> InteractionCallbackMessageOptions
InteractionCallbackMessageOptions
          { $sel:tts:InteractionCallbackMessageOptions :: Maybe Bool
tts = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe Bool) CreateMessageOptions (Maybe Bool)
-> Maybe Bool
forall s a. s -> Getting a s a -> a
^. IsLabel
  "tts" (Getting (Maybe Bool) CreateMessageOptions (Maybe Bool))
Getting (Maybe Bool) CreateMessageOptions (Maybe Bool)
#tts
          , $sel:content:InteractionCallbackMessageOptions :: Maybe Text
content = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe Text) CreateMessageOptions (Maybe Text)
-> Maybe Text
forall s a. s -> Getting a s a -> a
^. IsLabel
  "content" (Getting (Maybe Text) CreateMessageOptions (Maybe Text))
Getting (Maybe Text) CreateMessageOptions (Maybe Text)
#content
          , $sel:embeds:InteractionCallbackMessageOptions :: Maybe [Embed]
embeds = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed])
-> Maybe [Embed]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "embeds"
  (Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed]))
Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed])
#embeds
          , $sel:allowedMentions:InteractionCallbackMessageOptions :: Maybe AllowedMentions
allowedMentions = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe AllowedMentions)
     CreateMessageOptions
     (Maybe AllowedMentions)
-> Maybe AllowedMentions
forall s a. s -> Getting a s a -> a
^. IsLabel
  "allowedMentions"
  (Getting
     (Maybe AllowedMentions)
     CreateMessageOptions
     (Maybe AllowedMentions))
Getting
  (Maybe AllowedMentions)
  CreateMessageOptions
  (Maybe AllowedMentions)
#allowedMentions
          , $sel:ephemeral:InteractionCallbackMessageOptions :: Maybe Bool
ephemeral = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
          , $sel:suppressEmbeds:InteractionCallbackMessageOptions :: Maybe Bool
suppressEmbeds = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
          , $sel:components:InteractionCallbackMessageOptions :: Maybe [Component]
components = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe [Component]) CreateMessageOptions (Maybe [Component])
-> Maybe [Component]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "components"
  (Getting
     (Maybe [Component]) CreateMessageOptions (Maybe [Component]))
Getting
  (Maybe [Component]) CreateMessageOptions (Maybe [Component])
#components
          , $sel:attachments:InteractionCallbackMessageOptions :: Maybe [CreateMessageAttachment]
attachments = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe [CreateMessageAttachment])
     CreateMessageOptions
     (Maybe [CreateMessageAttachment])
-> Maybe [CreateMessageAttachment]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "attachments"
  (Getting
     (Maybe [CreateMessageAttachment])
     CreateMessageOptions
     (Maybe [CreateMessageAttachment]))
Getting
  (Maybe [CreateMessageAttachment])
  CreateMessageOptions
  (Maybe [CreateMessageAttachment])
#attachments
          }
   in do
        Snowflake Interaction
interactionID <- Sem r (Snowflake Interaction)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake Interaction)
getInteractionID
        InteractionToken
interactionToken <- Sem r InteractionToken
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r InteractionToken
getInteractionToken
        InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall (r :: [(* -> *) -> * -> *]) a.
(Members '[RatelimitEff, TokenEff, LogEff, MetricEff, Embed IO] r,
 Request a, ReadResponse (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (InteractionRequest ()
 -> Sem r (Either RestError (Result (InteractionRequest ()))))
-> InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall a b. (a -> b) -> a -> b
$ Snowflake Interaction
-> InteractionToken
-> InteractionCallbackMessageOptions
-> InteractionRequest ()
forall i.
HasID Interaction i =>
i
-> InteractionToken
-> InteractionCallbackMessageOptions
-> InteractionRequest ()
CreateResponseMessage Snowflake Interaction
interactionID InteractionToken
interactionToken InteractionCallbackMessageOptions
opts

-- | Respond to an interaction with an ephemeral message
respondEphemeral :: forall t r.
  (P.Members '[InteractionEff, RatelimitEff, TokenEff, LogEff, MetricEff, P.Embed IO] r, ToMessage t) =>
  t ->
  P.Sem r (Either RestError ())
respondEphemeral :: t -> Sem r (Either RestError ())
respondEphemeral (t -> CreateMessageOptions
forall a. ToMessage a => a -> CreateMessageOptions
runToMessage -> CreateMessageOptions
msg) =
  let opts :: InteractionCallbackMessageOptions
opts =
        InteractionCallbackMessageOptions :: Maybe Bool
-> Maybe Text
-> Maybe [Embed]
-> Maybe AllowedMentions
-> Maybe Bool
-> Maybe Bool
-> Maybe [Component]
-> Maybe [CreateMessageAttachment]
-> InteractionCallbackMessageOptions
InteractionCallbackMessageOptions
          { $sel:tts:InteractionCallbackMessageOptions :: Maybe Bool
tts = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe Bool) CreateMessageOptions (Maybe Bool)
-> Maybe Bool
forall s a. s -> Getting a s a -> a
^. IsLabel
  "tts" (Getting (Maybe Bool) CreateMessageOptions (Maybe Bool))
Getting (Maybe Bool) CreateMessageOptions (Maybe Bool)
#tts
          , $sel:content:InteractionCallbackMessageOptions :: Maybe Text
content = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe Text) CreateMessageOptions (Maybe Text)
-> Maybe Text
forall s a. s -> Getting a s a -> a
^. IsLabel
  "content" (Getting (Maybe Text) CreateMessageOptions (Maybe Text))
Getting (Maybe Text) CreateMessageOptions (Maybe Text)
#content
          , $sel:embeds:InteractionCallbackMessageOptions :: Maybe [Embed]
embeds = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed])
-> Maybe [Embed]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "embeds"
  (Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed]))
Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed])
#embeds
          , $sel:allowedMentions:InteractionCallbackMessageOptions :: Maybe AllowedMentions
allowedMentions = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe AllowedMentions)
     CreateMessageOptions
     (Maybe AllowedMentions)
-> Maybe AllowedMentions
forall s a. s -> Getting a s a -> a
^. IsLabel
  "allowedMentions"
  (Getting
     (Maybe AllowedMentions)
     CreateMessageOptions
     (Maybe AllowedMentions))
Getting
  (Maybe AllowedMentions)
  CreateMessageOptions
  (Maybe AllowedMentions)
#allowedMentions
          , $sel:ephemeral:InteractionCallbackMessageOptions :: Maybe Bool
ephemeral = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
          , $sel:suppressEmbeds:InteractionCallbackMessageOptions :: Maybe Bool
suppressEmbeds = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
          , $sel:components:InteractionCallbackMessageOptions :: Maybe [Component]
components = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe [Component]) CreateMessageOptions (Maybe [Component])
-> Maybe [Component]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "components"
  (Getting
     (Maybe [Component]) CreateMessageOptions (Maybe [Component]))
Getting
  (Maybe [Component]) CreateMessageOptions (Maybe [Component])
#components
          , $sel:attachments:InteractionCallbackMessageOptions :: Maybe [CreateMessageAttachment]
attachments = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe [CreateMessageAttachment])
     CreateMessageOptions
     (Maybe [CreateMessageAttachment])
-> Maybe [CreateMessageAttachment]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "attachments"
  (Getting
     (Maybe [CreateMessageAttachment])
     CreateMessageOptions
     (Maybe [CreateMessageAttachment]))
Getting
  (Maybe [CreateMessageAttachment])
  CreateMessageOptions
  (Maybe [CreateMessageAttachment])
#attachments
          }
   in do
        Snowflake Interaction
interactionID <- Sem r (Snowflake Interaction)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake Interaction)
getInteractionID
        InteractionToken
interactionToken <- Sem r InteractionToken
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r InteractionToken
getInteractionToken
        InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall (r :: [(* -> *) -> * -> *]) a.
(Members '[RatelimitEff, TokenEff, LogEff, MetricEff, Embed IO] r,
 Request a, ReadResponse (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (InteractionRequest ()
 -> Sem r (Either RestError (Result (InteractionRequest ()))))
-> InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall a b. (a -> b) -> a -> b
$ Snowflake Interaction
-> InteractionToken
-> InteractionCallbackMessageOptions
-> InteractionRequest ()
forall i.
HasID Interaction i =>
i
-> InteractionToken
-> InteractionCallbackMessageOptions
-> InteractionRequest ()
CreateResponseMessage Snowflake Interaction
interactionID InteractionToken
interactionToken InteractionCallbackMessageOptions
opts

-- | Respond to an interaction by editing the message that triggered the interaction
edit :: forall t r.
  (P.Members '[InteractionEff, RatelimitEff, TokenEff, LogEff, MetricEff, P.Embed IO] r, ToMessage t) =>
  t ->
  P.Sem r (Either RestError ())
edit :: t -> Sem r (Either RestError ())
edit (t -> CreateMessageOptions
forall a. ToMessage a => a -> CreateMessageOptions
runToMessage -> CreateMessageOptions
msg) =
  let opts :: InteractionCallbackMessageOptions
opts =
        InteractionCallbackMessageOptions :: Maybe Bool
-> Maybe Text
-> Maybe [Embed]
-> Maybe AllowedMentions
-> Maybe Bool
-> Maybe Bool
-> Maybe [Component]
-> Maybe [CreateMessageAttachment]
-> InteractionCallbackMessageOptions
InteractionCallbackMessageOptions
          { $sel:tts:InteractionCallbackMessageOptions :: Maybe Bool
tts = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe Bool) CreateMessageOptions (Maybe Bool)
-> Maybe Bool
forall s a. s -> Getting a s a -> a
^. IsLabel
  "tts" (Getting (Maybe Bool) CreateMessageOptions (Maybe Bool))
Getting (Maybe Bool) CreateMessageOptions (Maybe Bool)
#tts
          , $sel:content:InteractionCallbackMessageOptions :: Maybe Text
content = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe Text) CreateMessageOptions (Maybe Text)
-> Maybe Text
forall s a. s -> Getting a s a -> a
^. IsLabel
  "content" (Getting (Maybe Text) CreateMessageOptions (Maybe Text))
Getting (Maybe Text) CreateMessageOptions (Maybe Text)
#content
          , $sel:embeds:InteractionCallbackMessageOptions :: Maybe [Embed]
embeds = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed])
-> Maybe [Embed]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "embeds"
  (Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed]))
Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed])
#embeds
          , $sel:allowedMentions:InteractionCallbackMessageOptions :: Maybe AllowedMentions
allowedMentions = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe AllowedMentions)
     CreateMessageOptions
     (Maybe AllowedMentions)
-> Maybe AllowedMentions
forall s a. s -> Getting a s a -> a
^. IsLabel
  "allowedMentions"
  (Getting
     (Maybe AllowedMentions)
     CreateMessageOptions
     (Maybe AllowedMentions))
Getting
  (Maybe AllowedMentions)
  CreateMessageOptions
  (Maybe AllowedMentions)
#allowedMentions
          , $sel:ephemeral:InteractionCallbackMessageOptions :: Maybe Bool
ephemeral = Maybe Bool
forall a. Maybe a
Nothing
          , $sel:suppressEmbeds:InteractionCallbackMessageOptions :: Maybe Bool
suppressEmbeds = Maybe Bool
forall a. Maybe a
Nothing
          , $sel:components:InteractionCallbackMessageOptions :: Maybe [Component]
components = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe [Component]) CreateMessageOptions (Maybe [Component])
-> Maybe [Component]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "components"
  (Getting
     (Maybe [Component]) CreateMessageOptions (Maybe [Component]))
Getting
  (Maybe [Component]) CreateMessageOptions (Maybe [Component])
#components
          , $sel:attachments:InteractionCallbackMessageOptions :: Maybe [CreateMessageAttachment]
attachments = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe [CreateMessageAttachment])
     CreateMessageOptions
     (Maybe [CreateMessageAttachment])
-> Maybe [CreateMessageAttachment]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "attachments"
  (Getting
     (Maybe [CreateMessageAttachment])
     CreateMessageOptions
     (Maybe [CreateMessageAttachment]))
Getting
  (Maybe [CreateMessageAttachment])
  CreateMessageOptions
  (Maybe [CreateMessageAttachment])
#attachments
          }
   in do
        Snowflake Interaction
interactionID <- Sem r (Snowflake Interaction)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake Interaction)
getInteractionID
        InteractionToken
interactionToken <- Sem r InteractionToken
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r InteractionToken
getInteractionToken
        InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall (r :: [(* -> *) -> * -> *]) a.
(Members '[RatelimitEff, TokenEff, LogEff, MetricEff, Embed IO] r,
 Request a, ReadResponse (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (InteractionRequest ()
 -> Sem r (Either RestError (Result (InteractionRequest ()))))
-> InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall a b. (a -> b) -> a -> b
$ Snowflake Interaction
-> InteractionToken
-> InteractionCallbackMessageOptions
-> InteractionRequest ()
forall i.
HasID Interaction i =>
i
-> InteractionToken
-> InteractionCallbackMessageOptions
-> InteractionRequest ()
CreateResponseUpdate Snowflake Interaction
interactionID InteractionToken
interactionToken InteractionCallbackMessageOptions
opts

-- | Create a follow up response to an interaction
followUp :: forall t r.
  (P.Members '[InteractionEff, RatelimitEff, TokenEff, LogEff, MetricEff, P.Embed IO] r, ToMessage t) =>
  t ->
  P.Sem r (Either RestError ())
followUp :: t -> Sem r (Either RestError ())
followUp (t -> CreateMessageOptions
forall a. ToMessage a => a -> CreateMessageOptions
runToMessage -> CreateMessageOptions
msg) =
  let opts :: InteractionCallbackMessageOptions
opts =
        InteractionCallbackMessageOptions :: Maybe Bool
-> Maybe Text
-> Maybe [Embed]
-> Maybe AllowedMentions
-> Maybe Bool
-> Maybe Bool
-> Maybe [Component]
-> Maybe [CreateMessageAttachment]
-> InteractionCallbackMessageOptions
InteractionCallbackMessageOptions
          { $sel:tts:InteractionCallbackMessageOptions :: Maybe Bool
tts = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe Bool) CreateMessageOptions (Maybe Bool)
-> Maybe Bool
forall s a. s -> Getting a s a -> a
^. IsLabel
  "tts" (Getting (Maybe Bool) CreateMessageOptions (Maybe Bool))
Getting (Maybe Bool) CreateMessageOptions (Maybe Bool)
#tts
          , $sel:content:InteractionCallbackMessageOptions :: Maybe Text
content = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe Text) CreateMessageOptions (Maybe Text)
-> Maybe Text
forall s a. s -> Getting a s a -> a
^. IsLabel
  "content" (Getting (Maybe Text) CreateMessageOptions (Maybe Text))
Getting (Maybe Text) CreateMessageOptions (Maybe Text)
#content
          , $sel:embeds:InteractionCallbackMessageOptions :: Maybe [Embed]
embeds = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed])
-> Maybe [Embed]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "embeds"
  (Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed]))
Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed])
#embeds
          , $sel:allowedMentions:InteractionCallbackMessageOptions :: Maybe AllowedMentions
allowedMentions = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe AllowedMentions)
     CreateMessageOptions
     (Maybe AllowedMentions)
-> Maybe AllowedMentions
forall s a. s -> Getting a s a -> a
^. IsLabel
  "allowedMentions"
  (Getting
     (Maybe AllowedMentions)
     CreateMessageOptions
     (Maybe AllowedMentions))
Getting
  (Maybe AllowedMentions)
  CreateMessageOptions
  (Maybe AllowedMentions)
#allowedMentions
          , $sel:ephemeral:InteractionCallbackMessageOptions :: Maybe Bool
ephemeral = Maybe Bool
forall a. Maybe a
Nothing
          , $sel:suppressEmbeds:InteractionCallbackMessageOptions :: Maybe Bool
suppressEmbeds = Maybe Bool
forall a. Maybe a
Nothing
          , $sel:components:InteractionCallbackMessageOptions :: Maybe [Component]
components = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe [Component]) CreateMessageOptions (Maybe [Component])
-> Maybe [Component]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "components"
  (Getting
     (Maybe [Component]) CreateMessageOptions (Maybe [Component]))
Getting
  (Maybe [Component]) CreateMessageOptions (Maybe [Component])
#components
          , $sel:attachments:InteractionCallbackMessageOptions :: Maybe [CreateMessageAttachment]
attachments = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe [CreateMessageAttachment])
     CreateMessageOptions
     (Maybe [CreateMessageAttachment])
-> Maybe [CreateMessageAttachment]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "attachments"
  (Getting
     (Maybe [CreateMessageAttachment])
     CreateMessageOptions
     (Maybe [CreateMessageAttachment]))
Getting
  (Maybe [CreateMessageAttachment])
  CreateMessageOptions
  (Maybe [CreateMessageAttachment])
#attachments
          }
   in do
        Snowflake Application
applicationID <- Sem r (Snowflake Application)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake Application)
getApplicationID
        InteractionToken
interactionToken <- Sem r InteractionToken
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r InteractionToken
getInteractionToken
        InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall (r :: [(* -> *) -> * -> *]) a.
(Members '[RatelimitEff, TokenEff, LogEff, MetricEff, Embed IO] r,
 Request a, ReadResponse (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (InteractionRequest ()
 -> Sem r (Either RestError (Result (InteractionRequest ()))))
-> InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall a b. (a -> b) -> a -> b
$ Snowflake Application
-> InteractionToken
-> InteractionCallbackMessageOptions
-> InteractionRequest ()
forall i.
HasID Application i =>
i
-> InteractionToken
-> InteractionCallbackMessageOptions
-> InteractionRequest ()
CreateFollowupMessage Snowflake Application
applicationID InteractionToken
interactionToken InteractionCallbackMessageOptions
opts

-- | Create an ephemeral follow up response to an interaction
followUpEphemeral :: forall t r.
  (P.Members '[InteractionEff, RatelimitEff, TokenEff, LogEff, MetricEff, P.Embed IO] r, ToMessage t) =>
  t ->
  P.Sem r (Either RestError ())
followUpEphemeral :: t -> Sem r (Either RestError ())
followUpEphemeral (t -> CreateMessageOptions
forall a. ToMessage a => a -> CreateMessageOptions
runToMessage -> CreateMessageOptions
msg) =
  let opts :: InteractionCallbackMessageOptions
opts =
        InteractionCallbackMessageOptions :: Maybe Bool
-> Maybe Text
-> Maybe [Embed]
-> Maybe AllowedMentions
-> Maybe Bool
-> Maybe Bool
-> Maybe [Component]
-> Maybe [CreateMessageAttachment]
-> InteractionCallbackMessageOptions
InteractionCallbackMessageOptions
          { $sel:tts:InteractionCallbackMessageOptions :: Maybe Bool
tts = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe Bool) CreateMessageOptions (Maybe Bool)
-> Maybe Bool
forall s a. s -> Getting a s a -> a
^. IsLabel
  "tts" (Getting (Maybe Bool) CreateMessageOptions (Maybe Bool))
Getting (Maybe Bool) CreateMessageOptions (Maybe Bool)
#tts
          , $sel:content:InteractionCallbackMessageOptions :: Maybe Text
content = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe Text) CreateMessageOptions (Maybe Text)
-> Maybe Text
forall s a. s -> Getting a s a -> a
^. IsLabel
  "content" (Getting (Maybe Text) CreateMessageOptions (Maybe Text))
Getting (Maybe Text) CreateMessageOptions (Maybe Text)
#content
          , $sel:embeds:InteractionCallbackMessageOptions :: Maybe [Embed]
embeds = CreateMessageOptions
msg CreateMessageOptions
-> Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed])
-> Maybe [Embed]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "embeds"
  (Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed]))
Getting (Maybe [Embed]) CreateMessageOptions (Maybe [Embed])
#embeds
          , $sel:allowedMentions:InteractionCallbackMessageOptions :: Maybe AllowedMentions
allowedMentions = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe AllowedMentions)
     CreateMessageOptions
     (Maybe AllowedMentions)
-> Maybe AllowedMentions
forall s a. s -> Getting a s a -> a
^. IsLabel
  "allowedMentions"
  (Getting
     (Maybe AllowedMentions)
     CreateMessageOptions
     (Maybe AllowedMentions))
Getting
  (Maybe AllowedMentions)
  CreateMessageOptions
  (Maybe AllowedMentions)
#allowedMentions
          , $sel:ephemeral:InteractionCallbackMessageOptions :: Maybe Bool
ephemeral = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
          , $sel:suppressEmbeds:InteractionCallbackMessageOptions :: Maybe Bool
suppressEmbeds = Maybe Bool
forall a. Maybe a
Nothing
          , $sel:components:InteractionCallbackMessageOptions :: Maybe [Component]
components = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe [Component]) CreateMessageOptions (Maybe [Component])
-> Maybe [Component]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "components"
  (Getting
     (Maybe [Component]) CreateMessageOptions (Maybe [Component]))
Getting
  (Maybe [Component]) CreateMessageOptions (Maybe [Component])
#components
          , $sel:attachments:InteractionCallbackMessageOptions :: Maybe [CreateMessageAttachment]
attachments = CreateMessageOptions
msg CreateMessageOptions
-> Getting
     (Maybe [CreateMessageAttachment])
     CreateMessageOptions
     (Maybe [CreateMessageAttachment])
-> Maybe [CreateMessageAttachment]
forall s a. s -> Getting a s a -> a
^. IsLabel
  "attachments"
  (Getting
     (Maybe [CreateMessageAttachment])
     CreateMessageOptions
     (Maybe [CreateMessageAttachment]))
Getting
  (Maybe [CreateMessageAttachment])
  CreateMessageOptions
  (Maybe [CreateMessageAttachment])
#attachments
          }
   in do
        Snowflake Application
applicationID <- Sem r (Snowflake Application)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake Application)
getApplicationID
        InteractionToken
interactionToken <- Sem r InteractionToken
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r InteractionToken
getInteractionToken
        InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall (r :: [(* -> *) -> * -> *]) a.
(Members '[RatelimitEff, TokenEff, LogEff, MetricEff, Embed IO] r,
 Request a, ReadResponse (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (InteractionRequest ()
 -> Sem r (Either RestError (Result (InteractionRequest ()))))
-> InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall a b. (a -> b) -> a -> b
$ Snowflake Application
-> InteractionToken
-> InteractionCallbackMessageOptions
-> InteractionRequest ()
forall i.
HasID Application i =>
i
-> InteractionToken
-> InteractionCallbackMessageOptions
-> InteractionRequest ()
CreateFollowupMessage Snowflake Application
applicationID InteractionToken
interactionToken InteractionCallbackMessageOptions
opts

-- | Defer an interaction and show a loading state, use @followUp@ later on
defer :: P.Members '[InteractionEff, RatelimitEff, TokenEff, LogEff, MetricEff, P.Embed IO] r => P.Sem r (Either RestError ())
defer :: Sem r (Either RestError ())
defer = do
  Snowflake Interaction
interactionID <- Sem r (Snowflake Interaction)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake Interaction)
getInteractionID
  InteractionToken
interactionToken <- Sem r InteractionToken
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r InteractionToken
getInteractionToken
  InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall (r :: [(* -> *) -> * -> *]) a.
(Members '[RatelimitEff, TokenEff, LogEff, MetricEff, Embed IO] r,
 Request a, ReadResponse (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (InteractionRequest ()
 -> Sem r (Either RestError (Result (InteractionRequest ()))))
-> InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall a b. (a -> b) -> a -> b
$ Snowflake Interaction
-> InteractionToken -> Bool -> InteractionRequest ()
forall i.
HasID Interaction i =>
i -> InteractionToken -> Bool -> InteractionRequest ()
CreateResponseDefer Snowflake Interaction
interactionID InteractionToken
interactionToken Bool
False

-- | Defer an interaction and show an ephemeral loading state, use @followUp@
-- later on
deferEphemeral :: P.Members '[InteractionEff, RatelimitEff, TokenEff, LogEff, MetricEff, P.Embed IO] r => P.Sem r (Either RestError ())
deferEphemeral :: Sem r (Either RestError ())
deferEphemeral = do
  Snowflake Interaction
interactionID <- Sem r (Snowflake Interaction)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake Interaction)
getInteractionID
  InteractionToken
interactionToken <- Sem r InteractionToken
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r InteractionToken
getInteractionToken
  InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall (r :: [(* -> *) -> * -> *]) a.
(Members '[RatelimitEff, TokenEff, LogEff, MetricEff, Embed IO] r,
 Request a, ReadResponse (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (InteractionRequest ()
 -> Sem r (Either RestError (Result (InteractionRequest ()))))
-> InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall a b. (a -> b) -> a -> b
$ Snowflake Interaction
-> InteractionToken -> Bool -> InteractionRequest ()
forall i.
HasID Interaction i =>
i -> InteractionToken -> Bool -> InteractionRequest ()
CreateResponseDefer Snowflake Interaction
interactionID InteractionToken
interactionToken Bool
True

-- | Defer operation
deferComponent :: P.Members '[InteractionEff, RatelimitEff, TokenEff, LogEff, MetricEff, P.Embed IO] r => P.Sem r (Either RestError ())
deferComponent :: Sem r (Either RestError ())
deferComponent = do
  Snowflake Interaction
interactionID <- Sem r (Snowflake Interaction)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake Interaction)
getInteractionID
  InteractionToken
interactionToken <- Sem r InteractionToken
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r InteractionToken
getInteractionToken
  InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall (r :: [(* -> *) -> * -> *]) a.
(Members '[RatelimitEff, TokenEff, LogEff, MetricEff, Embed IO] r,
 Request a, ReadResponse (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (InteractionRequest ()
 -> Sem r (Either RestError (Result (InteractionRequest ()))))
-> InteractionRequest ()
-> Sem r (Either RestError (Result (InteractionRequest ())))
forall a b. (a -> b) -> a -> b
$ Snowflake Interaction -> InteractionToken -> InteractionRequest ()
forall i.
HasID Interaction i =>
i -> InteractionToken -> InteractionRequest ()
CreateResponseDeferComponent Snowflake Interaction
interactionID InteractionToken
interactionToken

fixupActionRow :: Component -> Component
fixupActionRow :: Component -> Component
fixupActionRow r :: Component
r@(ActionRow' [Component]
_) = Component
r
fixupActionRow Component
x = [Component] -> Component
ActionRow' [Component
x]

{- | Push a modal as a response to an interaction

 You should probably use this with 'Calamity.Interaction.View.runView'
-}
pushModal :: P.Members '[InteractionEff, RatelimitEff, TokenEff, LogEff, MetricEff, P.Embed IO] r => Text -> [Component] -> P.Sem r (Either RestError ())
pushModal :: Text -> [Component] -> Sem r (Either RestError ())
pushModal Text
title [Component]
c = do
  -- we don't actually use the custom id of the modal. the custom ids of the
  -- sub-components are enough to disambiguate
  CustomID
cid <- IO CustomID -> Sem r CustomID
forall (m :: * -> *) (r :: [(* -> *) -> * -> *]) a.
Member (Embed m) r =>
m a -> Sem r a
P.embed (IO CustomID -> Sem r CustomID) -> IO CustomID -> Sem r CustomID
forall a b. (a -> b) -> a -> b
$ (StdGen -> (CustomID, StdGen)) -> IO CustomID
forall (m :: * -> *) a. MonadIO m => (StdGen -> (a, StdGen)) -> m a
getStdRandom StdGen -> (CustomID, StdGen)
forall g a. (RandomGen g, Uniform a) => g -> (a, g)
uniform
  Snowflake Interaction
interactionID <- Sem r (Snowflake Interaction)
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r (Snowflake Interaction)
getInteractionID
  InteractionToken
interactionToken <- Sem r InteractionToken
forall (r :: [(* -> *) -> * -> *]).
Member InteractionEff r =>
Sem r InteractionToken
getInteractionToken
  InteractionRequest () -> Sem r (Either RestError ())
forall (r :: [(* -> *) -> * -> *]) a.
(Members '[RatelimitEff, TokenEff, LogEff, MetricEff, Embed IO] r,
 Request a, ReadResponse (Result a)) =>
a -> Sem r (Either RestError (Result a))
invoke (InteractionRequest () -> Sem r (Either RestError ()))
-> (InteractionCallbackModal -> InteractionRequest ())
-> InteractionCallbackModal
-> Sem r (Either RestError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Snowflake Interaction
-> InteractionToken
-> InteractionCallbackModal
-> InteractionRequest ()
forall i.
HasID Interaction i =>
i
-> InteractionToken
-> InteractionCallbackModal
-> InteractionRequest ()
CreateResponseModal Snowflake Interaction
interactionID InteractionToken
interactionToken (InteractionCallbackModal -> Sem r (Either RestError ()))
-> InteractionCallbackModal -> Sem r (Either RestError ())
forall a b. (a -> b) -> a -> b
$ CustomID -> Text -> [Component] -> InteractionCallbackModal
InteractionCallbackModal CustomID
cid Text
title ((Component -> Component) -> [Component] -> [Component]
forall a b. (a -> b) -> [a] -> [b]
map Component -> Component
fixupActionRow [Component]
c)