diff options
| author | 2018-08-31 10:51:47 +0100 | |
|---|---|---|
| committer | 2018-08-31 10:51:47 +0100 | |
| commit | 90ce92dc39395444862edfc7946ef7a1195f2464 (patch) | |
| tree | 6832c146a15879df292d265083bfe5795cbdfa69 /modules/commands.py | |
| parent | Reformat (diff) | |
Revert "Reformat"
This reverts commit abed9cf
Diffstat (limited to 'modules/commands.py')
| -rw-r--r-- | modules/commands.py | 84 |
1 files changed, 26 insertions, 58 deletions
diff --git a/modules/commands.py b/modules/commands.py index f75bf6db..fbce393d 100644 --- a/modules/commands.py +++ b/modules/commands.py @@ -8,51 +8,41 @@ OUT_CUTOFF = 400 REGEX_CUTOFF = re.compile("^.{1,%d}(?:\s|$)" % OUT_CUTOFF) - class Out(object): def __init__(self, module_name, target): self.module_name = module_name self.target = target self._text = "" self.written = False - def write(self, text): self._text += text self.written = True return self - def send(self): if self.has_text(): text = self._text text_encoded = text.encode("utf8") if len(text_encoded) > OUT_CUTOFF: text = "%s%s" % (text_encoded[:OUT_CUTOFF].decode("utf8" - ).rstrip(), - STR_MORE) + ).rstrip(), STR_MORE) self._text = "%s%s" % (STR_CONTINUED, text_encoded[OUT_CUTOFF: - ].decode("utf8").lstrip()) + ].decode("utf8").lstrip()) else: self._text = "" self.target.send_message(text, prefix="[%s] " % self.prefix()) - def set_prefix(self, prefix): self.module_name = prefix - def has_text(self): return bool(self._text) - class StdOut(Out): def prefix(self): return "%s%s%s" % (Utils.color(Utils.COLOR_GREEN), - self.module_name, Utils.FONT_RESET) - - + self.module_name, Utils.FONT_RESET) class StdErr(Out): def prefix(self): return "%s!%s%s" % (Utils.color(Utils.COLOR_RED), - self.module_name, Utils.FONT_RESET) - + self.module_name, Utils.FONT_RESET) class Module(object): def __init__(self, bot): @@ -62,19 +52,16 @@ class Module(object): bot.events.on("received").on("message").on("private").hook( self.private_message) bot.events.on("received").on("command").on("help").hook(self.help, - help="Show help for commands", - usage="<command>") + help="Show help for commands", usage="<command>") bot.events.on("received").on("command").on("usage").hook(self.usage, - help="Show usage help for commands", - min_args=1, - usage="<command>") + help="Show usage help for commands", min_args=1, + usage="<command>") bot.events.on("received").on("command").on("more").hook(self.more, - help="Get more output from the last command", - skip_out=True) + help="Get more output from the last command", skip_out=True) bot.events.on("postboot").on("configure").on( "channelset").assure_call(setting="command-prefix", - help="Set the command prefix used in this channel") + help="Set the command prefix used in this channel") bot.events.on("new").on("user", "channel").hook(self.new) bot.events.on("send").on("stdout").hook(self.send_stdout) @@ -91,16 +78,14 @@ class Module(object): def has_command(self, command): return command.lower() in self.bot.events.on("received").on( "command").get_children() - def get_hook(self, command): return self.bot.events.on("received").on("command").on(command - ).get_hooks()[0] + ).get_hooks()[0] def is_highlight(self, server, s): return s.lower() == server.nickname_lower or (s.lower().startswith( server.nickname_lower) and len(s) == len(server.nickname_lower - ) + 1 and s[-1] in [":", - ","]) + )+1 and s[-1] in [":", ","]) def message(self, event, command, args_index=1): if self.has_command(command): @@ -121,16 +106,11 @@ class Module(object): module_name = hook.function.__self__._name stdout, stderr = StdOut(module_name, target), StdErr(module_name, - target) + target) returns = self.bot.events.on("preprocess").on("command" - ).call(hook=hook, - user=event[ - "user"], - server=event[ - "server"], - target=target, - is_channel=is_channel) + ).call(hook=hook, user=event["user"], server=event["server"], + target=target, is_channel=is_channel) for returned in returns: if returned: stderr.write(returned).send() @@ -144,14 +124,13 @@ class Module(object): command, hook.kwargs["usage"])).send() else: stderr.write("Not enough arguments (minimum: %d)" % min_args - ).send() + ).send() else: args = " ".join(args_split) server = event["server"] user = event["user"] self.bot.events.on("received").on("command").on(command - ).call_limited( - 1, user=user, server=server, + ).call_limited(1, user=user, server=server, target=target, buffer=buffer, args=args, args_split=args_split, stdout=stdout, stderr=stderr, command=command.lower(), is_channel=is_channel) @@ -162,12 +141,10 @@ class Module(object): target.last_stderr = stderr buffer.skip_next() + def channel_message(self, event): command_prefix = event["channel"].get_setting("command-prefix", - event[ - "server"].get_setting( - "command-prefix", - "!")) + event["server"].get_setting("command-prefix", "!")) if event["message_split"][0].startswith(command_prefix): command = event["message_split"][0].replace( command_prefix, "", 1).lower() @@ -187,21 +164,17 @@ class Module(object): command = event["args_split"][0].lower() if command in self.bot.events.on("received").on( "command").get_children(): - hooks = self.bot.events.on("received").on("command").on( - command).get_hooks() + hooks = self.bot.events.on("received").on("command").on(command).get_hooks() if hooks and "help" in hooks[0].kwargs: - event["stdout"].write( - "%s: %s" % (command, hooks[0].kwargs["help"])) + event["stdout"].write("%s: %s" % (command, hooks[0].kwargs["help"])) else: event["stderr"].write("No help available for %s" % command) else: event["stderr"].write("Unknown command '%s'" % command) else: help_available = [] - for child in self.bot.events.on("received").on( - "command").get_children(): - hooks = self.bot.events.on("received").on("command").on( - child).get_hooks() + for child in self.bot.events.on("received").on("command").get_children(): + hooks = self.bot.events.on("received").on("command").on(child).get_hooks() if hooks and "help" in hooks[0].kwargs: help_available.append(child) help_available = sorted(help_available) @@ -211,20 +184,16 @@ class Module(object): command = event["args_split"][0].lower() if command in self.bot.events.on("received").on( "command").get_children(): - hooks = self.bot.events.on("received").on("command").on( - command).get_hooks() + hooks = self.bot.events.on("received").on("command").on(command).get_hooks() if hooks and "usage" in hooks[0].kwargs: - event["stdout"].write( - "Usage: %s %s" % (command, hooks[0].kwargs["usage"])) + event["stdout"].write("Usage: %s %s" % (command, hooks[0].kwargs["usage"])) else: - event["stderr"].write( - "No usage help available for %s" % command) + event["stderr"].write("No usage help available for %s" % command) else: event["stderr"].write("Unknown command '%s'" % command) def more(self, event): - if event["target"].last_stdout and event[ - "target"].last_stdout.has_text(): + if event["target"].last_stdout and event["target"].last_stdout.has_text(): event["target"].last_stdout.send() def send_stdout(self, event): @@ -232,7 +201,6 @@ class Module(object): stdout.write(event["message"]).send() if stdout.has_text(): event["target"].last_stdout = stdout - def send_stderr(self, event): stderr = StdErr(event["module_name"], event["target"]) stderr.write(event["message"]).send() |
