discord-haskell-1.6.1: Write bots for Discord in Haskell

Safe HaskellNone
LanguageHaskell2010

Discord

Synopsis

Documentation

restCall :: (FromJSON a, Request (r a)) => DiscordHandle -> r a -> IO (Either RestCallErrorCode a) Source #

Execute one http request and get a response

sendCommand :: DiscordHandle -> GatewaySendable -> IO () Source #

Send a GatewaySendable, but not Heartbeat, Identify, or Resume

readCache :: DiscordHandle -> IO Cache Source #

Access the current state of the gateway cache

stopDiscord :: DiscordHandle -> IO () Source #

Stop all the background threads

class FromJSON a #

A type that can be converted from JSON, with the possibility of failure.

In many cases, you can get the compiler to generate parsing code for you (see below). To begin, let's cover writing an instance by hand.

There are various reasons a conversion could fail. For example, an Object could be missing a required key, an Array could be of the wrong size, or a value could be of an incompatible type.

The basic ways to signal a failed conversion are as follows:

  • fail yields a custom error message: it is the recommended way of reporting a failure;
  • empty (or mzero) is uninformative: use it when the error is meant to be caught by some (<|>);
  • typeMismatch can be used to report a failure when the encountered value is not of the expected JSON type; unexpected is an appropriate alternative when more than one type may be expected, or to keep the expected type implicit.

prependFailure (or modifyFailure) add more information to a parser's error messages.

An example type and instance using typeMismatch and prependFailure:

-- Allow ourselves to write Text literals.
{-# LANGUAGE OverloadedStrings #-}

data Coord = Coord { x :: Double, y :: Double }

instance FromJSON Coord where
    parseJSON (Object v) = Coord
        <$> v .: "x"
        <*> v .: "y"

    -- We do not expect a non-Object value here.
    -- We could use empty to fail, but typeMismatch
    -- gives a much more informative error message.
    parseJSON invalid    =
        prependFailure "parsing Coord failed, "
            (typeMismatch "Object" invalid)

For this common case of only being concerned with a single type of JSON value, the functions withObject, withScientific, etc. are provided. Their use is to be preferred when possible, since they are more terse. Using withObject, we can rewrite the above instance (assuming the same language extension and data type) as:

instance FromJSON Coord where
    parseJSON = withObject "Coord" $ \v -> Coord
        <$> v .: "x"
        <*> v .: "y"

Instead of manually writing your FromJSON instance, there are two options to do it automatically:

  • Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time. The generated instance is optimized for your type so it will probably be more efficient than the following option.
  • The compiler can provide a default generic implementation for parseJSON.

To use the second, simply add a deriving Generic clause to your datatype and declare a FromJSON instance for your datatype without giving a definition for parseJSON.

For example, the previous example can be simplified to just:

{-# LANGUAGE DeriveGeneric #-}

import GHC.Generics

data Coord = Coord { x :: Double, y :: Double } deriving Generic

instance FromJSON Coord

The default implementation will be equivalent to parseJSON = genericParseJSON defaultOptions; if you need different options, you can customize the generic decoding by defining:

customOptions = defaultOptions
                { fieldLabelModifier = map toUpper
                }

instance FromJSON Coord where
    parseJSON = genericParseJSON customOptions
Instances
FromJSON Bool 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Char 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Double 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Float 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Int 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Int8 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Int16 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Int32 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Int64 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Integer

This instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype Scientific and provide your own instance using withScientific if you want to allow larger inputs.

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Natural 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Ordering 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Word 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Word8 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Word16 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Word32 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Word64 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON () 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser () #

parseJSONList :: Value -> Parser [()] #

FromJSON Version 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Scientific 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Text 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON UTCTime 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Value 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON DotNetTime 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Text 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Void 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON CTime 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON IntSet 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON DiffTime

This instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype Scientific and provide your own instance using withScientific if you want to allow larger inputs.

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON ZonedTime

Supported string formats:

YYYY-MM-DD HH:MM Z YYYY-MM-DD HH:MM:SS Z YYYY-MM-DD HH:MM:SS.SSS Z

The first space may instead be a T, and the second space is optional. The Z represents UTC. The Z may be replaced with a time zone offset of the form +0000 or -08:00, where the first two digits are hours, the : is optional and the second two digits (also optional) are minutes.

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON LocalTime 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON TimeOfDay 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON SystemTime 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON NominalDiffTime

This instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype Scientific and provide your own instance using withScientific if you want to allow larger inputs.

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON Day 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON CalendarDiffTime 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON CalendarDiffDays 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON DayOfWeek 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON UUID 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON EmbedField Source # 
Instance details

Defined in Discord.Internal.Types.Embed

FromJSON EmbedFooter Source # 
Instance details

Defined in Discord.Internal.Types.Embed

FromJSON EmbedAuthor Source # 
Instance details

Defined in Discord.Internal.Types.Embed

FromJSON EmbedProvider Source # 
Instance details

Defined in Discord.Internal.Types.Embed

FromJSON EmbedImage Source # 
Instance details

Defined in Discord.Internal.Types.Embed

FromJSON EmbedVideo Source # 
Instance details

Defined in Discord.Internal.Types.Embed

FromJSON EmbedThumbnail Source # 
Instance details

Defined in Discord.Internal.Types.Embed

FromJSON Embed Source # 
Instance details

Defined in Discord.Internal.Types.Embed

FromJSON Snowflake Source # 
Instance details

Defined in Discord.Internal.Types.Prelude

FromJSON ConnectionObject Source # 
Instance details

Defined in Discord.Internal.Types.User

FromJSON Webhook Source # 
Instance details

Defined in Discord.Internal.Types.User

FromJSON User Source # 
Instance details

Defined in Discord.Internal.Types.User

FromJSON Nonce Source # 
Instance details

Defined in Discord.Internal.Types.Channel

FromJSON Attachment Source # 
Instance details

Defined in Discord.Internal.Types.Channel

FromJSON Message Source # 
Instance details

Defined in Discord.Internal.Types.Channel

FromJSON Overwrite Source # 
Instance details

Defined in Discord.Internal.Types.Channel

FromJSON Channel Source # 
Instance details

Defined in Discord.Internal.Types.Channel

FromJSON GuildEmbed Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON IntegrationAccount Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON Integration Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON InviteMeta Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON InviteWithMeta Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON Invite Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON GuildBan Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON VoiceRegion Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON Role Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON Emoji Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON PartialGuild Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON GuildInfo Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON GuildUnavailable Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON Guild Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON GuildMember Source # 
Instance details

Defined in Discord.Internal.Types.Guild

FromJSON TypingInfo Source # 
Instance details

Defined in Discord.Internal.Types.Events

FromJSON PresenceInfo Source # 
Instance details

Defined in Discord.Internal.Types.Events

FromJSON ReactionRemoveInfo Source # 
Instance details

Defined in Discord.Internal.Types.Events

FromJSON ReactionInfo Source # 
Instance details

Defined in Discord.Internal.Types.Events

FromJSON GatewayReceivable Source # 
Instance details

Defined in Discord.Internal.Types.Gateway

FromJSON a => FromJSON [a] 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser [a] #

parseJSONList :: Value -> Parser [[a]] #

FromJSON a => FromJSON (Maybe a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

(FromJSON a, Integral a) => FromJSON (Ratio a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

(Storable a, FromJSON a) => FromJSON (Vector a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

HasResolution a => FromJSON (Fixed a)

This instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype Scientific and provide your own instance using withScientific if you want to allow larger inputs.

Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (Min a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Min a) #

parseJSONList :: Value -> Parser [Min a] #

FromJSON a => FromJSON (Max a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Max a) #

parseJSONList :: Value -> Parser [Max a] #

FromJSON a => FromJSON (First a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (Last a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (WrappedMonoid a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (Option a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (Identity a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (First a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (Last a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (Dual a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (NonEmpty a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (IntMap a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON v => FromJSON (Tree v) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (Seq a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Seq a) #

parseJSONList :: Value -> Parser [Seq a] #

(Ord a, FromJSON a) => FromJSON (Set a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Set a) #

parseJSONList :: Value -> Parser [Set a] #

FromJSON a => FromJSON (DList a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

(Prim a, FromJSON a) => FromJSON (PrimArray a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (SmallArray a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (Array a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

(Eq a, Hashable a, FromJSON a) => FromJSON (HashSet a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

(Vector Vector a, FromJSON a) => FromJSON (Vector a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

(Prim a, FromJSON a) => FromJSON (Vector a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

FromJSON a => FromJSON (Vector a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

(FromJSON a, FromJSON b) => FromJSON (Either a b) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Either a b) #

parseJSONList :: Value -> Parser [Either a b] #

(FromJSON a, FromJSON b) => FromJSON (a, b) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b) #

parseJSONList :: Value -> Parser [(a, b)] #

(FromJSON v, FromJSONKey k, Eq k, Hashable k) => FromJSON (HashMap k v) 
Instance details

Defined in Data.Aeson.Types.FromJSON

(FromJSONKey k, Ord k, FromJSON v) => FromJSON (Map k v) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Map k v) #

parseJSONList :: Value -> Parser [Map k v] #

FromJSON (Proxy a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

(FromJSON a, FromJSON b) => FromJSON (These a b)

Since: aeson-1.5.1.0

Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (These a b) #

parseJSONList :: Value -> Parser [These a b] #

(FromJSON a, FromJSON b, FromJSON c) => FromJSON (a, b, c) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c) #

parseJSONList :: Value -> Parser [(a, b, c)] #

FromJSON a => FromJSON (Const a b) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Const a b) #

parseJSONList :: Value -> Parser [Const a b] #

FromJSON b => FromJSON (Tagged a b) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Tagged a b) #

parseJSONList :: Value -> Parser [Tagged a b] #

(FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (These1 f g a)

Since: aeson-1.5.1.0

Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (These1 f g a) #

parseJSONList :: Value -> Parser [These1 f g a] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON (a, b, c, d) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d) #

parseJSONList :: Value -> Parser [(a, b, c, d)] #

(FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (Product f g a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Product f g a) #

parseJSONList :: Value -> Parser [Product f g a] #

(FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (Sum f g a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Sum f g a) #

parseJSONList :: Value -> Parser [Sum f g a] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) => FromJSON (a, b, c, d, e) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e) #

parseJSONList :: Value -> Parser [(a, b, c, d, e)] #

(FromJSON1 f, FromJSON1 g, FromJSON a) => FromJSON (Compose f g a) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (Compose f g a) #

parseJSONList :: Value -> Parser [Compose f g a] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f) => FromJSON (a, b, c, d, e, f) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f) #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f)] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g) => FromJSON (a, b, c, d, e, f, g) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g) #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g)] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h) => FromJSON (a, b, c, d, e, f, g, h) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h) #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h)] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i) => FromJSON (a, b, c, d, e, f, g, h, i) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i) #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i)] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j) => FromJSON (a, b, c, d, e, f, g, h, i, j) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j) #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j)] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k) => FromJSON (a, b, c, d, e, f, g, h, i, j, k) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j, k) #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j, k)] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j, k, l) #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j, k, l)] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j, k, l, m) #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j, k, l, m)] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m, FromJSON n) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m, n) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j, k, l, m, n) #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j, k, l, m, n)] #

(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j, FromJSON k, FromJSON l, FromJSON m, FromJSON n, FromJSON o) => FromJSON (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) 
Instance details

Defined in Data.Aeson.Types.FromJSON

Methods

parseJSON :: Value -> Parser (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) #

parseJSONList :: Value -> Parser [(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)] #

def :: Default a => a #

The default value for this type.