Skip to content

Instantly share code, notes, and snippets.

@fabiocerqueira
Created January 5, 2017 15:41
Show Gist options
  • Select an option

  • Save fabiocerqueira/63700bbe8237d1b6fd730e1b74c9474a to your computer and use it in GitHub Desktop.

Select an option

Save fabiocerqueira/63700bbe8237d1b6fd730e1b74c9474a to your computer and use it in GitHub Desktop.
Simple example running an echo bot on IRC using asyncio and quamash
import asyncio
import sys
import signal
import logging
from PyQt5.QtWidgets import QApplication
from quamash import QEventLoop
from PyIRC.signal import event
from PyIRC.io.asyncio import IRCProtocol
from PyIRC.extensions import bot_recommended
logging.basicConfig(level="DEBUG")
class EchoBotIRCProtocol(IRCProtocol):
@event("commands", "PRIVMSG")
def respond(self, event, line):
params = line.params
if len(params) < 2:
return
self.send("PRIVMSG", (line.hostmask.nick, params[1]))
def sigint(*protos):
for proto in protos:
try:
proto.send("QUIT", ["Terminating due to ctrl-c!"])
proto.close()
except Exception as e:
# Ugh! A race probably happened. Yay, signals.
pass
print()
print("Terminating due to ctrl-c!")
quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
loop = QEventLoop(app)
asyncio.set_event_loop(loop)
args = {
'serverport': ('weber.freenode.net', 6667),
'ssl': False,
'username': 'EchoBotTesting',
'nick': 'EchoBotTesting',
'gecos': 'I am a test, pls ignore :)',
'extensions': bot_recommended,
'join': ['#testingpyirc'],
}
inst = EchoBotIRCProtocol(**args)
loop.add_signal_handler(signal.SIGINT, sigint, inst)
loop.run_until_complete(inst.connect())
try:
loop.run_forever()
finally:
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment