Talk:Roll your own IRC bot
Thanks for this useful tutorial! I implemented the normal chat functionality using forkIO as suggested but I'm not happy with my solution. Where would you put the call to forkIO? And how would you write the function that you pass it? Is it possible to somehow use privmsg in this function or do I have to duplicate that code more or less? --Gog 19:09, 18 January 2007 (UTC)
I had the same problem. You can't really forkIO directly from inside the Reader monad, even with liftIO. I ended up with this but maybe there is a better way:
privmsgIO :: Handle -> String -> IO () privmsgIO h s = writeIO h "PRIVMSG" (botChan ++ " :" ++ s)
-- Send a privmsg to the current chan + server privmsg :: String -> IrcBot () privmsg s = do h <- asks socket liftIO $ privmsgIO h s
then when I need to forkIO, I do
foo = do h <- asks socket liftIO $ forkIO (privmsg h ...)
and use the IO variant of privmsg.
This works for some things but I'm not sure what'd be the cleanest way to get information back to the main thread from the forkIO'd commands.
--Nurpax 16:20, 8 April 2012 (UTC)
Btw, I would highly recommend modifying the
connect to set the socket encoding to UTF-8:
-- Connect to the server and return the initial bot state connect :: IO IrcBotRO connect = notify $ do h <- connectTo server (PortNumber port) hSetEncoding h utf8
It took me a while to debug why my UTF8 output was all messed up. --Nurpax 16:21, 8 April 2012 (UTC)