From d8c11dcb254099d6689e2149faf23f7d4331fa39 Mon Sep 17 00:00:00 2001 From: Firepup Sixfifty Date: Sun, 5 Nov 2023 21:26:41 -0600 Subject: [PATCH] Implement joining channels into bot --- bot.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/bot.py b/bot.py index 347486f..3d89899 100644 --- a/bot.py +++ b/bot.py @@ -60,6 +60,48 @@ class bot: self.exit("Lost connection to the server") self.log(f"Joined {server} successfully!") + def join(self, chan: str) -> None: + log(f"Joining {chan}...", server) + chan = chan.replace(" ", "") + if "," in chan: + chans = chan.split(",") + for subchan in chans: + joinchan(subchan, origin, chanList) + return + if chan.startswith("0") or (chan == "#main" and lock): + if origin != "null": + sendmsg(f"Refusing to join channel {chan} (protected)", origin) + return + if chan in channels and lock: + if origin != "null": + sendmsg(f"I'm already in {chan}.", origin) + return + send(f"JOIN {chan}\n") + while True: + ircmsg = self.recv().decode() + if ircmsg != "": + print(bytes(ircmsg).lazy_decode()) + if ircmsg.startswith("PING "): + ping(ircmsg) + elif len(ircmsg.split("\x01")) == 3: + CTCPHandler(ircmsg, isRaw=True) + elif ircmsg.find("No such channel") != -1: + self.log(f"Joining {chan} failed", "WARN") + if origin != "null": + sendmsg(f"{chan} is an invalid channel", origin) + break + elif ircmsg.find("Cannot join channel (+i)") != -1: + self.log(f"Joining {chan} failed (Private)", "WARN") + if origin != "null": + sendmsg(f"Permission denied to channel {chan}", origin) + break + elif ircmsg.find("End of") != -1: + log(f"Joining {chan} succeeded", server) + if origin != "null": + sendmsg(f"Joined {chan}", origin) + self.channels[chan] = 0 + break + def send(self, command: str) -> int: return ircsock.send(bytes(command))