MAJOR MIGRATION 3

This commit is contained in:
Firepup Sixfifty 2023-11-06 18:18:52 -06:00
parent 53b9194299
commit 33eefbbd77
Signed by: Firepup650
GPG key ID: 7C92E2ABBBFAB9BA
2 changed files with 251 additions and 2 deletions

79
bot.py
View file

@ -5,7 +5,7 @@ from logs import log
import re
from typing import NoReturn
from config import npbase, servers, __version__
import commands as cmds
class bot:
def __init__(self, server: str):
@ -61,7 +61,7 @@ class bot:
self.exit("Lost connection to the server")
self.log(f"Joined {server} successfully!")
def join(self, chan: str, origin: str) -> None:
def join(self, chan: str, origin: str, lock: bool = True) -> None:
self.log(f"Joining {chan}...")
chan = chan.replace(" ", "")
if "," in chan:
@ -164,3 +164,78 @@ class bot:
return True
self.log(f"Unknown CTCP {kind}")
return False
def mainloop(self) -> NoReturn:
self.log("Starting connection..")
self.connect()
if "pass" in servers[self.server]:
self.msg(f"IDENTIFY FireBot {servers[self.server]['pass']}", "NickServ")
sleep(0.5)
for chan in self.channels:
self.join(chan, "null", False)
while 1:
raw = self.recv()
ircmsg = raw.decode()
if ircmsg == "":
exit("Probably a netsplit")
else:
print(raw.lazy_decode(), sep="\n")
action = "Unknown"
try:
action = ircmsg.split(" ", 2)[1].strip()
except IndexError:
pass
if action == "PRIVMSG":
# Format of ":[Nick]![ident]@[host|vhost] PRIVMSG [channel] :[message]”
name = ircmsg.split("!", 1)[0][1:]
helpErr = False
if (name.startswith("saxjax") and server == "efnet") or (
name == "ReplIRC" and server == "replirc"
):
if ircmsg.find("<") != -1 and ircmsg.find(">") != -1:
Nname = ircmsg.split("<", 1)[1].split(">", 1)[0].strip()
if name == "ReplIRC":
name = Nname[4:]
else:
name = Nname
message = ircmsg.split(">", 1)[1].strip()
helpErr = True
else:
message = (
ircmsg.split("PRIVMSG", 1)[1].split(":", 1)[1].strip()
)
else:
message = ircmsg.split("PRIVMSG", 1)[1].split(":", 1)[1].strip()
if name.endswith("dsc"):
helpErr = True
chan = ircmsg.split("PRIVMSG", 1)[1].split(":", 1)[0].strip()
self.log(
f'Got "{bytes(message).lazy_decode()}" from "{name}" in "{chan}"',
)
if len(name) > nicklen:
self.log(f"Name too long ({len(name)} > {nicklen})")
continue
elif chan not in channels:
self.log(
f"Channel not in channels ({chan} not in {channels})",
"WARN"
)
else:
channels[chan] += 1
if "goat" in name.lower() and self.gmode == True:
cmds.goat(self, chan)
handled = False
for cmd in cmds.data:
if mfind(message, cmd.replace("$BOTNICK", self.nick), cmds.data[cmd]["prefix"]):
cmds.call[cmd](self, chan, name)
handled = True
break
if not handled:
for cmd in cmds.data:
for alias in cmds.data[cmd]["aliases"]:
if mfind(message, alias.replace("$BOTNICK", self.nick), cmds.data[cmd]["prefix"]):
cmds.call[cmd](self, chan, name)
handled = True
break
if handled:
break

174
commands.py Normal file
View file

@ -0,0 +1,174 @@
#TODO: Finish this
def goat(bot, chan: str, name: str) -> None:
bot.log("GOAT DETECTED")
bot.msg("Hello Goat", chan)
if mfind(
message.lower(),
["!botlist"],
False,
):
sendmsg(
f"Hi! I'm FireBot (https://git.amcforum.wiki/Firepup650/fire-ircbot)! My admins on this server are {adminnames}.",
chan,
)
if mfind(
message.lower(),
["bugs bugs bugs"],
False,
):
sendmsg(
f"\x01ACTION realizes {name} looks like a bug, and squashes {name}\x01",
chan,
)
if mfind(
message.lower(),
[f"hi {botnick.lower()}", f"hello {botnick.lower()}"],
False,
):
sendmsg(f"Hello {name}!", chan)
elif (
mfind(message, ["op me"], False) and name.lower() in adminnames
):
op(name, chan)
elif mfind(message, ["ping"]):
sendmsg(
f"{name}: pong",
chan,
)
elif mfind(message, ["uptime"]):
uptime = (
run(["uptime", "-p"], stdout=PIPE).stdout.decode().strip()
)
sendmsg(
f"Uptime: {uptime}",
chan,
)
elif mfind(message, ["amIAdmin"]):
sendmsg(
f"{name.lower()} in {adminnames} == {name.lower() in adminnames}",
chan,
)
elif mfind(message, ["help"]):
if not helpErr:
sendmsg("Command list needs rework", name)
continue
sendmsg("List of commands:", name)
sendmsg(f'Current prefix is "{prefix}"', name)
sendmsg(f"{prefix}help - Sends this help list", name)
sendmsg(
f"{prefix}quote - Sends a random firepup quote", name
)
sendmsg(
f"{prefix}(eightball,8ball,8b) [question]? - Asks the magic eightball a question",
name,
)
sendmsg(
f"(hi,hello) {botnick} - The bot says hi to you", name
)
if name.lower() in adminnames:
sendmsg(f"reboot {rebt} - Restarts the bot", name)
sendmsg(exitcode + " - Shuts down the bot", name)
sendmsg("op me - Makes the bot try to op you", name)
sendmsg(
f"{prefix}join [channel(s)] - Joins the bot to the specified channel(s)",
name,
)
else:
sendmsg("Sorry, I can't send help to bridged users.", chan)
elif name.lower() in adminnames and mfind(
message, ["goat.mode.activate"]
):
log("GOAT DETECTION ACTIVATED", server)
gmode = True
elif name.lower() in adminnames and mfind(
message, ["goat.mode.deactivate"]
):
log("GOAT DETECTION DEACTIVATED", server)
gmode = False
elif mfind(message, ["quote"]):
r.seed()
mm = open("mastermessages.txt", "r")
q = mm.readlines()
sel = decode_escapes(
str(r.sample(q, 1))
.strip("[]'")
.replace("\\n", "")
.strip('"')
)
sendmsg(sel, chan)
mm.close()
elif mfind(message, ["join "]) and name.lower() in adminnames:
newchan = message.split(" ")[1].strip()
channels = joinchan(newchan, chan, channels)
elif mfind(message, ["eightball", "8ball", "8b"]):
if message.endswith("?"):
eb = open("eightball.txt", "r")
q = eb.readlines()
sel = (
str(r.sample(q, 1))
.strip("[]'")
.replace("\\n", "")
.strip('"')
)
sendmsg(f"The magic eightball says: {sel}", chan)
eb.close()
else:
sendmsg("Please pose a Yes or No question.", chan)
elif (
mfind(message, ["debug", "dbg"]) and name.lower() in adminnames
):
sendmsg(f"[DEBUG] NICKLEN={nicklen}", chan)
sendmsg(f"[DEBUG] ADMINS={adminnames}", chan)
sendmsg(f"[DEBUG] CHANNELS={channels}", chan)
elif (
mfind(message, ["raw ", "cmd "]) and name.lower() in adminnames
):
sendraw(message.split(" ", 1)[1])
elif (
mfind(message, [f"reboot {rebt}", f"reboot {gblrebt}"], False)
or mfind(message, ["restart", "reboot"])
) and name.lower() in adminnames:
send("QUIT :Rebooting\n")
exit("Reboot")
elif sucheck(message):
if name.lower() in adminnames:
sendmsg(
"Error - system failure, contact system operator", chan
)
elif "bot" in name.lower():
log("lol, no.", server)
else:
sendmsg("Access Denied", chan)
elif np.search(message) and name in npallowed:
x02 = "\x02"
sendmsg(
f"f.sp {message.split(':')[1].split('(')[0].strip(f' {x02}')}",
chan,
)
elif len(message.split("\x01")) == 3:
if not CTCPHandler(message, name):
CTCP = message.split("\x01")[1]
if CTCP == "ACTION ducks":
sendmsg("\x01ACTION gets hit by a duck\x01", chan)
elif CTCP.startswith("ACTION ducks"):
sendmsg(
f"\x01ACTION gets hit by {CTCP.split(' ', 2)[2]}\x01",
chan,
)
if chan in channels and channels[chan] >= interval:
r.seed()
channels[chan] = 0
mm = open("mastermessages.txt", "r")
q = mm.readlines()
sel = decode_escapes(
str(r.sample(q, 1))
.strip("[]'")
.replace("\\n", "")
.strip('"')
)
sendmsg(f"[QUOTE] {sel}", chan)
mm.close()
data = {"!botlist": {"prefix": False, "aliases": []}, "bugs bugs bugs": {"prefix": False, "aliases": []}, "hi $BOTNICK": {"prefix": False, "aliases": ["hello $BOTNICK"]}, }
call = {"!botlist": botlist, "bugs bugs bugs": bugs, "hi $BOTNICK": hi, }