From 6c28d16a6e952a726e37216d2a11b91e85d7245f Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 9 Apr 2020 14:27:14 +0000 Subject: [PATCH] [testbot] Create testbot for future testing endavours --- haboli-testbot/README.md | 1 + haboli-testbot/app/Main.hs | 9 ++++ haboli-testbot/haboli-testbot.cabal | 58 +++++++++++++++++++++ haboli-testbot/package.yaml | 34 +++++++++++++ haboli-testbot/src/Haboli/Bots/TestBot.hs | 62 +++++++++++++++++++++++ stack.yaml | 1 + 6 files changed, 165 insertions(+) create mode 100644 haboli-testbot/README.md create mode 100644 haboli-testbot/app/Main.hs create mode 100644 haboli-testbot/haboli-testbot.cabal create mode 100644 haboli-testbot/package.yaml create mode 100644 haboli-testbot/src/Haboli/Bots/TestBot.hs diff --git a/haboli-testbot/README.md b/haboli-testbot/README.md new file mode 100644 index 0000000..e739648 --- /dev/null +++ b/haboli-testbot/README.md @@ -0,0 +1 @@ +# haboli-testbot diff --git a/haboli-testbot/app/Main.hs b/haboli-testbot/app/Main.hs new file mode 100644 index 0000000..2e3a175 --- /dev/null +++ b/haboli-testbot/app/Main.hs @@ -0,0 +1,9 @@ +module Main where + +import Control.Monad + +import Haboli.Bots.TestBot +import Haboli.Euphoria + +main :: IO () +main = void $ runClient defaultConfig $ testBot Nothing diff --git a/haboli-testbot/haboli-testbot.cabal b/haboli-testbot/haboli-testbot.cabal new file mode 100644 index 0000000..47778a5 --- /dev/null +++ b/haboli-testbot/haboli-testbot.cabal @@ -0,0 +1,58 @@ +cabal-version: 1.12 + +-- This file has been generated from package.yaml by hpack version 0.31.2. +-- +-- see: https://github.com/sol/hpack +-- +-- hash: c59191837f2f234376576136c23122cdc5288703e37b782ee1908d225e22dcee + +name: haboli-testbot +version: 0.1.0.0 +synopsis: A bot used for testing small things +description: See +homepage: https://github.com/Garmelon/haboli-bot-collection#readme +bug-reports: https://github.com/Garmelon/haboli-bot-collection/issues +author: Garmelon +maintainer: Garmelon +copyright: 2020 Garmelon +license: MIT +build-type: Simple +extra-source-files: + README.md + +source-repository head + type: git + location: https://github.com/Garmelon/haboli-bot-collection + +library + exposed-modules: + Haboli.Bots.TestBot + other-modules: + Paths_haboli_testbot + hs-source-dirs: + src + build-depends: + base >=4.7 && <5 + , containers + , haboli + , microlens-platform + , text + , time + default-language: Haskell2010 + +executable haboli-testbot + main-is: Main.hs + other-modules: + Paths_haboli_testbot + hs-source-dirs: + app + ghc-options: -threaded -rtsopts -with-rtsopts=-N + build-depends: + base >=4.7 && <5 + , containers + , haboli + , haboli-testbot + , microlens-platform + , text + , time + default-language: Haskell2010 diff --git a/haboli-testbot/package.yaml b/haboli-testbot/package.yaml new file mode 100644 index 0000000..d3a76d6 --- /dev/null +++ b/haboli-testbot/package.yaml @@ -0,0 +1,34 @@ +name: haboli-testbot +version: 0.1.0.0 +license: MIT +author: Garmelon +copyright: 2020 Garmelon + +synopsis: A bot used for testing small things +description: See +github: Garmelon/haboli-bot-collection + +extra-source-files: + - README.md + +dependencies: + - base >= 4.7 && < 5 + - containers + - haboli + - microlens-platform + - text + - time + +library: + source-dirs: src + +executables: + haboli-testbot: + main: Main.hs + source-dirs: app + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N + dependencies: + - haboli-testbot diff --git a/haboli-testbot/src/Haboli/Bots/TestBot.hs b/haboli-testbot/src/Haboli/Bots/TestBot.hs new file mode 100644 index 0000000..7211fdc --- /dev/null +++ b/haboli-testbot/src/Haboli/Bots/TestBot.hs @@ -0,0 +1,62 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TemplateHaskell #-} + +module Haboli.Bots.TestBot + ( testBot + ) where + +import Control.Concurrent +import Control.Monad +import Control.Monad.IO.Class +import qualified Data.Text as T +import Data.Time +import Lens.Micro.Platform + +import Haboli.Euphoria +import Haboli.Euphoria.Botrulez + +data BotState = BotState + { _botStartTime :: UTCTime + , _botListing :: Listing + } deriving (Show) + +makeLenses ''BotState + +testBot :: Maybe T.Text -> Client T.Text () +testBot mPasswd = do + startTime <- liftIO getCurrentTime + initialEvents <- untilConnected $ + respondingToBounce mPasswd $ + respondingToPing nextEvent + let initialState = BotState startTime $ newListing initialEvents + stateVar <- liftIO $ newMVar initialState + preferNickVia botListing stateVar "TestBot" + botMain stateVar + +botMain :: MVar BotState -> Client T.Text () +botMain stateVar = forever $ do + event <- respondingToCommands (getCommands stateVar) $ + respondingToPing nextEvent + updateFromEventVia botListing stateVar event + +longHelp :: T.Text +longHelp = T.concat + [ "This bot exists to test various things." + , "\n" + , "\n" + , "Made by @Garmy using https://github.com/Garmelon/haboli/." + , "\n" + , "Source code available at https://github.com/Garmelon/haboli-bot-collection." + ] + +getCommands :: MVar BotState -> Client e [Command T.Text] +getCommands stateVar = do + state <- liftIO $ readMVar stateVar + let name = state ^. botListing . lsSelfL . svNickL + pure + [ botrulezPingGeneral + , botrulezPingSpecific name + , botrulezHelpSpecific name longHelp + , botrulezUptimeSpecific name $ state ^. botStartTime + , botrulezKillSpecific name + ] diff --git a/stack.yaml b/stack.yaml index 44f8a19..fcb4392 100644 --- a/stack.yaml +++ b/stack.yaml @@ -2,6 +2,7 @@ resolver: lts-15.7 packages: - haboli-infobot + - haboli-testbot extra-deps: - github: Garmelon/haboli