diff --git a/modules/admin.py b/modules/admin.py index 77260711..7fa4c602 100644 --- a/modules/admin.py +++ b/modules/admin.py @@ -51,18 +51,16 @@ class Module(ModuleManager.BaseModule): """ id = event["args_split"][0] if not id.isdigit(): - event["stderr"].write("Please provide a numeric server ID") - return + raise utils.EventError("Please provide a numeric server ID") id = int(id) if not self.bot.database.servers.get(id): - event["stderr"].write("Unknown server ID") - return + raise utils.EventError("Unknown server ID") existing_server = self.bot.get_server(id) if existing_server: - event["stderr"].write("Already connected to %s" % str( + raise utils.EventError("Already connected to %s" % str( existing_server)) - return + server = self.bot.add_server(id) event["stdout"].write("Connecting to %s" % str(server)) diff --git a/modules/define.py b/modules/define.py index 9b8f3d14..b2714b12 100644 --- a/modules/define.py +++ b/modules/define.py @@ -58,9 +58,8 @@ class Module(ModuleManager.BaseModule): if len(definition): definition = definition[0] else: - event["stderr"].write("Try again in a couple of " - "seconds") - return + raise utils.EventError("Try again in a couple of seconds") + event["stdout"].write("Random Word: %s - Definition: %s" % ( page["word"], definition["text"])) else: diff --git a/modules/dice.py b/modules/dice.py index 26adca3e..5e8ddd33 100644 --- a/modules/dice.py +++ b/modules/dice.py @@ -15,12 +15,10 @@ class Module(ModuleManager.BaseModule): results = [] if len(roll) is not 2: - event["stderr"].write(ERROR_FORMAT) - return + raise utils.EventError(ERROR_FORMAT) if roll[0].isdigit() is False or roll[1].isdigit() is False: - event["stderr"].write(ERROR_FORMAT) - return + raise utils.EventError(ERROR_FORMAT) roll = [int(roll[0]), int(roll[1])] diff --git a/modules/eval_lua.py b/modules/eval_lua.py index 5208d00e..02f5ca2b 100644 --- a/modules/eval_lua.py +++ b/modules/eval_lua.py @@ -13,9 +13,8 @@ class Module(ModuleManager.BaseModule): method="POST", soup=True) except socket.timeout: - event["stderr"].write("%s: eval timed out" % + raise utils.EventError("%s: eval timed out" % event["user"].nickname) - return if page: textareas = page.find_all("textarea") diff --git a/modules/eval_rust.py b/modules/eval_rust.py index 0b9d03d9..bddcdda2 100644 --- a/modules/eval_rust.py +++ b/modules/eval_rust.py @@ -33,9 +33,8 @@ class Module(ModuleManager.BaseModule): page = utils.http.get_url(EVAL_URL, json_data=args, method="POST", json=True) except socket.timeout: - event["stderr"].write("%s: eval timed out" % + raise utils.EventError("%s: eval timed out" % event["user"].nickname) - return err_or_out = "stdout" if page["success"] else "stderr" event[err_or_out].write("%s: %s" % (event["user"].nickname, diff --git a/modules/ip_addresses.py b/modules/ip_addresses.py index 84d25aa7..2877e000 100644 --- a/modules/ip_addresses.py +++ b/modules/ip_addresses.py @@ -20,8 +20,8 @@ class Module(ModuleManager.BaseModule): address_info = socket.getaddrinfo(hostname, 1, 0, socket.SOCK_DGRAM) except socket.gaierror: - event["stderr"].write("Failed to find hostname") - return + raise utils.EventError("Failed to find hostname") + ips = [] for _, _, _, _, address in address_info: ips.append(address[0]) @@ -68,13 +68,10 @@ class Module(ModuleManager.BaseModule): match = REGEX_IP.search(line.message) ip = match.group(1) or match.group(2) if not ip: - event["stderr"].write("No IP provided") - return + raise utils.EventError("No IP provided") - print(ip) try: hostname, alias, ips = socket.gethostbyaddr(ip) except (socket.herror, socket.gaierror) as e: - event["stderr"].write(e.strerror) - return + raise utils.EventError(e.strerror) event["stdout"].write("(%s) %s" % (ips[0], hostname)) diff --git a/modules/modules.py b/modules/modules.py index d502ac48..7762c230 100644 --- a/modules/modules.py +++ b/modules/modules.py @@ -10,8 +10,7 @@ class Module(ModuleManager.BaseModule): """ name = event["args_split"][0].lower() if name in self.bot.modules.modules: - event["stderr"].write("Module '%s' is already loaded" % name) - return + raise utils.EventError("Module '%s' is already loaded" % name) self.bot.modules.load_module(self.bot, name) event["stdout"].write("Loaded '%s'" % name) @@ -24,8 +23,7 @@ class Module(ModuleManager.BaseModule): """ name = event["args_split"][0].lower() if not name in self.bot.modules.modules: - event["stderr"].write("Module '%s' isn't loaded" % name) - return + raise utils.EventError("Module '%s' isn't loaded" % name) self.bot.modules.unload_module(name) event["stdout"].write("Unloaded '%s'" % name) @@ -44,16 +42,13 @@ class Module(ModuleManager.BaseModule): try: self._reload(name) except ModuleManager.ModuleNotFoundException: - event["stderr"].write("Module '%s' isn't loaded" % name) - return + raise utils.EventError("Module '%s' isn't loaded" % name) except ModuleManager.ModuleWarning as warning: - event["stderr"].write("Module '%s' not loaded: %s" % ( + raise utils.EventError("Module '%s' not loaded: %s" % ( name, str(warning))) - return except Exception as e: - event["stderr"].write("Failed to reload module '%s': %s" % ( + raise utils.EventError("Failed to reload module '%s': %s" % ( name, str(e))) - return event["stdout"].write("Reloaded '%s'" % name) @utils.hook("received.command.reloadallmodules") @@ -92,8 +87,7 @@ class Module(ModuleManager.BaseModule): name = event["args_split"][0].lower() blacklist = self.bot.get_setting("module-blacklist", []) if not name in blacklist: - event["stderr"].write("Module '%s' isn't disabled" % name) - return + raise utils.EventError("Module '%s' isn't disabled" % name) blacklist.remove(name) event["stdout"].write("Module '%s' has been enabled and can now " @@ -114,8 +108,7 @@ class Module(ModuleManager.BaseModule): blacklist = self.bot.get_setting("module-blacklist", []) if name in blacklist: - event["stderr"].write("Module '%s' is already disabled" % name) - return + raise utils.EventError("Module '%s' is already disabled" % name) blacklist.append(name) self.bot.set_setting("module-blacklist", blacklist) diff --git a/modules/nr.py b/modules/nr.py index 4777d537..6cacd9fc 100644 --- a/modules/nr.py +++ b/modules/nr.py @@ -149,10 +149,11 @@ class Module(ModuleManager.BaseModule): }) if filter["errors"]: - return event["stderr"].write("Filter: " + filter["errors_summary"]) + raise utils.EventError("Filter: " + filter["errors_summary"]) if filter["inter"] and filter["type"]!="departure": - return event["stderr"].write("Filtering by intermediate stations is only supported for departures.") + raise utils.EventError("Filtering by intermediate stations is only " + "supported for departures.") nr_filterlist = client.factory.create("filterList") if filter["inter"]: nr_filterlist.crs.append(filter["inter"]) @@ -171,9 +172,9 @@ class Module(ModuleManager.BaseModule): nr_filterlist, "to", '', "PBS", filter["nonpassenger"]) except WebFault as detail: if str(detail) == "Server raised fault: 'Invalid crs code supplied'": - return event["stderr"].write("Invalid CRS code.") + raise utils.EventError("Invalid CRS code.") else: - return event["stderr"].write("An error occurred.") + raise utils.EventError("An error occurred.") nrcc_severe = len([a for a in query["nrccMessages"][0] if a["severity"] == "Major"]) if "nrccMessages" in query else 0 if event.get("external"): @@ -327,8 +328,7 @@ class Module(ModuleManager.BaseModule): }) if filter["errors"]: - event["stderr"].write("Filter: " + filter["errors_summary"]) - return + raise utils.EventError("Filter: " + filter["errors_summary"]) rid = service_id if len(service_id) <= 8: @@ -508,7 +508,8 @@ class Module(ModuleManager.BaseModule): datetime.utcnow().time().strftime("%H:%M:%S+0000")) if not query: - return event["stderr"].write("No currently running services match this identifier") + raise utils.EventError("No currently running services match this " + "identifier") services = query["serviceList"][0] if event.get("external"): @@ -527,7 +528,8 @@ class Module(ModuleManager.BaseModule): client = self.client if not event["args"].isnumeric(): - return event["stderr"].write("The delay/cancellation code must be a number") + raise utils.EventError("The delay/cancellation code must be a " + "number") reasons = {a["code"]:(a["lateReason"], a["cancReason"]) for a in client.service.GetReasonCodeList()[0]} if event["args"] in reasons: event["stdout"].write("%s: %s" % (event["args"], " / ".join(reasons[event["args"]]))) diff --git a/modules/permissions.py b/modules/permissions.py index 6301ced6..64bba006 100644 --- a/modules/permissions.py +++ b/modules/permissions.py @@ -47,14 +47,12 @@ class Module(ModuleManager.BaseModule): identity_mechanism = event["server"].get_setting("identity-mechanism", "internal") if not identity_mechanism == "internal": - event["stderr"].write("The 'identify' command isn't available " + raise utils.EventError("The 'identify' command isn't available " "on this network") - return if not event["user"].channels: - event["stderr"].write("You must share at least one channel " + raise utils.EventError("You must share at least one channel " "with me before you can identify") - return if not event["user"].identified_account_override: if len(event["args_split"]) > 1: @@ -91,9 +89,8 @@ class Module(ModuleManager.BaseModule): identity_mechanism = event["server"].get_setting("identity-mechanism", "internal") if not identity_mechanism == "internal": - event["stderr"].write("The 'identify' command isn't available " + raise utils.EventError("The 'identify' command isn't available " "on this network") - return hash, salt = self._get_hash(event["server"], event["user"].nickname) if not hash and not salt: @@ -203,8 +200,7 @@ class Module(ModuleManager.BaseModule): event["server"], event["args_split"][0]) if target.get_identified_account() == None: - event["stderr"].write("%s isn't registered" % target.nickname) - return + raise utils.EventError("%s isn't registered" % target.nickname) if permission in permissions: event["stderr"].write("%s already has permission '%s'" % ( @@ -226,8 +222,7 @@ class Module(ModuleManager.BaseModule): event["server"], event["args_split"][0]) if target.identified_account == None: - event["stderr"].write("%s isn't registered" % target.nickname) - return + raise utils.EventError("%s isn't registered" % target.nickname) if permission not in permissions: event["stderr"].write("%s doesn't have permission '%s'" % ( diff --git a/modules/soundcloud.py b/modules/soundcloud.py index fd7300c3..20285222 100644 --- a/modules/soundcloud.py +++ b/modules/soundcloud.py @@ -33,8 +33,7 @@ class Module(ModuleManager.BaseModule): last_soundcloud.message).string if not query and not url: - event["stderr"].write("no search phrase provided") - return + raise utils.EventError("no search phrase provided") has_query = not query == None get_params = {"limit": 1, "client_id": self.bot.config["soundcloud-api-key"]} diff --git a/modules/title.py b/modules/title.py index 098d28e3..96a07e57 100644 --- a/modules/title.py +++ b/modules/title.py @@ -19,12 +19,10 @@ class Module(ModuleManager.BaseModule): if url: url = re.search(REGEX_URL, url.message).group(0) if not url: - event["stderr"].write("No URL provided/found.") - return + raise utils.EventError("No URL provided/found.") soup = utils.http.get_url(url, soup=True) if not soup: - event["stderr"].write("Failed to get URL.") - return + raise utils.EventError("Failed to get URL.") title = soup.title if title: title = title.text.replace("\n", " ").replace("\r", "" diff --git a/modules/todo.py b/modules/todo.py index ac8293b8..1300cd36 100644 --- a/modules/todo.py +++ b/modules/todo.py @@ -31,9 +31,7 @@ class Module(ModuleManager.BaseModule): todo = event["user"].get_setting("todo", []) for item in todo: if item.lower() == arg_lower: - event["stderr"].write( - "That is already in your todo") - return + raise utils.EventError("That is already in your todo") todo.append(event["args"]) event["user"].set_setting("todo", todo) event["stdout"].write("Saved") @@ -70,14 +68,12 @@ class Module(ModuleManager.BaseModule): _from, to = int(_from_str)-1, int(to_str)-1 if _from < 0 or to < 0: - event["stderr"].write("Both indexes must be above 0") - return + raise utils.EventError("Both indexes must be above 0") todo = event["user"].get_setting("todo", []) if _from > len(todo) or to > len(todo): - event["stderr"].write("Both indexes must be less than the " + raise utils.EventError("Both indexes must be less than the " "size of your todo list") - return todo.insert(to, todo.pop(_from)) event["user"].set_setting("todo", todo) diff --git a/modules/translate.py b/modules/translate.py index 3a8e5b91..4173191b 100644 --- a/modules/translate.py +++ b/modules/translate.py @@ -20,8 +20,7 @@ class Module(ModuleManager.BaseModule): if phrase: phrase = phrase.message if not phrase: - event["stderr"].write("No phrase provided.") - return + raise utils.EventError("No phrase provided.") source_language = "auto" target_language = "en" diff --git a/modules/upc.py b/modules/upc.py index 7cbcbde4..f67f06b9 100644 --- a/modules/upc.py +++ b/modules/upc.py @@ -13,16 +13,14 @@ class Module(ModuleManager.BaseModule): """ arg_len = len(event["args_split"][0]) if not arg_len == 12 and not arg_len == 13: - event["stderr"].write("Invalid UPC/EAN/GTIN provided") - return + raise utils.EventError("Invalid UPC/EAN/GTIN provided") page = utils.http.get_url(UPCITEMDB_URL, get_params={"upc": event["args_split"][0]}, json=True) if page: if not len(page["items"]): - event["stderr"].write("UPC/EAN not found") - return + raise utils.EventError("UPC/EAN not found") item = page["items"][0] brand = item.get("brand", None)