Skip to content

Instantly share code, notes, and snippets.

@prologic
Created May 19, 2014 03:06
Show Gist options
  • Select an option

  • Save prologic/d11b6f1cc74368a5c6f8 to your computer and use it in GitHub Desktop.

Select an option

Save prologic/d11b6f1cc74368a5c6f8 to your computer and use it in GitHub Desktop.
Pico IRC Bot using circuits in 24 Lines of Code.
from circuits import Component
from circuits.net.sockets import TCPClient, connect
from circuits.net.protocols.irc import IRC, PRIVMSG, USER, NICK, JOIN, RPL_ENDOFMOTD, ERR_NOMOTD # noqa
class PBot(Component):
def init(self, host, port=6667):
self.target = (host, port)
self += (TCPClient() + IRC())
def ready(self, *args):
self.fire(connect(*self.target))
def connected(self, host, port):
self.fire(USER("pbot", self.target[0], self.target[1], "pbot"))
self.fire(NICK("pbot"))
def numeric(self, source, target, numeric, args, message):
if numeric in (RPL_ENDOFMOTD, ERR_NOMOTD):
self.fire(JOIN("#circuits"))
self.fire(PRIVMSG("#circuits", "Hello World!"))
PBot("irc.freenode.net").run()
@prologic

Copy link
Copy Markdown
Author

This is in response to Python: IRC client in 19 lines which I can't beat in circuits but nevertheless 24 Lines of Code is still pretty good for a framework like circuits.

Enjoy!

@prologic

Copy link
Copy Markdown
Author

Upon further review of the article this gist is challenging, I notice that there are a few bugs off the bat.

  • connected is never set to True
  • The 19 loc bot will fail miserably on imperfect server responses.
  • The 19 loc bot will likely not succeed in connecting or joining the channel due to various edge cases.

@prologic

Copy link
Copy Markdown
Author

Note: The pbot.py example above is based on the circuits framework and the ircbot.py example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment