How to use karmabot - 10 common examples

To help you get started, we’ve selected a few karmabot examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github chromakode / karmabot / karmabot / core / client.py View on Github external
# This file is part of 'karmabot' and is distributed under the BSD license.
# See LICENSE for more details.
import random

from twisted.words.protocols import irc
from twisted.internet import reactor, task
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.python import log

from .thing import ThingStore
from .commands.sets import CommandSet

VERSION = "0.2"

listen = CommandSet("listen")
thing = CommandSet("thing", regex_format="(^{0}$)")


class Context(object):

    def __init__(self, user, where, bot, private=False):
        self.user = user
        self.where = where
        self.bot = bot
        self.replied = False
        self.private = private

    @property
    def nick(self):
        return self.user.split("!", 1)[0]

    @property
github chromakode / karmabot / karmabot / core / client.py View on Github external
#
# This file is part of 'karmabot' and is distributed under the BSD license.
# See LICENSE for more details.
import random

from twisted.words.protocols import irc
from twisted.internet import reactor, task
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.python import log

from .thing import ThingStore
from .commands.sets import CommandSet

VERSION = "0.2"

listen = CommandSet("listen")
thing = CommandSet("thing", regex_format="(^{0}$)")


class Context(object):

    def __init__(self, user, where, bot, private=False):
        self.user = user
        self.where = where
        self.bot = bot
        self.replied = False
        self.private = private

    @property
    def nick(self):
        return self.user.split("!", 1)[0]
github chromakode / karmabot / karmabot / command.py View on Github external
for command in traverse_commands(self):
            regex = command.to_regex()
            formatted_regex = self.regex_format.format(regex)

            command_info = {"re":        re.compile(formatted_regex, re.U),
                            "command":   command,
                            "exclusive": command.exclusive}
            command_infos.append(command_info)
        
        # Sort exclusive commands before non-exclusive ones
        command_infos.sort(key=lambda c:c["exclusive"], reverse=True)
        
        return CommandParser(command_infos)


class FacetCommandSet(CommandSet):
    def add(self, *args, **kwargs):
        def doit(handler):
            return CommandSet.add(self, *args, **kwargs)(thing_command(handler, self.name))
        
        return doit

# TODO: stripping listen commands such as --/++
class Command(object):

    def __init__(self, parent, format, handler, help=None, visible=True, exclusive=False):
        self.parent = parent
        self.format = format
        self.handler = handler
        self.help = help
        self.visible = visible
        self.exclusive = exclusive
github chromakode / karmabot / karmabot / command.py View on Github external
def doit(handler):
            return CommandSet.add(self, *args, **kwargs)(thing_command(handler, self.name))
github chromakode / karmabot / karmabot / extensions / github.py View on Github external
try:
    import json
except ImportError:
    import simplejson as json

from karmabot.client import thing
from karmabot.thing import facet_classes, presenters, ThingFacet
from karmabot import command
from karmabot.utils import Cache


@facet_classes.register
class GitHubFacet(ThingFacet):
    name = "github"
    commands = command.thing.add_child(command.FacetCommandSet(name))

    def __init__(self, thing_):
        ThingFacet.__init__(self, thing_)
        self.get_info = Cache(self._get_info, expire_seconds=10 * 60)

    @classmethod
    def does_attach(cls, thing):
        return False

    @commands.add(u"forget that {thing} is on github",
                  help=u"unset {thing}'s github username",
                  exclusive=True)
    def unset_github_username(self, thing, context):
        del self.data
        self.thing.detach_persistent(self)
github chromakode / karmabot / karmabot / extensions / twitter.py View on Github external
import urllib
from xml.sax.saxutils import unescape as unescape_html

try:
    import json
except ImportError:
    import simplejson as json
    
from karmabot import thing
from karmabot import command
from karmabot.utils import Cache

@thing.facet_classes.register
class TwitterFacet(thing.ThingFacet):
    name = "twitter"
    commands = command.thing.add_child(command.FacetCommandSet(name))
    
    def __init__(self, thing_):
        thing.ThingFacet.__init__(self, thing_)
        self.get_info = Cache(self._get_info, expire_seconds=10*60)
    
    @classmethod
    def does_attach(cls, thing):
        return False
        
    @commands.add(u"forget that {thing} is on twitter",
                  help=u"unset {thing}'s twitter username",
                  exclusive=True)
    def unset_twitterer(self, thing, context):
        del self.data
        self.thing.detach_persistent(self)
github chromakode / karmabot / karmabot / extensions / reddit.py View on Github external
import urllib

try:
    import json
except ImportError:
    import simplejson as json

from karmabot import thing
from karmabot import command
from karmabot.utils import Cache


@thing.facet_classes.register
class RedditorFacet(thing.ThingFacet):
    name = "redditor"
    commands = command.thing.add_child(command.FacetCommandSet(name))

    def __init__(self, thing_):
        thing.ThingFacet.__init__(self, thing_)
        self.get_info = Cache(self._get_info, expire_seconds=10 * 60)

    @classmethod
    def does_attach(cls, thing):
        return False

    @commands.add(u"forget that {thing} is a redditor",
                  help=u"unset {thing}'s reddit username",
                  exclusive=True)
    def unset_redditor(self, thing, context):
        del self.data
        self.thing.detach_persistent(self)
github chromakode / karmabot / karmabot / extensions / twitter.py View on Github external
@command.thing_command
def set_twitterer(thing, context):
    thing.attach_persistent(TwitterFacet)
github chromakode / karmabot / karmabot / extensions / eightball.py View on Github external
"You may rely on it",
		"Reply hazy, try again",
		"Ask again later",
		"Better not tell you now",
		"Cannot predict now",
		"Concentrate and ask again",
		"Don't count on it",
		"My reply is no",
		"My sources say no",
		"Outlook not so good",
		"Very doubtful"]

@thing.facet_classes.register
class EightBallFacet(thing.ThingFacet):
    name = "eightball"    
    commands = command.thing.add_child(command.FacetCommandSet(name))
    
    @classmethod
    def does_attach(cls, thing):
        return thing.name == "eightball"  
    
    @commands.add("shake {thing}", help="shake the magic eightball")
    def shake(self, thing, context):
        context.reply(random.choice(predictions) + ".")
github chromakode / karmabot / karmabot / facets / name.py View on Github external
from karmabot import thing
from karmabot import command
from karmabot import ircutils 

@thing.facet_classes.register
class NameFacet(thing.ThingFacet):
    name = "name"    
    commands = command.thing.add_child(command.FacetCommandSet(name))
    
    @classmethod
    def does_attach(cls, thing):
        return True   
    
    @commands.add(u"{thing}\?*", help=u"show information about {thing}")
    def describe(self, thing, context):
        context.reply(thing.describe(context))

@thing.presenters.register(set(["name"]), order=-10)
def present(thing, context):
    return u"{name}".format(name = ircutils.bold(thing.name))