How to use the karmabot.core.commands.sets.CommandSet function in karmabot

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 / core / facets / name.py View on Github external
# Copyright the Karmabot authors and contributors.
# All rights reserved.  See AUTHORS.
#
# This file is part of 'karmabot' and is distributed under the BSD license.
# See LICENSE for more details.
from karmabot.core import ircutils
from karmabot.core.client import thing
from karmabot.core.commands.sets import CommandSet
from karmabot.core.thing import ThingFacet
from karmabot.core.register import facet_registry, presenter_registry


@facet_registry.register
class NameFacet(ThingFacet):
    name = "name"
    commands = thing.add_child(CommandSet(name))

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

    @commands.add(u"{thing}\?*", help_str=u"show information about {thing}")
    def describe(self, context, thing):
        # this is a thing object not the list of things
        context.reply(thing.describe(context))


@presenter_registry.register(set(["name"]), order=-10)
def present(thing, context):
    return u"{name}".format(name=ircutils.bold(thing.name))
github chromakode / karmabot / karmabot / core / facets / help.py View on Github external
from karmabot.core.client import thing
from karmabot.core.register import facet_registry
from karmabot.core.thing import ThingFacet
from karmabot.core.commands.sets import CommandSet
from itertools import chain


def numbered(strs):
    return (u"{0}. {1}".format(num + 1, line)
            for num, line in enumerate(strs))


@facet_registry.register
class HelpFacet(ThingFacet):
    name = "help"
    commands = thing.add_child(CommandSet(name))
    short_template = u"\"{0}\""
    full_template = short_template + u": {1}"

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

    def get_topics(self, this_thing):
        topics = dict()
        for cmd in chain(thing, this_thing.iter_commands()):
            if cmd.visible:
                topic = cmd.format.replace("{thing}", thing.name)
                help = cmd.help.replace("{thing}", thing.name)
                topics[topic] = help
        return topics
github chromakode / karmabot / karmabot / extensions / cs_schedule.py View on Github external
from karmabot.core.commands.sets import CommandSet

#TODO:
#      Get rid of course command syntax? maybe?
#      Add error handing to webpage fetching.
#      Search with different keys.


@facet_registry.register
class ScheduleFacet(Facet):
    """
    Class which implements the ThingFacet interface and provides the new
    karmabot course info reporting functionality.
    """
    name = "course"
    commands = thing.add_child(CommandSet(name))
    URL = "http://cs.pdx.edu/schedule/termschedule?"

    @commands.add(u"course {CSXXX} {TERM} {YEAR}",
                  u"Get course information from CS website.")
    def course(self, context, CSXXX, TERM, YEAR):
        """
        High level command handler for the course command. Manages
        course cache and recrawls if timeout is exceeded.
        """
        CSXXX = CSXXX.replace('CS', '').strip()
        sched_key = TERM + YEAR
        cur_time = time.time()
        response = ""
        _cls = ScheduleFacet
        defaults = {"ret_times": {}, "schedules": {}}
github chromakode / karmabot / karmabot / core / facets / irc.py View on Github external
# Copyright the Karmabot authors and contributors.
# All rights reserved.  See AUTHORS.
#
# This file is part of 'karmabot' and is distributed under the BSD license.
# See LICENSE for more details.
from karmabot.core.client import thing, listen
from karmabot.core.commands.sets import CommandSet
from karmabot.core.thing import ThingFacet
from karmabot.core.register import facet_registry, presenter_registry


@facet_registry.register
class IRCChannelFacet(ThingFacet):
    name = "ircchannel"
    commands = thing.add_child(CommandSet(name))

    @classmethod
    def does_attach(cls, thing):
        return thing.name.startswith("#")

    @commands.add(u"join {thing}", help_str=u"join the channel {thing}")
    def join(self, thing, context):
        context.bot.join_with_key(thing.name.encode("utf-8"))

    @commands.add(u"leave {thing}", help_str=u"leave the channel {thing}")
    def leave(self, thing, context):
        channel = thing.name.encode("utf-8")
        context.reply("Bye!", where=channel)
        context.bot.leave(channel)

    # @commands.add("set topic of {thing} to {topic}",
github chromakode / karmabot / karmabot / extensions / lmgtfy.py View on Github external
pass
        else:
            # named entity
            try:
                text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
            except KeyError:
                pass
        # leave as is
        return text
    return re.sub("&#?\w+;", fixup, text)


@facet_registry.register
class LmgtfyFacet(ThingFacet):
    name = "lmgtfy"
    commands = thing.add_child(CommandSet(name))

    @classmethod
    def does_attach(cls, thing):
        return thing.name == "lmgtfy"

    @commands.add(u"lmgtfy {item}",
                  u"googles for a {item}")
    def lmgtfy(self, context, item):
        api_url = "http://ajax.googleapis.com/ajax/services/search/web?"
        response = urlopen(api_url + urlencode(dict(v="1.0",
                                                    q=item)))
        response = dict(JSONDecoder().decode(response.read()))
        top_result = {}
        if response.get('responseStatus') == 200:
            results = response.get('responseData').get('results')
            top_result = results.pop(0)
github chromakode / karmabot / karmabot / core / facets / karma.py View on Github external
# Copyright the Karmabot authors and contributors.
# All rights reserved.  See AUTHORS.
#
# This file is part of 'karmabot' and is distributed under the BSD license.
# See LICENSE for more details.
from karmabot.core.client import listen, thing
from karmabot.core.thing import ThingFacet
from karmabot.core.commands.sets import CommandSet
from karmabot.core.register import facet_registry, presenter_registry


@facet_registry.register
class KarmaFacet(ThingFacet):
    name = "karma"
    listens = listen.add_child(CommandSet(name))

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

    @listens.add(u"{thing}++", help_str=u"add 1 to karma")
    def inc(self, thing, context):
        self.data.setdefault(context.who, 0)
        self.data[context.who] += 1
        return thing.name

    @listens.add(u"{thing}--", help_str=u"subtract 1 from karma")
    def dec(self, thing, context):
        self.data.setdefault(context.who, 0)
        self.data[context.who] -= 1
        return thing.name