def cmd_alias(msg): if not len(msg.args): raise IMException("!alias takes as argument an alias to extend.") alias = context.subparse(msg, msg.args[0]) if alias is None or not isinstance(alias, Command): raise IMException("%s is not a valid alias" % msg.args[0]) if alias.cmd in context.data.getNode("aliases").index: return Response( "%s corresponds to %s" % (alias.cmd, context.data.getNode("aliases").index[alias.cmd]["origin"]), channel=msg.channel, nick=msg.frm) elif len(msg.args) > 1: create_alias(alias.cmd, " ".join(msg.args[1:]), channel=msg.channel, creator=msg.frm) return Response("New alias %s successfully registered." % alias.cmd, channel=msg.channel) else: wym = [ m for m in guess(alias.cmd, context.data.getNode("aliases").index) ] raise IMException(msg.args[0] + " is not an alias." + (" Would you mean: %s?" % ", ".join(wym) if len(wym) else ""))
def _in_miss(self, msg): from nemubot.message.command import Command as CommandMessage from nemubot.message.directask import DirectAsk as DirectAskMessage if isinstance(msg, CommandMessage): from nemubot.hooks import Command as CommandHook from nemubot.tools.human import guess hooks = self.hm.get_reverse_hooks("in", type(msg).__name__) suggest = [ s for s in guess(msg.cmd, [ h.name for h in hooks if isinstance(h, CommandHook) and h.name is not None ]) ] if len(suggest) >= 1: yield DirectAskMessage( msg.frm, "Unknown command %s. Would you mean: %s?" % (msg.cmd, ", ".join(suggest)), to=msg.to_response) elif isinstance(msg, DirectAskMessage): yield DirectAskMessage( msg.frm, "Sorry, I'm just a bot and your sentence is too complex for me :( But feel free to teach me some tricks at https://github.com/nemunaire/nemubot/!", to=msg.to_response)
def _in_miss(self, msg): from nemubot.message.command import Command as CommandMessage from nemubot.message.directask import DirectAsk as DirectAskMessage if isinstance(msg, CommandMessage): from nemubot.hooks import Command as CommandHook from nemubot.tools.human import guess hooks = self.hm.get_reverse_hooks("in", type(msg).__name__) suggest = [ s for s in guess(msg.cmd, [h.name for h in hooks if isinstance(h, CommandHook) and h.name is not None]) ] if len(suggest) >= 1: yield DirectAskMessage( msg.frm, "Unknown command %s. Would you mean: %s?" % (msg.cmd, ", ".join(suggest)), to=msg.to_response, ) elif isinstance(msg, DirectAskMessage): yield DirectAskMessage( msg.frm, "Sorry, I'm just a bot and your sentence is too complex for me :( But feel free to teach me some tricks at https://github.com/nemunaire/nemubot/!", to=msg.to_response, )
def check(self, mkw): for k in mkw: if ((k + "?") not in self.chk_args) and ((mkw[k] and k not in self.chk_args) or (not mkw[k] and k not in self.chk_noarg)): if mkw[k] and k in self.chk_noarg: raise KeywordException("Keyword %s doesn't take value." % k) elif not mkw[k] and k in self.chk_args: raise KeywordException("Keyword %s requires a value." % k) else: ch = [c for c in guess(k, self.d)] raise KeywordException("Unknown keyword %s." % k + (" Did you mean: " + ", ".join(ch) + "?" if len(ch) else "")) return super().check(mkw)
def check(self, mkw): for k in mkw: if ((k + "?") not in self.chk_args) and ( (mkw[k] and k not in self.chk_args) or (not mkw[k] and k not in self.chk_noarg)): if mkw[k] and k in self.chk_noarg: raise KeywordException("Keyword %s doesn't take value." % k) elif not mkw[k] and k in self.chk_args: raise KeywordException("Keyword %s requires a value." % k) else: ch = [c for c in guess(k, self.d)] raise KeywordException("Unknown keyword %s." % k + (" Did you mean: " + ", ".join(ch) + "?" if len(ch) else "")) return super().check(mkw)
def cmd_alias(msg): if not len(msg.args): raise IMException("!alias takes as argument an alias to extend.") alias = context.subparse(msg, msg.args[0]) if alias is None or not isinstance(alias, Command): raise IMException("%s is not a valid alias" % msg.args[0]) if alias.cmd in context.data.getNode("aliases").index: return Response("%s corresponds to %s" % (alias.cmd, context.data.getNode("aliases").index[alias.cmd]["origin"]), channel=msg.channel, nick=msg.frm) elif len(msg.args) > 1: create_alias(alias.cmd, " ".join(msg.args[1:]), channel=msg.channel, creator=msg.frm) return Response("New alias %s successfully registered." % alias.cmd, channel=msg.channel) else: wym = [m for m in guess(alias.cmd, context.data.getNode("aliases").index)] raise IMException(msg.args[0] + " is not an alias." + (" Would you mean: %s?" % ", ".join(wym) if len(wym) else ""))
def test_guess(self): self.assertListEqual([g for g in guess("drunk", ["eat", "drink"])], ["drink"]) self.assertListEqual([g for g in guess("drunk", ["long", "short"])], [])
def test_guess(self): self.assertListEqual([g for g in guess("drunk", ["eat", "drink"])], ["drink"]) self.assertListEqual([g for g in guess("drunk", ["long", "short"])], [])