How to use the karmabot.core.thing.ThingFacet 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 / facets / help.py View on Github external
# This file is part of 'karmabot' and is distributed under the BSD license.
# See LICENSE for more details.
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
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)
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)
github chromakode / karmabot / karmabot / core / facets / irc.py View on Github external
return self.data.get("topic", None)

    @topic.setter
    def topic(self, value):
        self.data["topic"] = value


@presenter_registry.register(set(["ircchannel"]))
def present(thing, context):
    facet = thing.facets["ircchannel"]
    if facet.topic:
        return u"Topic: {topic}".format(topic=facet.topic)


@facet_registry.register
class IRCUserFacet(ThingFacet):
    #TODO: IRCUser facet, with trusted/admin types and verified hostmasks
    name = "ircuser"

    @classmethod
    def does_attach(cls, thing):
        # Attached by the listener
        return False

    @property
    def is_verified(self):
        return self.data.get("verified", False)

    @is_verified.setter
    def is_verified(self, value):
        self.data["verified"] = value
github chromakode / karmabot / karmabot / core / facets / bot.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 VERSION
from karmabot.core import thing
from karmabot.core.register import facet_registry, presenter_registry


@facet_registry.register
class KarmaBotFacet(thing.ThingFacet):
    name = "karmabot"

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

    #TODO: add save/reload/quit commands, customizable messages and behavior


@presenter_registry.register(set(["karmabot", "name", "karma", "description"]))
def present(thing, context):
    output_str = u"{name}[v{version}]({karma}): {descriptions} ({things} things)"
    text = output_str.format(
        name=thing.describe(context, facets=set(["name"])),
        karma=thing.facets["karma"].karma,
        descriptions=thing.facets["description"].present(),
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 / extensions / lmgtfy.py View on Github external
return unichr(int(text[2:-1]))
            except ValueError:
                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: