| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
from errbot import BotPlugin, Command, arg_botcmd, botcmd, botmatch |
| 4 |
|
| 5 |
|
| 6 |
def say_foo(plugin, msg, args): |
| 7 |
return "foo %s" % type(plugin) |
| 8 |
|
| 9 |
|
| 10 |
class Dyna(BotPlugin): |
| 11 |
"""Just a test plugin to see if dynamic plugin API works.""" |
| 12 |
|
| 13 |
@botcmd |
| 14 |
def add_simple(self, _, _1): |
| 15 |
simple1 = Command( |
| 16 |
lambda plugin, msg, args: "yep %s" % type(plugin), name="say_yep" |
| 17 |
) |
| 18 |
simple2 = Command(say_foo) |
| 19 |
|
| 20 |
self.create_dynamic_plugin( |
| 21 |
"simple with special#", (simple1, simple2), doc="documented" |
| 22 |
) |
| 23 |
|
| 24 |
return "added" |
| 25 |
|
| 26 |
@botcmd |
| 27 |
def remove_simple(self, msg, args): |
| 28 |
self.destroy_dynamic_plugin("simple with special#") |
| 29 |
return "removed" |
| 30 |
|
| 31 |
@botcmd |
| 32 |
def add_arg(self, _, _1): |
| 33 |
cmd1_name = "echo_to_me" |
| 34 |
cmd1 = Command( |
| 35 |
lambda plugin, msg, args: "string to echo is %s" % args.positional_arg, |
| 36 |
cmd_type=arg_botcmd, |
| 37 |
cmd_args=("positional_arg",), |
| 38 |
cmd_kwargs={"unpack_args": False, "name": cmd1_name}, |
| 39 |
name=cmd1_name, |
| 40 |
) |
| 41 |
|
| 42 |
self.create_dynamic_plugin("arg", (cmd1,), doc="documented") |
| 43 |
|
| 44 |
return "added" |
| 45 |
|
| 46 |
@botcmd |
| 47 |
def remove_arg(self, msg, args): |
| 48 |
self.destroy_dynamic_plugin("arg") |
| 49 |
return "removed" |
| 50 |
|
| 51 |
@botcmd |
| 52 |
def add_re(self, _, _1): |
| 53 |
re1 = Command( |
| 54 |
lambda plugin, msg, match: "fffound", |
| 55 |
name="ffound", |
| 56 |
cmd_type=botmatch, |
| 57 |
cmd_args=(r"^.*cheese.*$",), |
| 58 |
) |
| 59 |
self.create_dynamic_plugin("re", (re1,)) |
| 60 |
return "added" |
| 61 |
|
| 62 |
@botcmd |
| 63 |
def remove_re(self, msg, args): |
| 64 |
self.destroy_dynamic_plugin("re") |
| 65 |
return "removed" |
| 66 |
|
| 67 |
@botcmd |
| 68 |
def add_saw(self, _, _1): |
| 69 |
re1 = Command( |
| 70 |
lambda plugin, msg, args: "+".join(args), |
| 71 |
name="splitme", |
| 72 |
cmd_type=botcmd, |
| 73 |
cmd_kwargs={"split_args_with": ","}, |
| 74 |
) |
| 75 |
self.create_dynamic_plugin("saw", (re1,)) |
| 76 |
return "added" |
| 77 |
|
| 78 |
@botcmd |
| 79 |
def remove_saw(self, msg, args): |
| 80 |
self.destroy_dynamic_plugin("saw") |
| 81 |
return "removed" |
| 82 |
|
| 83 |
@botcmd |
| 84 |
def clash(self, msg, args): |
| 85 |
return "original" |
| 86 |
|
| 87 |
@botcmd |
| 88 |
def add_clashing(self, _, _1): |
| 89 |
simple1 = Command(lambda plugin, msg, args: "dynamic", name="clash") |
| 90 |
self.create_dynamic_plugin("clashing", (simple1,)) |
| 91 |
return "added" |
| 92 |
|
| 93 |
@botcmd |
| 94 |
def remove_clashing(self, _, _1): |
| 95 |
self.destroy_dynamic_plugin("clashing") |
| 96 |
return "removed" |