discord-haskell 0.8.2 → 0.8.3
raw patch · 6 files changed
+58/−35 lines, 6 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Discord: loginRestGatewayWithLog :: Auth -> String -> IO (RestChan, Gateway, [ThreadIdType])
+ Discord.Types.Channel: [userIsWebhook] :: User -> Bool
- Discord.Types.Channel: Message :: MessageId -> ChannelId -> Either WebhookId User -> Text -> UTCTime -> Maybe UTCTime -> Bool -> Bool -> [User] -> [RoleId] -> [Attachment] -> [Embed] -> Maybe Snowflake -> Bool -> Maybe GuildId -> Message
+ Discord.Types.Channel: Message :: MessageId -> ChannelId -> User -> Text -> UTCTime -> Maybe UTCTime -> Bool -> Bool -> [User] -> [RoleId] -> [Attachment] -> [Embed] -> Maybe Snowflake -> Bool -> Maybe GuildId -> Message
- Discord.Types.Channel: User :: UserId -> String -> String -> Maybe String -> Bool -> Maybe Bool -> Maybe Bool -> Maybe String -> User
+ Discord.Types.Channel: User :: UserId -> String -> String -> Maybe String -> Bool -> Bool -> Maybe Bool -> Maybe Bool -> Maybe String -> User
- Discord.Types.Channel: [messageAuthor] :: Message -> Either WebhookId User
+ Discord.Types.Channel: [messageAuthor] :: Message -> User
Files
- README.md +20/−12
- changelog.md +5/−0
- discord-haskell.cabal +2/−2
- src/Discord.hs +19/−8
- src/Discord/Rest/Prelude.hs +1/−1
- src/Discord/Types/Channel.hs +11/−12
README.md view
@@ -1,7 +1,7 @@ # discord-haskell [](https://travis-ci.org/aquarial/discord-haskell) -Please refer to `Getting Started` and `Notes` when -relevant. A few minutes of reading can save you +Please refer to `Getting Started` and `Notes` when+relevant. A few minutes of reading can save you hours of debugging. Recent change: `master` branch has the potentially broken, most@@ -17,17 +17,16 @@ 2 Add a 'Bot User' using the settings pane on the left. Take note of `CLIENT ID` on this page. -3 Use the BOT PERMISSIONS tab to compute a Permissions Int-(this does not immediately affect anything, hold onto this number)+3 Use the BOT PERMISSIONS tab to compute a Permissions Int to use for step 3 -3 Invite the bot to a server filling in the `<>` information.+3 Invite the bot to a server filling in the `<information>` below. Client ID and Permissions come from previous steps. `https://discordapp.com/oauth2/authorize?client_id=<CLIENT_ID>&scope=bot&permissions=<PERMISSIONS>` -4 Connect to the gateway once in order to send CreateMessage events.+4 Tos end `CreateMessage` events with restCall, you must connect to the gateway at least once. Try running `examples/gateway.hs` with your token to satisfy this. [This is a Discord requirement.](https://discordapp.com/developers/docs/resources/channel#create-message) -5 Look at the examples.+5 Look at the examples to get an idea of how the library is used. [examples/gateway.hs](./examples/gateway.hs), [examples/rest.hs](./examples/rest.hs), [examples/cache.hs](./examples/cache.hs), and@@ -44,9 +43,9 @@ [Gateway Events](https://discordapp.com/developers/docs/topics/gateway#commands-and-events-gateway-events) provide the other source of info, using `nextEvent` and `sendCommand`. Use `:info` to explore `Event` and `GatewaySendable` ADTs. -7 Add this library to your dependencies. discord-haskell is on hackage-with strict version bounds to stackage lts-12.10. You can also use-the github repo.+7 Add this library to your dependencies. discord-haskell is on hackage,+open an issue if the dependencies are too strict and you can't+add it to your project.. You can also use the github repo. ``` # in stack.yaml (if using stack)@@ -65,7 +64,7 @@ ## Notes `loginRest` allows `restCall`. `loginRestGateway` allows `restCall`,-`nextEvent`, `sendCommand`, and `readCache`. **Use `loginRest` if you don't need the +`nextEvent`, `sendCommand`, and `readCache`. **Use `loginRest` if you don't need the gateway.** Use `Control.Exception.finally` with `stopDiscord` to safely@@ -75,11 +74,20 @@ The examples will work on the `stable` branch. The `master` branch has the most recent (potentially) breaking changes. -To get the format to use for Emoji, type `\:emoji:` into +To get the format to use for Emoji, type `\:emoji:` into a discord chat. You should copy-paste that into the request. This can be a bit finicky. The equivalent of `:thumbsup::skin-tone-3:` is `"👍\127997"` for example, and a custom emoji will look like `<name:id_number>` or `name:id_number`.++## Debugging++If something goes wrong with the library please open an issue. It is helpful,+but not always necessary, to attach a log of what's going on when the library+crashes.++Use `loginRestGatewayWithLog :: Auth -> String -> IO (stuff)` to write the events to+a file. Remember to remove sensitive information before posting. ## History
changelog.md view
@@ -4,6 +4,11 @@ ## master +## 0.8.3++Simplify Message Author from `Either WebhookId User` to `User`++Add `loginRestGatewayWithLog` ### 0.8.2
discord-haskell.cabal view
@@ -1,6 +1,7 @@+cabal-version: 2.0 name: discord-haskell -- library version is also noted at src/Discord/Rest/Prelude.hs-version: 0.8.2+version: 0.8.3 description: Functions and data types to write discord bots. Official discord docs <https://discordapp.com/developers/docs/reference>. .@@ -18,7 +19,6 @@ build-type: Simple extra-doc-files: README.md , changelog.md-cabal-version: 1.18 source-repository head type: git
src/Discord.hs view
@@ -21,6 +21,7 @@ , stopDiscord , loginRest , loginRestGateway+ , loginRestGatewayWithLog ) where import Prelude hiding (log)@@ -53,8 +54,7 @@ loginRest :: Auth -> IO (RestChan, NotLoggedIntoGateway, [ThreadIdType]) loginRest auth = do log <- newChan- -- writeFile "the-log-of-discord-haskell.txt" ""- logId <- forkIO (logger log False)+ logId <- forkIO (logger log False "") (restHandler, restId) <- createHandler auth log pure (restHandler, NotLoggedIntoGateway, [ ThreadLogger logId , ThreadRest restId@@ -64,8 +64,7 @@ loginRestGateway :: Auth -> IO (RestChan, Gateway, [ThreadIdType]) loginRestGateway auth = do log <- newChan- -- writeFile "the-log-of-discord-haskell.txt" ""- logId <- forkIO (logger log False)+ logId <- forkIO (logger log False "") (restHandler, restId) <- createHandler auth log (gate, gateId) <- startGatewayThread auth log pure (restHandler, gate, [ ThreadLogger logId@@ -73,6 +72,18 @@ , ThreadGateway gateId ]) +-- | Start HTTP rest handler and gateway background threads+loginRestGatewayWithLog :: Auth -> String -> IO (RestChan, Gateway, [ThreadIdType])+loginRestGatewayWithLog auth path = do+ log <- newChan+ logId <- forkIO (logger log True path)+ (restHandler, restId) <- createHandler auth log+ (gate, gateId) <- startGatewayThread auth log+ pure (restHandler, gate, [ ThreadLogger logId+ , ThreadRest restId+ , ThreadGateway gateId+ ])+ -- | Execute one http request and get a response restCall :: (FromJSON a, Request (r a)) => (RestChan, y, z) -> r a -> IO (Either RestCallException a)@@ -104,9 +115,9 @@ ThreadLogger a -> a -- | Add anything from the Chan to the log file, forever-logger :: Chan String -> Bool -> IO ()-logger log False = forever $ readChan log >>= \_ -> pure ()-logger log True = forever $ do+logger :: Chan String -> Bool -> String -> IO ()+logger log False _ = forever $ readChan log >>= \_ -> pure ()+logger log True f = forever $ do x <- readChan log let line = x <> "\n\n"- appendFile "the-log-of-discord-haskell.txt" line+ appendFile f line
src/Discord/Rest/Prelude.hs view
@@ -24,7 +24,7 @@ where -- | https://discordapp.com/developers/docs/reference#user-agent -- Second place where the library version is noted- agent = "DiscordBot (https://github.com/aquarial/discord-haskell, 0.8.2)"+ agent = "DiscordBot (https://github.com/aquarial/discord-haskell, 0.8.3)" -- Append to an URL infixl 5 //
src/Discord/Types/Channel.hs view
@@ -17,17 +17,14 @@ -- | Represents information about a user. data User = User- { userId :: UserId -- ^ The user's id.- , userName :: String -- ^ The user's username, not unique across- -- the platform.+ { userId :: UserId -- ^ The user's id.+ , userName :: String -- ^ The user's username (not unique) , userDiscrim :: String -- ^ The user's 4-digit discord-tag. , userAvatar :: Maybe String -- ^ The user's avatar hash.- , userIsBot :: Bool -- ^ Whether the user belongs to an OAuth2- -- application.- , userMfa :: Maybe Bool -- ^ Whether the user has two factor- -- authentication enabled on the account.- , userVerified :: Maybe Bool -- ^ Whether the email on this account has- -- been verified.+ , userIsBot :: Bool -- ^ User is an OAuth2 application.+ , userIsWebhook:: Bool -- ^ User is a webhook+ , userMfa :: Maybe Bool -- ^ User has two factor authentication enabled on the account.+ , userVerified :: Maybe Bool -- ^ Whether the email has been verified. , userEmail :: Maybe String -- ^ The user's email. } deriving (Show, Eq, Ord) @@ -38,6 +35,7 @@ <*> o .: "discriminator" <*> o .:? "avatar" <*> o .:? "bot" .!= False+ <*> pure False -- webhook <*> o .:? "mfa_enabled" <*> o .:? "verified" <*> o .:? "email"@@ -164,7 +162,7 @@ { messageId :: MessageId -- ^ The id of the message , messageChannel :: ChannelId -- ^ Id of the channel the message -- was sent in- , messageAuthor :: Either WebhookId User -- ^ The 'User' the message was sent+ , messageAuthor :: User -- ^ The 'User' the message was sent -- by , messageText :: T.Text -- ^ Contents of the message , messageTimestamp :: UTCTime -- ^ When the message was sent@@ -190,9 +188,10 @@ Message <$> o .: "id" <*> o .: "channel_id" <*> (do isW <- o .:? "webhook_id"+ a <- o .: "author" case isW :: Maybe WebhookId of- Nothing -> Right <$> o .: "author"- Just w -> pure (Left w))+ Nothing -> pure a+ Just _ -> pure $ a { userIsWebhook = True }) <*> o .:? "content" .!= "" <*> o .:? "timestamp" .!= epochTime <*> o .:? "edited_timestamp"