基于异步的rfc2812兼容irc客户端
bottom的Python项目详细描述
基于异步的RFC2812兼容IRC客户端(3.5+)
底部不是厨房水槽图书馆。相反,它提供了一致的api 有一个小的表面积,调整性能和易于扩展。 与bottle.py的路由样式类似,连接到事件是一行。
安装
pip install bottom
开始
(完整的文档可在此处获得:http://bottom-docs.readthedocs.io/)
创建实例:
importasyncioimportbottomhost='chat.freenode.net'port=6697ssl=TrueNICK="bottom-bot"CHANNEL="#bottom-dev"bot=bottom.Client(host=host,port=port,ssl=ssl)
建立连接时发送nick/user/join:
@bot.on('CLIENT_CONNECT')asyncdefconnect(**kwargs):bot.send('NICK',nick=NICK)bot.send('USER',user=NICK,realname='https://github.com/numberoverzero/bottom')# Don't try to join channels until the server has# sent the MOTD, or signaled that there's no MOTD.done,pending=awaitasyncio.wait([bot.wait("RPL_ENDOFMOTD"),bot.wait("ERR_NOMOTD")],loop=bot.loop,return_when=asyncio.FIRST_COMPLETED)# Cancel whichever waiter's event didn't come in.forfutureinpending:future.cancel()bot.send('JOIN',channel=CHANNEL)
响应ping:
@bot.on('PING')defkeepalive(message,**kwargs):bot.send('PONG',message=message)
回送消息(频道和直接):
@bot.on('PRIVMSG')defmessage(nick,target,message,**kwargs):""" Echo all messages """# don't echo selfifnick==NICK:return# respond directlyiftarget==NICK:target=nickbot.send("PRIVMSG",target=target,message=message)
连接并永远运行机器人:
bot.loop.create_task(bot.connect())bot.loop.run_forever()
API
完整的api由1个类组成,有8个方法:
# manage connectionsasyncClient.connect()asyncClient.disconnect()# send, receive, and wait for rfc-2812 messagesClient.send(command,**kwargs)@Client.on(event)Client.trigger(event,**kwargs)asyncClient.wait(event)# send and receive anything newline-terminated,# provided for eg. IRCv3 extensionsClient.send_raw(message)Client.handle_raw(message)