From cdc5d58c5b15b25b7be6f369a7e15bc1b4b3326a Mon Sep 17 00:00:00 2001 From: Siina Mashek Date: Tue, 3 May 2022 21:37:48 +0300 Subject: [PATCH] Breaking out placeholder stuff to its own def --- core/owncast.py | 63 +++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/core/owncast.py b/core/owncast.py index 8f9b746..2e997f0 100644 --- a/core/owncast.py +++ b/core/owncast.py @@ -70,6 +70,41 @@ def get_quote(num): return quote.get(num) # NOQA should work even if linter complains +def process_placeholders(sender, param, text): + # Move this dictionary to a configuration file or something + # This is so bad, it runs everything every command :pikajoy: + logging.debug("Processing placeholders") + placeholders = { + "sender": sender, + "botowner": config["DEFAULT"]["BotOwner"], + "target": param, + "random": str(random.randrange(1, 100, 1)), + "commands": ", ".join(list(commands.keys())), + "aliases": ", ".join(list(aliases.keys())), + "bot_version": __version__, + "quote": get_quote(param), + "quote_parameters": "Not implemented yet", + } + + for placeholder in placeholders: + findme = "{%s}" % placeholder + if findme in text: + logging.debug("Found {}".format(findme)) + if placeholders[placeholder] == "": + logging.debug("Placeholder empty") + text = "%s required" % placeholder.upper() + try: + logging.debug( + "Replacing {} with {}".format( + findme, placeholders[placeholder])) + text = text.replace( + findme, placeholders[placeholder]) + except TypeError: + text = "Target not specified." + + return text + + def process_chat(data): sender = data["user"]["displayName"] text = data["body"] @@ -88,29 +123,7 @@ def process_chat(data): except IndexError: first_parameter = None - # Move this dictionary to a configuration file or something - # This is so bad, it runs everything every command :pikajoy: - placeholders = { - "sender": sender, - "botowner": config["DEFAULT"]["BotOwner"], - "target": first_parameter, - "random": str(random.randrange(1, 100, 1)), - "commands": ", ".join(list(commands.keys())), - "aliases": ", ".join(list(aliases.keys())), - "bot_version": __version__, - "quote": get_quote(first_parameter), - "quote_parameters": "Not implemented yet", - } - - for placeholder in placeholders: - findme = "{%s}" % placeholder - if findme in command_reply: - if placeholders[placeholder] == "": - command_reply = "%s required" % placeholder.upper() - try: - command_reply = command_reply.replace( - findme, placeholders[placeholder]) - except TypeError: - command_reply = "Target not specified." - + command_reply = process_placeholders( + sender, first_parameter, command_reply) + logging.debug("Reply: {}".format(command_reply)) return command_reply