def test_hook_decorate(): from cloudbot import hook from cloudbot.event import EventType @hook.event(EventType.message) @hook.event([EventType.notice, EventType.action]) @hook.command('test') @hook.irc_raw('*') @hook.irc_raw(['PRIVMSG']) @hook.irc_out() @hook.on_stop() @hook.on_start() @hook.regex(['test', re.compile('test')]) @hook.regex('test1') @hook.regex(re.compile('test2')) @hook.periodic(20) @hook.permission('perm') @hook.post_hook() @hook.on_connect() @hook.on_cap_ack('capname') @hook.on_cap_available('capname') def f(): pass # pragma: no cover assert f._cloudbot_hook['event'].types == { EventType.message, EventType.notice, EventType.action } assert f._cloudbot_hook['command'].aliases == {'test'} assert f._cloudbot_hook['irc_raw'].triggers == {'*', 'PRIVMSG'} assert 'irc_out' in f._cloudbot_hook assert 'on_start' in f._cloudbot_hook assert 'on_stop' in f._cloudbot_hook assert 'regex' in f._cloudbot_hook assert 'periodic' in f._cloudbot_hook assert 'perm_check' in f._cloudbot_hook assert 'post_hook' in f._cloudbot_hook assert 'on_connect' in f._cloudbot_hook assert 'on_cap_available' in f._cloudbot_hook assert 'on_cap_ack' in f._cloudbot_hook assert len(f._cloudbot_hook['regex'].regexes) == 4 assert f._cloudbot_hook['periodic'].interval == 20 with pytest.raises(ValueError, match="Invalid command name test 123"): hook.command('test 123')(f) with pytest.raises(TypeError): hook.periodic(f) with pytest.raises(TypeError): hook.regex(f) with pytest.raises(TypeError): hook.event(f) with pytest.raises(TypeError): hook.irc_raw(f) @hook.sieve def sieve_func(bot, event, _hook): pass # pragma: no cover assert 'sieve' in sieve_func._cloudbot_hook @hook.sieve() def sieve_func2(bot, event, _hook): pass # pragma: no cover assert 'sieve' in sieve_func2._cloudbot_hook @hook.on_connect @hook.irc_out @hook.post_hook @hook.on_start @hook.on_stop def plain_dec(bot, event, _hook): pass # pragma: no cover assert 'on_connect' in plain_dec._cloudbot_hook assert 'irc_out' in plain_dec._cloudbot_hook assert 'post_hook' in plain_dec._cloudbot_hook assert 'on_start' in plain_dec._cloudbot_hook assert 'on_stop' in plain_dec._cloudbot_hook
def init_aliases(): for alias in ALIASES: _hook = alias_wrapper(alias) globals()[_hook.__name__] = hook.command(*alias.cmds, autohelp=False)(_hook)
def create_basic_hooks(): for attack in ATTACKS: globals()[attack.name] = hook.command(*attack.commands, autohelp=attack.require_target)( basic_attack(attack))
def init_hooks(): for food in BASIC_FOOD: globals()[food.name] = hook.command(*food.commands)(basic_food(food))
def init_hooks(): for game in GAMES: func = score_hook(game) globals()[func.__name__] = hook.command(*game.cmds, autohelp=False)(func)
kwargs['user'] = user generator = textgen.TextGenerator(data["templates"], data["parts"], variables=kwargs) return generator.generate_string() def make_cmd_list(value): if isinstance(value, str): value = [value] return value def basic_food(food): def func(text, action, is_nick_valid): if not is_nick_valid(text): return "I can't give {} to that user.".format(food.unitname) action(basic_format(text, basic_food_data[food.name])) func.__name__ = food.name func.__doc__ = "<user> - gives {} to [user]".format(food.unitname) return func for food in BASIC_FOOD: globals()[food.name] = hook.command(*make_cmd_list(food.commands))( basic_food(food))
def _karma(text, db): """k/karma <nick> -- returns karma stats for <nick>""" query = db.execute( select([karma_table]) .where(karma_table.c.nick_vote == text.lower()) ).fetchone() if not query: return "That user has no karma :(" else: return "{} has \x02{}\x02 karma!".format(text, query['up_karma'] - query['down_karma']) karma = hook.command('karma', 'k')(_karma) @hook.command('loved') def loved(db): """loved -- Shows the users with the most karma!""" query = db.execute( select([karma_table]) .order_by(karma_table.c.total_karma.desc()) .limit(5) ).fetchall() if not query: return "??" else: return query