calamity: A library for writing discord bots

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Please see the README on GitHub at https://github.com/nitros12/calamity#readme


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.0, 0.1.2.0, 0.1.3.0, 0.1.3.1, 0.1.4.0, 0.1.4.1, 0.1.4.2, 0.1.4.3, 0.1.4.4, 0.1.5.0, 0.1.5.1, 0.1.6.0, 0.1.6.1, 0.1.6.2, 0.1.7.0, 0.1.8.0, 0.1.8.1, 0.1.9.0, 0.1.9.1, 0.1.9.2, 0.1.9.3, 0.1.9.4, 0.1.10, 0.1.10.1, 0.1.11.0, 0.1.11.1, 0.1.11.2, 0.1.12.0, 0.1.13.0, 0.1.14.0, 0.1.14.1, 0.1.14.2, 0.1.14.3, 0.1.14.4, 0.1.14.5, 0.1.14.6, 0.1.14.7, 0.1.14.8, 0.1.14.9, 0.1.15.0, 0.1.16.0, 0.1.17.0, 0.1.17.1, 0.1.18.0, 0.1.18.1, 0.1.19.0, 0.1.19.1, 0.1.19.2, 0.1.20.0, 0.1.20.1, 0.1.21.0, 0.1.22.0, 0.1.22.1, 0.1.23.0, 0.1.23.1, 0.1.24.0, 0.1.24.1, 0.1.24.2, 0.1.25.0, 0.1.25.1, 0.1.26.0, 0.1.26.1, 0.1.27.0, 0.1.28.0, 0.1.28.1, 0.1.28.2, 0.1.28.3, 0.1.28.4, 0.1.28.5, 0.1.29.0, 0.1.30.0, 0.1.30.1, 0.1.30.2, 0.1.30.3, 0.1.30.4, 0.1.31.0, 0.2.0.0, 0.2.0.2, 0.3.0.0, 0.4.0.0, 0.5.0.0, 0.6.0.0, 0.7.0.0, 0.7.0.1, 0.7.1.0, 0.8.0.0, 0.9.0.0, 0.10.0.0, 0.11.0.0
Change log ChangeLog.md
Dependencies aeson (>=1.4 && <2), async (>=2.2 && <3), base (>=4.12 && <5), bytestring (>=0.10 && <0.11), concurrent-extra (>=0.7 && <0.8), data-default-class (>=0.1 && <0.2), deepseq (>=1.4.4.0 && <2), deque (>=0.4 && <0.5), df1 (>=0.3 && <0.4), di-polysemy (>=0.1 && <0.2), exceptions (>=0.10 && <0.11), fmt (>=0.6 && <0.7), focus (>=1.0 && <2), generic-lens (>=2.0 && <3), generic-override (>=0.0.0.0 && <0.0.1), generic-override-aeson (>=0.0.0.0 && <0.0.1), hashable (>=1.2 && <2), http-date (>=0.0.8 && <0.1), http-types (>=0.12 && <0.13), lens (>=4.18 && <5), lens-aeson (>=1.1 && <2), mtl (>=2.2 && <3), polysemy (>=1.3 && <2), polysemy-plugin (>=0.2 && <0.3), reflection (>=2.1 && <3), scientific (>=0.3 && <0.4), stm (>=2.5 && <3), stm-chans (>=3.0 && <4), stm-containers (>=1.1 && <2), text (>=1.2 && <2), text-show (>=3.8 && <4), time (>=1.8 && <1.11), typerep-map (>=0.3 && <0.4), unordered-containers (>=0.2 && <0.3), vector (>=0.12 && <0.13), websockets (>=0.12 && <0.13), wreq-patchable (>=1.0 && <2), wuss (>=1.1 && <2) [details]
License MIT
Copyright 2020 Ben Simms
Author Ben Simms
Maintainer ben@bensimms.moe
Category Network, Web
Home page https://github.com/nitros12/calamity
Bug tracker https://github.com/nitros12/calamity/issues
Source repo head: git clone https://github.com/nitros12/calamity
Uploaded by nitros12 at 2020-04-22T22:16:50Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for calamity-0.1.0.0

[back to package description]

Calamity

A discord library for haskell

Example

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLabels #-}

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}

{-# LANGUAGE TypeOperators #-}

module Main where

import           Calamity
import           Calamity.Cache.InMemory

import           Control.Concurrent
import           Control.Concurrent.STM.TVar
import           Control.Lens
import           Control.Monad

import           Data.Text.Lazy              ( Text, fromStrict )
import           Data.Text.Strict.Lens

import qualified DiPolysemy                  as DiP

import qualified Polysemy                    as P
import qualified Polysemy.Async              as P
import qualified Polysemy.AtomicState        as P
import qualified Polysemy.Embed              as P
import qualified Polysemy.Fail               as P

import           Prelude                     hiding ( error )

import           TextShow

data Counter m a where
  GetCounter :: Counter m Int

P.makeSem ''Counter

runCounterAtomic :: P.Member (P.Embed IO) r => P.Sem (Counter ': r) () -> P.Sem r ()
runCounterAtomic m = do
  var <- P.embed $ newTVarIO (0 :: Int)
  P.runAtomicStateTVar var $ P.reinterpret (\case
                                              GetCounter -> P.atomicState (\v -> (v + 1, v))) m

handleErrorByLogging m = do
  r <- P.runFail m
  case r of
    Left e -> DiP.error (e ^. packed)
    _      -> pure ()

info = DiP.info @Text
debug = DiP.info @Text

main :: IO ()
main = do
  P.runFinal . P.embedToFinal . runCounterAtomic . runCacheInMemory $ runBotIO (BotToken "") $ do
    react @"messagecreate" $ \msg -> handleErrorByLogging $ do
      when (msg ^. #content == "!count") $ replicateM_ 3 $ do
        val <- getCounter
        info $ "the counter is: " <> fromStrict (showt val)
        void . invokeRequest $ CreateMessage (msg ^. #channelID) ("The value is: " <> showt val)
      when (msg ^. #content == "!say hi") $ replicateM_ 3 . P.async $ do
        info "saying heya"
        Right msg' <- invokeRequest $ CreateMessage (msg ^. #channelID) "heya"
        info "sleeping"
        P.embed $ threadDelay (5 * 1000 * 1000)
        info "slept"
        void . invokeRequest $ EditMessage (msg ^. #channelID) msg' (Just "lol") Nothing
        info "edited"
      when (msg ^. #content == "!explode") $ do
        Just x <- pure Nothing
        debug "unreachable!"
      when (msg ^. #content == "!bye") $ do
        void . invokeRequest $ CreateMessage (msg ^. #channelID) "bye!"
        stopBot