Last active
July 3, 2017 21:41
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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