website/backend/src/Main.hs

41 lines
1.2 KiB
Haskell
Raw Normal View History

2024-12-05 15:44:50 -06:00
-- allows "string literals" to be Text
{-# LANGUAGE OverloadedStrings #-}
2024-12-05 02:26:48 -06:00
module Main where
2024-12-05 15:44:50 -06:00
import Control.Monad (void, when)
import Data.Text (Text, isPrefixOf, toLower)
import Data.Text.IO as TIO
import Discord
import Discord.Requests as R
import Discord.Types
import UnliftIO.Concurrent
-- | Replies "pong" to every message that starts with "ping"
2024-12-05 02:26:48 -06:00
main :: IO ()
2024-12-05 15:44:50 -06:00
main = do
userFacingError <-
runDiscord $
def
{ discordToken = "Bot ZZZZZZZZZZZZZZZZZZZ",
discordOnEvent = eventHandler,
discordOnLog = \s -> TIO.putStrLn s >> TIO.putStrLn ""
} -- if you see OnLog error, post in the discord / open an issue
TIO.putStrLn userFacingError
-- userFacingError is an unrecoverable error
-- put normal 'cleanup' code in discordOnEnd (see examples)
eventHandler :: Event -> DiscordHandler ()
eventHandler event = case event of
MessageCreate m -> when (isPing m && not (fromBot m)) $ do
void $ restCall (R.CreateReaction (messageChannelId m, messageId m) "eyes")
threadDelay (2 * 10 ^ 6)
void $ restCall (R.CreateMessage (messageChannelId m) "Pong!")
_ -> return ()
fromBot :: Message -> Bool
fromBot = userIsBot . messageAuthor
isPing :: Message -> Bool
isPing = ("ping" `isPrefixOf`) . toLower . messageContent