yellowtides / owenbot-hs

A utility bot written in Haskell for the Edinburgh Informatics Class of ‘24’s main messaging platform (Discord server).
https://yellowtid.es/owenbot-hs/
BSD 3-Clause "New" or "Revised" License
8 stars 7 forks source link

Implement Commands, 1000x faster parsers, and polymorphic functions interacting with Discord #34

Closed yutotakano closed 3 years ago

yutotakano commented 3 years ago

Learning from last time's long changelog, this one's left short and to the point.

Read the generated Haddock documentation (after this is merged) to read more if you want!

Commands

Example

receivers =
    [ runCommand setStatus,
    , runCommand ...
    , runHelp [setStatus, ...]
    ]

simple :: Command DiscordHandler
simple = command "ping" $ \msg -> respond msg "pong!"

setStatus :: Command DiscordHandler
setStatus
    = prefix "~~!"
    . requires (\msg -> pure $ Just "needs special privs!")
    . requires (\msg -> isUserMod msg >> pure $ Just "another permission issue!")
    . help "this command cannot be run because no one has the privs"
    . command "status"
    $ \msg newStatus newType (Remaining newName) -> do
        -- newStatus is automatically parsed to UpdateStatusType, 
        -- newType is parsed to ActivitiyType,
        -- and newName to T.Text
        updateStatus newStatus newType newName
        respond msg "Status updated :) Keep in mind it may take up to a minute for your client to refresh."

Aux

MonadDiscord

-- create a message from pure IO using an Auth token
main :: IO ()
main = runReaderT func (Auth "<token here>")

-- create a message from a discord handler
receiver :: DiscordHandler ()
receiver = func

-- polymorphic function 
func :: (MonadDiscord m) => m ()
func = void $ createMessage (read "123123") "This is sent from polymorphic function"
yutotakano commented 3 years ago

owo