From 34f8e736a155ae8198450cfc22059a66453bcfdf Mon Sep 17 00:00:00 2001 From: swee Date: Sun, 22 Dec 2024 19:14:44 -0800 Subject: [PATCH] Update sweebot.py --- sweebot.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/sweebot.py b/sweebot.py index 846abff..89fc46f 100644 --- a/sweebot.py +++ b/sweebot.py @@ -136,6 +136,11 @@ def humanbytes(B): return '{0:.2f}GB'.format(B / GB) elif TB <= B: return '{0:.2f}TB'.format(B / TB) +def rplce(text, old, new, n): + parts = text.split(old) + if len(parts) <= n: + return text # Not enough occurrences to replace + return old.join(parts[:n]) + new + old.join(parts[n:]) def replace_color_codes(text): def replacer(match): code = match.group(0) @@ -239,6 +244,7 @@ pats = ["-w-", "Meep...", "Prr!"] meows_happy = ['Meow!', 'Nyaa~', 'Mrow.', 'Prr! :3', "Mrrp?", "Mreow.", "!woeM", "3: !rrP", "~aayN", "Mew!", "Moew!"] meows_upset = ['Hiss!', "!ssiH", "Grrr..."] my_self = ["MeowNexUS", "MeowNexU5", "MeowN3xUS"] +logs = {} # {channel: [line, line]} max 10 lines happiness = 5 global times times = 0 @@ -329,6 +335,10 @@ while True: print(nick) print(username) print(perms) + cont = " ".join(command) + if not channel in logs: + logs[channel] = [{nick: nick, content: cont}] + if len(logs[channel]) > 10: logs[channel] = logs[channel][:10] #open("log-text-"+channel, "a").write(" ".join(command) + "\n") #open("log-name-"+channel, "a").write(nick + "\n") except: @@ -598,14 +608,47 @@ while True: elif command[0] == "$sed": - if len(command) > 1: - try: - #if True: - raise Exception("WorkInProgress!!!") - except: - print(traceback.format_exc()) + if len(command) == 1: + if channel in logs: + try: + if command[1].split("/")[0].lower() != "s": raise IndexError() + find = command[1].split("/")[1] + replace = command[1].split("/")[2] + user = None + repeat = False + index = 1 + try: + extraflags = command[1].split("/")[3:] + for i in extraflags: + if i.lower() == "g": + repeat = True + elif i.isnumeric(): + index = int(i) + else: + user = i.lower() + cache = None + for i in logs[channel]: + if find in i["content"] and (user == None or i["nick"] == user): + cache = i + break + if cache == None: + raise Exception("The specified text couldn't be found") + else: + if repeat: + cache["content"] = cache["content"].replace(find, replace) + else: + cache["content"] = rplce(cache["content"], find, replace, index) + cachenick = cache["nick"] + cachecontent = cache["content"] + multiline(f"[RPL] <{cachenick}> {cachecontent}", channel) + except IndexError: + irc.send_irc(channel, nick + ": [ERR] Sed expression might be incorrectly written!") + except Exception as ex: + irc.send_irc(channel, nick + ": [ERR] " + str(ex)) + else: + irc.send_irc(channel, nick + ": [!!!] No logs saved in " + channel) else: - irc.send_irc(channel, nick + ": What to correct?") + irc.send_irc(channel, nick + ": [???] This command takes only one argument.") elif command[0] == "$config":