Code Monkey home page Code Monkey logo

bottom's Introduction

https://readthedocs.org/projects/bottom-docs/badge?style=flat-square https://img.shields.io/travis/numberoverzero/bottom/master.svg?style=flat-square https://img.shields.io/pypi/v/bottom.svg?style=flat-square https://img.shields.io/github/issues-raw/numberoverzero/bottom.svg?style=flat-square https://img.shields.io/pypi/l/bottom.svg?style=flat-square

asyncio-based rfc2812-compliant IRC Client (3.8+)

bottom isn't a kitchen-sink library. Instead, it provides a consistent API with a small surface area, tuned for performance and ease of extension. Similar to the routing style of bottle.py, hooking into events is one line.

Installation

pip install bottom

Getting Started

(the full documentation is available here: http://bottom-docs.readthedocs.io/)

Create an instance:

import asyncio
import bottom

host = 'chat.freenode.net'
port = 6697
ssl = True

NICK = "bottom-bot"
CHANNEL = "#bottom-dev"

bot = bottom.Client(host=host, port=port, ssl=ssl)

Send nick/user/join when connection is established:

@bot.on('CLIENT_CONNECT')
async def connect(**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 = await asyncio.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.
    for future in pending:
        future.cancel()

    bot.send('JOIN', channel=CHANNEL)

Respond to ping:

@bot.on('PING')
def keepalive(message, **kwargs):
    bot.send('PONG', message=message)

Echo messages (channel and direct):

@bot.on('PRIVMSG')
def message(nick, target, message, **kwargs):
    """ Echo all messages """
    # don't echo self
    if nick == NICK: return
    # respond directly
    if target == NICK: target = nick
    bot.send("PRIVMSG", target=target, message=message)

Connect and run the bot forever:

bot.loop.create_task(bot.connect())
bot.loop.run_forever()

API

The full API consists of 1 class, with 8 methods:

# manage connections

async Client.connect()
async Client.disconnect()

# send, receive, and wait for rfc-2812 messages

Client.send(command, **kwargs)
@Client.on(event)
Client.trigger(event, **kwargs)
async Client.wait(event)

# send and receive anything newline-terminated,
# provided for eg. IRCv3 extensions

Client.send_raw(message)
Client.handle_raw(message)

Contributors

bottom's People

Contributors

amorporkian avatar dependabot[bot] avatar doc-hex avatar fahhem avatar gbw avatar johanlorenzo avatar lalitmaganti avatar larsks avatar miedzinski avatar nedbat avatar numberoverzero avatar yay295 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.