{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

-- | Provides actions for Channel API interactions
module Discord.Internal.Rest.Emoji
  ( EmojiRequest (..),
    ModifyGuildEmojiOpts (..),
    parseEmojiImage,
    parseStickerImage,
    StickerRequest (..),
    CreateGuildStickerOpts (..),
    EditGuildStickerOpts (..)
  )
where

import Data.Aeson
import qualified Data.ByteString as B
import qualified Data.ByteString.Base64 as B64
import qualified Data.Text as T
import Discord.Internal.Rest.Prelude
import Discord.Internal.Types
import Network.HTTP.Req ((/:), (/~))
import qualified Network.HTTP.Req as R

instance Request (EmojiRequest a) where
  majorRoute :: EmojiRequest a -> String
majorRoute = EmojiRequest a -> String
forall a. EmojiRequest a -> String
emojiMajorRoute
  jsonRequest :: EmojiRequest a -> JsonRequest
jsonRequest = EmojiRequest a -> JsonRequest
forall a. EmojiRequest a -> JsonRequest
emojiJsonRequest

-- | Data constructor for requests. See <https://discord.com/developers/docs/resources/ API>
data EmojiRequest a where
  -- | List of emoji objects for the given guild. Requires MANAGE_EMOJIS permission.
  ListGuildEmojis :: GuildId -> EmojiRequest [Emoji]
  -- | Emoji object for the given guild and emoji ID
  GetGuildEmoji :: GuildId -> EmojiId -> EmojiRequest Emoji
  -- | Create a new guild emoji (static&animated). Requires MANAGE_EMOJIS permission.
  CreateGuildEmoji :: GuildId -> T.Text -> Base64Image Emoji -> EmojiRequest Emoji
  -- | Requires MANAGE_EMOJIS permission
  ModifyGuildEmoji :: GuildId -> EmojiId -> ModifyGuildEmojiOpts -> EmojiRequest Emoji
  -- | Requires MANAGE_EMOJIS permission
  DeleteGuildEmoji :: GuildId -> EmojiId -> EmojiRequest ()

data ModifyGuildEmojiOpts = ModifyGuildEmojiOpts
  { ModifyGuildEmojiOpts -> Text
modifyGuildEmojiName :: T.Text,
    ModifyGuildEmojiOpts -> [RoleId]
modifyGuildEmojiRoles :: [RoleId]
  }
  deriving (Int -> ModifyGuildEmojiOpts -> ShowS
[ModifyGuildEmojiOpts] -> ShowS
ModifyGuildEmojiOpts -> String
(Int -> ModifyGuildEmojiOpts -> ShowS)
-> (ModifyGuildEmojiOpts -> String)
-> ([ModifyGuildEmojiOpts] -> ShowS)
-> Show ModifyGuildEmojiOpts
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ModifyGuildEmojiOpts -> ShowS
showsPrec :: Int -> ModifyGuildEmojiOpts -> ShowS
$cshow :: ModifyGuildEmojiOpts -> String
show :: ModifyGuildEmojiOpts -> String
$cshowList :: [ModifyGuildEmojiOpts] -> ShowS
showList :: [ModifyGuildEmojiOpts] -> ShowS
Show, ReadPrec [ModifyGuildEmojiOpts]
ReadPrec ModifyGuildEmojiOpts
Int -> ReadS ModifyGuildEmojiOpts
ReadS [ModifyGuildEmojiOpts]
(Int -> ReadS ModifyGuildEmojiOpts)
-> ReadS [ModifyGuildEmojiOpts]
-> ReadPrec ModifyGuildEmojiOpts
-> ReadPrec [ModifyGuildEmojiOpts]
-> Read ModifyGuildEmojiOpts
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS ModifyGuildEmojiOpts
readsPrec :: Int -> ReadS ModifyGuildEmojiOpts
$creadList :: ReadS [ModifyGuildEmojiOpts]
readList :: ReadS [ModifyGuildEmojiOpts]
$creadPrec :: ReadPrec ModifyGuildEmojiOpts
readPrec :: ReadPrec ModifyGuildEmojiOpts
$creadListPrec :: ReadPrec [ModifyGuildEmojiOpts]
readListPrec :: ReadPrec [ModifyGuildEmojiOpts]
Read, ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
(ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool)
-> (ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool)
-> Eq ModifyGuildEmojiOpts
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
== :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
$c/= :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
/= :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
Eq, Eq ModifyGuildEmojiOpts
Eq ModifyGuildEmojiOpts =>
(ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Ordering)
-> (ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool)
-> (ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool)
-> (ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool)
-> (ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool)
-> (ModifyGuildEmojiOpts
    -> ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts)
-> (ModifyGuildEmojiOpts
    -> ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts)
-> Ord ModifyGuildEmojiOpts
ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Ordering
ModifyGuildEmojiOpts
-> ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Ordering
compare :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Ordering
$c< :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
< :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
$c<= :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
<= :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
$c> :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
> :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
$c>= :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
>= :: ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts -> Bool
$cmax :: ModifyGuildEmojiOpts
-> ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts
max :: ModifyGuildEmojiOpts
-> ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts
$cmin :: ModifyGuildEmojiOpts
-> ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts
min :: ModifyGuildEmojiOpts
-> ModifyGuildEmojiOpts -> ModifyGuildEmojiOpts
Ord)

instance ToJSON ModifyGuildEmojiOpts where
  toJSON :: ModifyGuildEmojiOpts -> Value
toJSON (ModifyGuildEmojiOpts Text
name [RoleId]
roles) =
    [Pair] -> Value
object [Key
"name" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
name, Key
"roles" Key -> [RoleId] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= [RoleId]
roles]


-- | @parseEmojiImage bs@ will attempt to convert the given image bytestring @bs@
-- to the base64 format expected by the Discord API. It may return Left with an
-- error reason if either the bytestring is too large, or if the image format
-- could not be predetermined from the opening few bytes. This function does
-- /not/ validate the rest of the image, nor check that its dimensions are
-- 128x128 as required by Discord. This is up to the library user to check.
--
-- This function accepts all file types accepted by 'getMimeType'.
parseEmojiImage :: B.ByteString -> Either T.Text (Base64Image Emoji)
parseEmojiImage :: ByteString -> Either Text (Base64Image Emoji)
parseEmojiImage ByteString
bs
  | ByteString -> Int
B.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
256000        = Text -> Either Text (Base64Image Emoji)
forall a b. a -> Either a b
Left Text
"Cannot create emoji - File is larger than 256kb"
  | Just Text
mime <- ByteString -> Maybe Text
getMimeType ByteString
bs = Base64Image Emoji -> Either Text (Base64Image Emoji)
forall a b. b -> Either a b
Right (Text -> ByteString -> Base64Image Emoji
forall a. Text -> ByteString -> Base64Image a
Base64Image Text
mime (ByteString -> ByteString
B64.encode ByteString
bs))
  | Bool
otherwise                   = Text -> Either Text (Base64Image Emoji)
forall a b. a -> Either a b
Left Text
"Unsupported image format provided"

emojiMajorRoute :: EmojiRequest a -> String
emojiMajorRoute :: forall a. EmojiRequest a -> String
emojiMajorRoute EmojiRequest a
c = case EmojiRequest a
c of
  (ListGuildEmojis GuildId
g) -> String
"emoji " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> GuildId -> String
forall a. Show a => a -> String
show GuildId
g
  (GetGuildEmoji GuildId
g EmojiId
_) -> String
"emoji " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> GuildId -> String
forall a. Show a => a -> String
show GuildId
g
  (CreateGuildEmoji GuildId
g Text
_ Base64Image Emoji
_) -> String
"emoji " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> GuildId -> String
forall a. Show a => a -> String
show GuildId
g
  (ModifyGuildEmoji GuildId
g EmojiId
_ ModifyGuildEmojiOpts
_) -> String
"emoji " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> GuildId -> String
forall a. Show a => a -> String
show GuildId
g
  (DeleteGuildEmoji GuildId
g EmojiId
_) -> String
"emoji " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> GuildId -> String
forall a. Show a => a -> String
show GuildId
g

guilds :: R.Url 'R.Https
guilds :: Url 'Https
guilds = Url 'Https
baseUrl Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"guilds"

emojiJsonRequest :: EmojiRequest r -> JsonRequest
emojiJsonRequest :: forall a. EmojiRequest a -> JsonRequest
emojiJsonRequest EmojiRequest r
c = case EmojiRequest r
c of
  (ListGuildEmojis GuildId
g) -> Url 'Https -> Option 'Https -> JsonRequest
Get (Url 'Https
guilds Url 'Https -> GuildId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ GuildId
g Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"emojis") Option 'Https
forall a. Monoid a => a
mempty
  (GetGuildEmoji GuildId
g EmojiId
e) -> Url 'Https -> Option 'Https -> JsonRequest
Get (Url 'Https
guilds Url 'Https -> GuildId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ GuildId
g Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"emojis" Url 'Https -> EmojiId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ EmojiId
e) Option 'Https
forall a. Monoid a => a
mempty
  (CreateGuildEmoji GuildId
g Text
name Base64Image Emoji
b64im) ->
    Url 'Https
-> RestIO (ReqBodyJson Value) -> Option 'Https -> JsonRequest
forall a.
HttpBody a =>
Url 'Https -> RestIO a -> Option 'Https -> JsonRequest
Post
      (Url 'Https
guilds Url 'Https -> GuildId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ GuildId
g Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"emojis")
      ( ReqBodyJson Value -> RestIO (ReqBodyJson Value)
forall a. a -> RestIO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
          ( Value -> ReqBodyJson Value
forall a. a -> ReqBodyJson a
R.ReqBodyJson
              ( [Pair] -> Value
object
                  [ Key
"name" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
name,
                    Key
"image" Key -> Base64Image Emoji -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Base64Image Emoji
b64im
                    -- todo , "roles" .= ...
                  ]
              )
          )
      )
      Option 'Https
forall a. Monoid a => a
mempty
  (ModifyGuildEmoji GuildId
g EmojiId
e ModifyGuildEmojiOpts
o) ->
    Url 'Https
-> RestIO (ReqBodyJson ModifyGuildEmojiOpts)
-> Option 'Https
-> JsonRequest
forall a.
HttpBody a =>
Url 'Https -> RestIO a -> Option 'Https -> JsonRequest
Patch
      (Url 'Https
guilds Url 'Https -> GuildId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ GuildId
g Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"emojis" Url 'Https -> EmojiId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ EmojiId
e)
      (ReqBodyJson ModifyGuildEmojiOpts
-> RestIO (ReqBodyJson ModifyGuildEmojiOpts)
forall a. a -> RestIO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ModifyGuildEmojiOpts -> ReqBodyJson ModifyGuildEmojiOpts
forall a. a -> ReqBodyJson a
R.ReqBodyJson ModifyGuildEmojiOpts
o))
      Option 'Https
forall a. Monoid a => a
mempty
  (DeleteGuildEmoji GuildId
g EmojiId
e) -> Url 'Https -> Option 'Https -> JsonRequest
Delete (Url 'Https
guilds Url 'Https -> GuildId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ GuildId
g Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"emojis" Url 'Https -> EmojiId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ EmojiId
e) Option 'Https
forall a. Monoid a => a
mempty

-- | @parseStickerImage bs@ accepts PNG, APNG, or Lottie JSON bytestring @bs@ and
-- will attempt to convert it to the base64 format expected by the Discord API.
-- It may return Left with an error reason if the image format is unexpected.
-- This function does /not/ validate the contents of the image, this is up to
-- the library user to check.
parseStickerImage :: B.ByteString -> Either T.Text (Base64Image Sticker)
parseStickerImage :: ByteString -> Either Text (Base64Image Sticker)
parseStickerImage ByteString
bs
  | ByteString -> Int
B.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
512000
      = Text -> Either Text (Base64Image Sticker)
forall a b. a -> Either a b
Left Text
"Cannot create sticker - File is larger than 512kb"
  | Just Text
"image/png" <- ByteString -> Maybe Text
getMimeType ByteString
bs
      = Base64Image Sticker -> Either Text (Base64Image Sticker)
forall a b. b -> Either a b
Right (Text -> ByteString -> Base64Image Sticker
forall a. Text -> ByteString -> Base64Image a
Base64Image Text
"image/png" (ByteString -> ByteString
B64.encode ByteString
bs))
  | Bool -> Bool
not (ByteString -> Bool
B.null ByteString
bs) Bool -> Bool -> Bool
&& HasCallStack => ByteString -> Word8
ByteString -> Word8
B.head ByteString
bs Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x7b -- '{'
      = Base64Image Sticker -> Either Text (Base64Image Sticker)
forall a b. b -> Either a b
Right (Text -> ByteString -> Base64Image Sticker
forall a. Text -> ByteString -> Base64Image a
Base64Image Text
"application/json" (ByteString -> ByteString
B64.encode ByteString
bs))
  | Bool
otherwise
      = Text -> Either Text (Base64Image Sticker)
forall a b. a -> Either a b
Left Text
"Unsupported image format provided"

-- | Options for `CreateGuildSticker`
data CreateGuildStickerOpts = CreateGuildStickerOpts
  { CreateGuildStickerOpts -> Text
guildStickerName :: T.Text,
    CreateGuildStickerOpts -> Text
guildStickerDescription :: T.Text,
    CreateGuildStickerOpts -> [Text]
guildStickerTags :: [T.Text],
    CreateGuildStickerOpts -> Base64Image Sticker
guildStickerFile :: Base64Image Sticker
  }
  deriving (Int -> CreateGuildStickerOpts -> ShowS
[CreateGuildStickerOpts] -> ShowS
CreateGuildStickerOpts -> String
(Int -> CreateGuildStickerOpts -> ShowS)
-> (CreateGuildStickerOpts -> String)
-> ([CreateGuildStickerOpts] -> ShowS)
-> Show CreateGuildStickerOpts
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CreateGuildStickerOpts -> ShowS
showsPrec :: Int -> CreateGuildStickerOpts -> ShowS
$cshow :: CreateGuildStickerOpts -> String
show :: CreateGuildStickerOpts -> String
$cshowList :: [CreateGuildStickerOpts] -> ShowS
showList :: [CreateGuildStickerOpts] -> ShowS
Show, ReadPrec [CreateGuildStickerOpts]
ReadPrec CreateGuildStickerOpts
Int -> ReadS CreateGuildStickerOpts
ReadS [CreateGuildStickerOpts]
(Int -> ReadS CreateGuildStickerOpts)
-> ReadS [CreateGuildStickerOpts]
-> ReadPrec CreateGuildStickerOpts
-> ReadPrec [CreateGuildStickerOpts]
-> Read CreateGuildStickerOpts
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS CreateGuildStickerOpts
readsPrec :: Int -> ReadS CreateGuildStickerOpts
$creadList :: ReadS [CreateGuildStickerOpts]
readList :: ReadS [CreateGuildStickerOpts]
$creadPrec :: ReadPrec CreateGuildStickerOpts
readPrec :: ReadPrec CreateGuildStickerOpts
$creadListPrec :: ReadPrec [CreateGuildStickerOpts]
readListPrec :: ReadPrec [CreateGuildStickerOpts]
Read, CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
(CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool)
-> (CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool)
-> Eq CreateGuildStickerOpts
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
== :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
$c/= :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
/= :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
Eq, Eq CreateGuildStickerOpts
Eq CreateGuildStickerOpts =>
(CreateGuildStickerOpts -> CreateGuildStickerOpts -> Ordering)
-> (CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool)
-> (CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool)
-> (CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool)
-> (CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool)
-> (CreateGuildStickerOpts
    -> CreateGuildStickerOpts -> CreateGuildStickerOpts)
-> (CreateGuildStickerOpts
    -> CreateGuildStickerOpts -> CreateGuildStickerOpts)
-> Ord CreateGuildStickerOpts
CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
CreateGuildStickerOpts -> CreateGuildStickerOpts -> Ordering
CreateGuildStickerOpts
-> CreateGuildStickerOpts -> CreateGuildStickerOpts
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Ordering
compare :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Ordering
$c< :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
< :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
$c<= :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
<= :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
$c> :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
> :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
$c>= :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
>= :: CreateGuildStickerOpts -> CreateGuildStickerOpts -> Bool
$cmax :: CreateGuildStickerOpts
-> CreateGuildStickerOpts -> CreateGuildStickerOpts
max :: CreateGuildStickerOpts
-> CreateGuildStickerOpts -> CreateGuildStickerOpts
$cmin :: CreateGuildStickerOpts
-> CreateGuildStickerOpts -> CreateGuildStickerOpts
min :: CreateGuildStickerOpts
-> CreateGuildStickerOpts -> CreateGuildStickerOpts
Ord)

instance ToJSON CreateGuildStickerOpts where
  toJSON :: CreateGuildStickerOpts -> Value
toJSON (CreateGuildStickerOpts Text
name Text
desc [Text]
tags Base64Image Sticker
b64im) =
    [Pair] -> Value
object
      [ (Key
"name", Text -> Value
forall a. ToJSON a => a -> Value
toJSON Text
name),
        (Key
"description", Text -> Value
forall a. ToJSON a => a -> Value
toJSON Text
desc),
        (Key
"tags", Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Text -> Value) -> Text -> Value
forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Text
T.intercalate Text
"," [Text]
tags),
        (Key
"file", Base64Image Sticker -> Value
forall a. ToJSON a => a -> Value
toJSON Base64Image Sticker
b64im)
      ]

-- | Options for `ModifyGuildSticker`
data EditGuildStickerOpts = EditGuildStickerOpts
  { EditGuildStickerOpts -> Maybe Text
editGuildStickerName :: Maybe T.Text,
    EditGuildStickerOpts -> Maybe Text
editGuildStickerDescription :: Maybe T.Text,
    EditGuildStickerOpts -> Maybe [Text]
editGuildStickerTags :: Maybe [T.Text]
  }
  deriving (Int -> EditGuildStickerOpts -> ShowS
[EditGuildStickerOpts] -> ShowS
EditGuildStickerOpts -> String
(Int -> EditGuildStickerOpts -> ShowS)
-> (EditGuildStickerOpts -> String)
-> ([EditGuildStickerOpts] -> ShowS)
-> Show EditGuildStickerOpts
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EditGuildStickerOpts -> ShowS
showsPrec :: Int -> EditGuildStickerOpts -> ShowS
$cshow :: EditGuildStickerOpts -> String
show :: EditGuildStickerOpts -> String
$cshowList :: [EditGuildStickerOpts] -> ShowS
showList :: [EditGuildStickerOpts] -> ShowS
Show, ReadPrec [EditGuildStickerOpts]
ReadPrec EditGuildStickerOpts
Int -> ReadS EditGuildStickerOpts
ReadS [EditGuildStickerOpts]
(Int -> ReadS EditGuildStickerOpts)
-> ReadS [EditGuildStickerOpts]
-> ReadPrec EditGuildStickerOpts
-> ReadPrec [EditGuildStickerOpts]
-> Read EditGuildStickerOpts
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS EditGuildStickerOpts
readsPrec :: Int -> ReadS EditGuildStickerOpts
$creadList :: ReadS [EditGuildStickerOpts]
readList :: ReadS [EditGuildStickerOpts]
$creadPrec :: ReadPrec EditGuildStickerOpts
readPrec :: ReadPrec EditGuildStickerOpts
$creadListPrec :: ReadPrec [EditGuildStickerOpts]
readListPrec :: ReadPrec [EditGuildStickerOpts]
Read, EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
(EditGuildStickerOpts -> EditGuildStickerOpts -> Bool)
-> (EditGuildStickerOpts -> EditGuildStickerOpts -> Bool)
-> Eq EditGuildStickerOpts
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
== :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
$c/= :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
/= :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
Eq, Eq EditGuildStickerOpts
Eq EditGuildStickerOpts =>
(EditGuildStickerOpts -> EditGuildStickerOpts -> Ordering)
-> (EditGuildStickerOpts -> EditGuildStickerOpts -> Bool)
-> (EditGuildStickerOpts -> EditGuildStickerOpts -> Bool)
-> (EditGuildStickerOpts -> EditGuildStickerOpts -> Bool)
-> (EditGuildStickerOpts -> EditGuildStickerOpts -> Bool)
-> (EditGuildStickerOpts
    -> EditGuildStickerOpts -> EditGuildStickerOpts)
-> (EditGuildStickerOpts
    -> EditGuildStickerOpts -> EditGuildStickerOpts)
-> Ord EditGuildStickerOpts
EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
EditGuildStickerOpts -> EditGuildStickerOpts -> Ordering
EditGuildStickerOpts
-> EditGuildStickerOpts -> EditGuildStickerOpts
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: EditGuildStickerOpts -> EditGuildStickerOpts -> Ordering
compare :: EditGuildStickerOpts -> EditGuildStickerOpts -> Ordering
$c< :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
< :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
$c<= :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
<= :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
$c> :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
> :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
$c>= :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
>= :: EditGuildStickerOpts -> EditGuildStickerOpts -> Bool
$cmax :: EditGuildStickerOpts
-> EditGuildStickerOpts -> EditGuildStickerOpts
max :: EditGuildStickerOpts
-> EditGuildStickerOpts -> EditGuildStickerOpts
$cmin :: EditGuildStickerOpts
-> EditGuildStickerOpts -> EditGuildStickerOpts
min :: EditGuildStickerOpts
-> EditGuildStickerOpts -> EditGuildStickerOpts
Ord)

instance ToJSON EditGuildStickerOpts where
  toJSON :: EditGuildStickerOpts -> Value
toJSON EditGuildStickerOpts {Maybe [Text]
Maybe Text
editGuildStickerName :: EditGuildStickerOpts -> Maybe Text
editGuildStickerDescription :: EditGuildStickerOpts -> Maybe Text
editGuildStickerTags :: EditGuildStickerOpts -> Maybe [Text]
editGuildStickerName :: Maybe Text
editGuildStickerDescription :: Maybe Text
editGuildStickerTags :: Maybe [Text]
..} =
    [Maybe Pair] -> Value
objectFromMaybes
      [ Key
"name" Key -> Maybe Text -> Maybe Pair
forall a. ToJSON a => Key -> Maybe a -> Maybe Pair
.=? Maybe Text
editGuildStickerName,
        Key
"description" Key -> Maybe Text -> Maybe Pair
forall a. ToJSON a => Key -> Maybe a -> Maybe Pair
.=? Maybe Text
editGuildStickerDescription,
        Key
"tags" Key -> Maybe Text -> Maybe Pair
forall a. ToJSON a => Key -> Maybe a -> Maybe Pair
.=? ([Text] -> Text) -> Maybe [Text] -> Maybe Text
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> [Text] -> Text
T.intercalate Text
",") Maybe [Text]
editGuildStickerTags
      ]

instance Request (StickerRequest a) where
  majorRoute :: StickerRequest a -> String
majorRoute = StickerRequest a -> String
forall a. StickerRequest a -> String
stickerMajorRoute
  jsonRequest :: StickerRequest a -> JsonRequest
jsonRequest = StickerRequest a -> JsonRequest
forall a. StickerRequest a -> JsonRequest
stickerJsonRequest

-- | Data constructor for requests. See <https://discord.com/developers/docs/resources/ API>
--
-- Be warned that these are untested due to not having a spare server with 
-- boosts. Functionality is at your own risk.
data StickerRequest a where
  -- | Returns a sticker object for the given sticker ID.
  GetSticker :: StickerId -> StickerRequest Sticker
  -- | Returns the list of sticker packs available to Nitro subscribers.
  ListNitroStickerPacks :: StickerRequest [StickerPack]
  -- | Returns an array of sticker objects for the given guild.
  ListGuildStickers :: GuildId -> StickerRequest [Sticker]
  -- | Returns a sticker object for the given guild and sticker ID.
  GetGuildSticker :: GuildId -> StickerId -> StickerRequest Sticker
  -- | Create a new sticker for the guild.
  CreateGuildSticker :: GuildId -> CreateGuildStickerOpts -> StickerRequest Sticker
  -- | Modify a sticker for a guild.
  ModifyGuildSticker :: GuildId -> StickerId -> EditGuildStickerOpts -> StickerRequest Sticker
  -- | Delete a guild sticker
  DeleteGuildSticker :: GuildId -> StickerId -> StickerRequest ()

stickerMajorRoute :: StickerRequest a -> String
stickerMajorRoute :: forall a. StickerRequest a -> String
stickerMajorRoute = \case
  GetSticker StickerId
gid -> String
"sticker " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> StickerId -> String
forall a. Show a => a -> String
show StickerId
gid
  StickerRequest a
ListNitroStickerPacks -> String
"sticker"
  ListGuildStickers GuildId
gid -> String
"sticker " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> GuildId -> String
forall a. Show a => a -> String
show GuildId
gid
  GetGuildSticker GuildId
gid StickerId
_ -> String
"sticker " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> GuildId -> String
forall a. Show a => a -> String
show GuildId
gid
  CreateGuildSticker GuildId
gid CreateGuildStickerOpts
_ -> String
"sticker " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> GuildId -> String
forall a. Show a => a -> String
show GuildId
gid
  ModifyGuildSticker GuildId
gid StickerId
_ EditGuildStickerOpts
_ -> String
"sticker " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> GuildId -> String
forall a. Show a => a -> String
show GuildId
gid
  DeleteGuildSticker GuildId
gid StickerId
_ -> String
"sticker " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> GuildId -> String
forall a. Show a => a -> String
show GuildId
gid

stickerJsonRequest :: StickerRequest a -> JsonRequest
stickerJsonRequest :: forall a. StickerRequest a -> JsonRequest
stickerJsonRequest = \case
  GetSticker StickerId
gid -> Url 'Https -> Option 'Https -> JsonRequest
Get (Url 'Https
baseUrl Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"stickers" Url 'Https -> StickerId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ StickerId
gid) Option 'Https
forall a. Monoid a => a
mempty
  StickerRequest a
ListNitroStickerPacks -> Url 'Https -> Option 'Https -> JsonRequest
Get (Url 'Https
baseUrl Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"sticker-packs") Option 'Https
forall a. Monoid a => a
mempty
  ListGuildStickers GuildId
gid -> Url 'Https -> Option 'Https -> JsonRequest
Get (GuildId -> Url 'Https
forall {a}. ToHttpApiData a => a -> Url 'Https
stickersGuild GuildId
gid) Option 'Https
forall a. Monoid a => a
mempty
  GetGuildSticker GuildId
gid StickerId
sid -> Url 'Https -> Option 'Https -> JsonRequest
Get (GuildId -> Url 'Https
forall {a}. ToHttpApiData a => a -> Url 'Https
stickersGuild GuildId
gid Url 'Https -> StickerId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ StickerId
sid) Option 'Https
forall a. Monoid a => a
mempty
  CreateGuildSticker GuildId
gid CreateGuildStickerOpts
cgso -> Url 'Https
-> RestIO (ReqBodyJson Value) -> Option 'Https -> JsonRequest
forall a.
HttpBody a =>
Url 'Https -> RestIO a -> Option 'Https -> JsonRequest
Post (GuildId -> Url 'Https
forall {a}. ToHttpApiData a => a -> Url 'Https
stickersGuild GuildId
gid) (ReqBodyJson Value -> RestIO (ReqBodyJson Value)
forall a. a -> RestIO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ReqBodyJson Value -> RestIO (ReqBodyJson Value))
-> ReqBodyJson Value -> RestIO (ReqBodyJson Value)
forall a b. (a -> b) -> a -> b
$ Value -> ReqBodyJson Value
forall a. a -> ReqBodyJson a
R.ReqBodyJson (Value -> ReqBodyJson Value) -> Value -> ReqBodyJson Value
forall a b. (a -> b) -> a -> b
$ CreateGuildStickerOpts -> Value
forall a. ToJSON a => a -> Value
toJSON CreateGuildStickerOpts
cgso) Option 'Https
forall a. Monoid a => a
mempty
  ModifyGuildSticker GuildId
gid StickerId
sid EditGuildStickerOpts
egso -> Url 'Https
-> RestIO (ReqBodyJson EditGuildStickerOpts)
-> Option 'Https
-> JsonRequest
forall a.
HttpBody a =>
Url 'Https -> RestIO a -> Option 'Https -> JsonRequest
Patch (GuildId -> Url 'Https
forall {a}. ToHttpApiData a => a -> Url 'Https
stickersGuild GuildId
gid Url 'Https -> StickerId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ StickerId
sid) (ReqBodyJson EditGuildStickerOpts
-> RestIO (ReqBodyJson EditGuildStickerOpts)
forall a. a -> RestIO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ReqBodyJson EditGuildStickerOpts
 -> RestIO (ReqBodyJson EditGuildStickerOpts))
-> ReqBodyJson EditGuildStickerOpts
-> RestIO (ReqBodyJson EditGuildStickerOpts)
forall a b. (a -> b) -> a -> b
$ EditGuildStickerOpts -> ReqBodyJson EditGuildStickerOpts
forall a. a -> ReqBodyJson a
R.ReqBodyJson EditGuildStickerOpts
egso) Option 'Https
forall a. Monoid a => a
mempty
  DeleteGuildSticker GuildId
gid StickerId
sid -> Url 'Https -> Option 'Https -> JsonRequest
Delete (GuildId -> Url 'Https
forall {a}. ToHttpApiData a => a -> Url 'Https
stickersGuild GuildId
gid Url 'Https -> StickerId -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ StickerId
sid) Option 'Https
forall a. Monoid a => a
mempty
  where
    stickersGuild :: a -> Url 'Https
stickersGuild a
gid = Url 'Https
baseUrl Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"guilds" Url 'Https -> a -> Url 'Https
forall a (scheme :: Scheme).
ToHttpApiData a =>
Url scheme -> a -> Url scheme
/~ a
gid Url 'Https -> Text -> Url 'Https
forall (scheme :: Scheme). Url scheme -> Text -> Url scheme
/: Text
"stickers"