From c6924dd5ac15761722f15e15bc304acf9df9e1b1 Mon Sep 17 00:00:00 2001 From: ShowierData9978 Date: Thu, 11 May 2023 19:42:22 -0500 Subject: [PATCH] Fix subcommands, fix #0000, fix ip thing --- MeowerBot/Bot.py | 36 ++++++++++++++++++++---------------- MeowerBot/command.py | 18 +++++++++++++++--- pyproject.toml | 2 +- tests/command.py | 2 +- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/MeowerBot/Bot.py b/MeowerBot/Bot.py index bb91825..a3f17ae 100644 --- a/MeowerBot/Bot.py +++ b/MeowerBot/Bot.py @@ -95,6 +95,12 @@ def run_cb(self, cbid, args=(), kwargs=None): # cq: ignore if not kwargs: kwargs = {} + + if cbid == "error" and isinstance(args[0], KeyboardInterrupt()): + self.logger.error("KeyboardInterrupt") + self.bad_exit = True + self.stop() + return kwargs["bot"] = self for callback in self.callbacks[cbid]: @@ -134,17 +140,6 @@ def _debug_fix(self, packet): self.run_cb("error", args=(e, )) def __handle_on_connect__(self): - self.wss.sendPacket( - { - "cmd": "direct", - "val": { - "cmd": "ip", - "val": requests.get("https://api.meower.org/ip").text, - }, - "listener": "__meowerbot__send_ip", - } - ) - self.wss.sendPacket( { "cmd": "direct", @@ -173,7 +168,7 @@ def inner(func): self.commands.update(info) - return func + return cmd #allow subcommands without a cog return inner @@ -261,6 +256,17 @@ def __handle_close__(self, *args, **kwargs): self.run_cb("close", args=args, kwargs=kwargs) + def handle_bridges(self, packet): + if packet["val"]["u"] in self.__bridges__ and ": " in packet["val"]["p"]: + split = packet["val"]["p"].split(": ", 1) + packet["val"]["p"] = split[1] + packet["val"]["u"] = split[0] + + if packet["val"]["p"].startswith(self.prefix+"#0000"): + packet["val"]["p"] = packet["val"]["p"].replace("#0000", self.prefix) + + return packet + def __handle_packet__(self, packet): if packet["cmd"] == "statuscode": @@ -273,10 +279,7 @@ def __handle_packet__(self, packet): self.run_cb("ulist", self.wss.statedata["ulist"]["usernames"]) elif packet["cmd"] == "direct" and "post_origin" in packet["val"]: - if packet["val"]["u"] in self.__bridges__ and ": " in packet["val"]["p"]: - split = packet["val"]["p"].split(": ", 1) - packet["val"]["p"] = split[1] - packet["val"]["u"] = split[0] + packet = self.handle_bridges(packet) ctx = CTX(packet["val"], self) if "message" in self.callbacks: @@ -324,6 +327,7 @@ def run_command(self, message): try: self.commands[args[0]]["command"].run_cmd(message.ctx, *args[1:]) except KeyError as e: + self.logger.error(traceback.format_exc()) self.run_cb("error", args=(e,)) def send_msg(self, msg, to="home"): diff --git a/MeowerBot/command.py b/MeowerBot/command.py index 2220ed3..ecdaf88 100644 --- a/MeowerBot/command.py +++ b/MeowerBot/command.py @@ -23,24 +23,36 @@ def __call__(self, *args): def register_class(self, con): self.connected = con + for subcommand in self.subcommands.values(): + subcommand.register_class(con) + def subcommand(self, name=None, args=0): def inner(func): cmd = AppCommand(func, name=name, args=args) cmd.register_class(self.connected) - self.subcommands[name] = cmd.info() + self.subcommands.update(cmd.info()) return func #dont want mb to register this as a root command - return inner + def run_cmd(self, ctx, *args): + print( + f"Running command {self.name} with args {args} and ctx {ctx} and connected {self.connected}" + + ) - if self.subcommands and (args[0] if len(args) >= 1 else None) in self.subcommands: + try: self.subcommands[args[0]]["command"].run_cmd(ctx, *args[1:]) return + except KeyError: + print(f"KeyError: {args}") + print(f"Subcommands: {self.subcommands}") + except IndexError: + print(f"IndexError: {args}") if not self.args == 0: args = args[: self.args] diff --git a/pyproject.toml b/pyproject.toml index 0bf8f0d..22074a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "MeowerBot" -version = "2.5.2" +version = "2.5.3" description = "A meower bot lib for py" authors = ["showierdata9978 <68120127+showierdata9978@users.noreply.github.com>"] license = "MIT" diff --git a/tests/command.py b/tests/command.py index 7bae512..a605762 100644 --- a/tests/command.py +++ b/tests/command.py @@ -23,5 +23,5 @@ def sub(ctx, *args): def reloadtime(ctx): ctx.send_msg(f"My reload time is {round(bot.autoreload_time)}" ) -bot.run(env['username'], env['password']) +bot.run(env['uname'], env['password'])