Personal tools

Talk:Roll your own IRC bot

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(question about forkIO stuff)
 
Line 1: Line 1:
 
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?
 
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?
 
--[[User:Gog|Gog]] 19:09, 18 January 2007 (UTC)
 
--[[User:Gog|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.

Revision as of 16:19, 8 April 2012

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.