Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
Last active July 3, 2017 21:41
Show Gist options
  • Save numberoverzero/c99df568b7703275fa349892bab7bf3c to your computer and use it in GitHub Desktop.
Save numberoverzero/c99df568b7703275fa349892bab7bf3c to your computer and use it in GitHub Desktop.
Another way to attach event handlers to a bottom Client without hardcoding the Client instance
import bottom
from collections import defaultdict
_handlers = defaultdict(list)
def attach_handlers(client: bottom.Client) -> bottom.Client:
for event, funcs in _handlers.items():
for func_builder in funcs:
client.on(event, func=func_builder(client))
def make_keepalive(client):
def keepalive(message=None, **kw):
message = message or ""
client.send("pong", message=message)
return keepalive
_handlers["ping"].append(make_keepalive)
def make_echo(client):
def respond(nick, target, message, **kwargs):
bot.send("privmsg", target=target, message=message)
return respond
_handlers["privmsg"].append(make_echo)
from bottom import Client
from views import attach_handlers
client = Client(...)
attach_handlers(client)
bot.loop.create_task(bot.connect())
bot.loop.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment