FireBot/commands.py

462 lines
15 KiB
Python
Raw Normal View History

2023-11-20 23:52:18 +00:00
#!/usr/bin/python3
2023-11-07 05:19:40 +00:00
from subprocess import run, PIPE
import config as conf
2023-11-07 13:53:14 +00:00
import random as r
2023-11-13 21:42:46 +00:00
from typing import Any, Callable
2023-11-20 23:52:18 +00:00
import bare, re, checks
2023-11-07 05:19:59 +00:00
2023-11-08 02:30:35 +00:00
def fpmp(bot: bare.bot, chan: str, name: str, message: str) -> None:
bot.msg("Firepup's master playlist", chan)
bot.msg("https://open.spotify.com/playlist/4ctNy3O0rOwhhXIKyLvUZM", chan)
2024-04-07 05:38:49 +00:00
def version(bot: bare.bot, chan: str, name: str, message: str) -> None:
bot.msg("Version: " + bot.__version__, chan)
2024-04-07 05:38:49 +00:00
2023-11-13 21:42:46 +00:00
def goat(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 00:18:52 +00:00
bot.log("GOAT DETECTED")
bot.msg("Hello Goat", chan)
2023-11-07 15:05:23 +00:00
bot.gmode = False
2023-11-07 13:06:55 +00:00
2023-11-13 21:42:46 +00:00
def botlist(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 13:06:55 +00:00
bot.msg(
f"Hi! I'm FireBot (https://git.h.hackclub.app/Firepup650/FireBot)! {'My admins on this server are' + str(bot.adminnames) + '.' if bot.adminnames else ''}", # pyright: ignore [reportOperatorIssue]
2023-11-07 13:06:55 +00:00
chan,
)
2023-11-07 05:19:59 +00:00
2023-11-13 21:42:46 +00:00
def bugs(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 05:19:59 +00:00
bot.msg(
f"\x01ACTION realizes {name} looks like a bug, and squashes {name}\x01",
chan,
)
2023-11-13 21:42:46 +00:00
def hi(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 05:19:59 +00:00
bot.msg(f"Hello {name}!", chan)
2023-11-13 21:42:46 +00:00
def op(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 05:22:36 +00:00
bot.op(name, chan)
2023-11-07 05:19:59 +00:00
2023-11-13 21:42:46 +00:00
def ping(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 05:19:59 +00:00
bot.msg(
f"{name}: pong",
chan,
)
2023-11-13 21:42:46 +00:00
def uptime(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 05:19:59 +00:00
uptime = run(["uptime", "-p"], stdout=PIPE).stdout.decode().strip()
bot.msg(
f"Uptime: {uptime}",
chan,
)
2023-11-13 21:42:46 +00:00
def isAdmin(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 05:19:59 +00:00
bot.msg(
2024-02-15 03:17:08 +00:00
f"{'Yes' if checks.admin(bot, name) else 'No'}",
2023-11-07 05:19:59 +00:00
chan,
)
2023-11-13 21:42:46 +00:00
def help(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-09 03:20:50 +00:00
helpErr = False
category = None
if len(message.split(" ")) > 1:
category = message.split(" ")[1]
2023-11-20 23:52:18 +00:00
if bot.current == "bridge":
2023-11-09 03:20:50 +00:00
helpErr = True
2023-11-07 05:19:59 +00:00
if not helpErr:
bot.msg("Command list needs rework", chan)
2023-11-07 05:19:59 +00:00
return
match category:
case None:
bot.msg("Categories of commands: gen, dbg, adm, fun, msc", chan)
case "gen":
bot.msg("Commands in the General category: ", chan)
case "dbg":
bot.msg("Commands in the [DEBUG] category: ", chan)
case "adm":
bot.msg("Commands in the Admin category: ", chan)
case "fun":
bot.msg("Commands in the Fun category: ", chan)
case "msc":
bot.msg("Commands in the Misc. category: ", chan)
case _:
bot.msg("Unknown commands category.", chan)
2023-11-07 05:19:59 +00:00
else:
bot.msg("Sorry, I can't send help to bridged users.", chan)
2023-11-13 21:42:46 +00:00
def goatOn(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 05:19:59 +00:00
bot.log("GOAT DETECTION ACTIVATED")
bot.gmode = True
2023-11-13 21:42:46 +00:00
def goatOff(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 05:19:59 +00:00
bot.log("GOAT DETECTION DEACTIVATED")
bot.gmode = False
2023-11-13 21:42:46 +00:00
def quote(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-21 02:11:44 +00:00
qfilter = ""
query = ""
2023-11-18 02:24:36 +00:00
if " " in message:
query = message.split(" ", 1)[1]
2024-04-07 05:38:49 +00:00
qfilter = query.replace(
" ", "\s"
) # pyright: ignore [reportInvalidStringEscapeSequence]
2023-11-07 05:19:59 +00:00
r.seed()
2023-11-21 02:11:44 +00:00
with open("mastermessages.txt", "r") as mm:
2024-05-11 01:51:14 +00:00
q = []
try:
q = list(filter(lambda x: re.search(qfilter, x), mm.readlines()))
except re.error:
2024-05-11 01:51:14 +00:00
q = ["Sorry, your query is invalid regex. Please try again."]
2023-11-21 02:11:44 +00:00
if q == []:
q = [f'No results for "{query}" ']
sel = conf.decode_escapes(
2024-02-20 22:02:26 +00:00
r.sample(q, 1)[0].replace("\\n", "").replace("\n", "")
2023-11-21 02:11:44 +00:00
)
bot.msg(sel, chan)
2023-11-07 05:19:59 +00:00
2023-11-13 21:42:46 +00:00
def join(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-08 02:30:35 +00:00
newchan = message.split(" ", 1)[1].strip()
bot.join(newchan, chan)
2023-11-07 05:19:59 +00:00
2023-11-13 21:42:46 +00:00
def eball(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 05:19:59 +00:00
if message.endswith("?"):
eb = open("eightball.txt", "r")
q = eb.readlines()
sel = str(r.sample(q, 1)).strip("[]'").replace("\\n", "").strip('"')
bot.msg(f"The magic eightball says: {sel}", chan)
eb.close()
else:
bot.msg("Please pose a Yes or No question.", chan)
2023-11-13 21:42:46 +00:00
def debug(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-08 03:23:59 +00:00
dbg_out = {
"VERSION": bot.__version__,
"NICKLEN": bot.nicklen,
"NICK": bot.nick,
2024-04-07 05:38:49 +00:00
"ADMINS": str(bot.adminnames) + " (Does not include hostname checks)",
2023-11-08 03:23:59 +00:00
"CHANNELS": bot.channels,
}
2023-11-08 02:49:44 +00:00
bot.msg(f"[DEBUG] {dbg_out}", chan)
2023-11-07 05:19:59 +00:00
def debugInternal(bot: bare.bot, chan: str, name: str, message: str) -> None:
things = dir(bot)
try:
2024-05-24 17:06:41 +00:00
thing = message.split(" ", 1)[1]
except IndexError:
bot.msg("You can't just ask me to lookup nothing.", chan)
return
if thing in things:
bot.msg(f"self.{thing} = {getattr(bot, thing)}", chan)
else:
2024-05-24 17:06:41 +00:00
bot.msg(f'I have nothing called "{thing}"', chan)
def debugEval(bot: bare.bot, chan: str, name: str, message: str) -> None:
try:
2024-05-24 17:06:41 +00:00
bot.msg(str(eval(message.split(" ", 1)[1])), chan)
except Exception as E:
bot.msg(f"Exception: {E}", chan)
2023-11-13 21:42:46 +00:00
def raw(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-07 15:20:56 +00:00
bot.sendraw(message.split(" ", 1)[1])
2023-11-07 05:19:59 +00:00
2023-11-13 21:42:46 +00:00
def reboot(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-08 02:30:35 +00:00
bot.send("QUIT :Rebooting\n")
bot.exit("Reboot")
2023-11-07 05:19:59 +00:00
2023-11-13 21:42:46 +00:00
def sudo(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-20 23:52:18 +00:00
if checks.admin(bot, name):
2023-11-07 05:19:59 +00:00
bot.msg("Error - system failure, contact system operator", chan)
elif "bot" in name.lower():
bot.log("lol, no.")
else:
bot.msg("Access Denied", chan)
2023-11-13 21:42:46 +00:00
def nowplaying(bot: bare.bot, chan: str, name: str, message: str) -> None:
2023-11-23 18:27:21 +00:00
if name in bot.npallowed and not bot.current == "bridge":
2023-11-07 00:18:52 +00:00
x02 = "\x02"
2023-11-07 05:19:40 +00:00
bot.msg(
2023-11-13 21:42:46 +00:00
f"f.sp {message.split(x02)[1]}",
2023-11-07 00:18:52 +00:00
chan,
)
2023-11-18 03:24:10 +00:00
2024-04-07 04:45:38 +00:00
def fmpull(bot: bare.bot, chan: str, name: str, message: str) -> None:
song = None
2024-04-07 04:45:38 +00:00
try:
song = bot.lastfmLink.get_user("Firepup650").get_now_playing()
2024-04-07 04:45:38 +00:00
except:
2024-04-07 05:38:49 +00:00
bot.msg(
"Sorry, the music api isn't cooperating, please try again in a minute", chan
)
if song:
bot.msg(
2024-04-12 02:45:40 +00:00
"Firepup is currently listening to: " + song.__str__(),
chan,
)
else:
bot.msg("Firepup currently has his music stopped :/", chan)
2024-04-07 05:38:49 +00:00
2024-04-07 04:45:38 +00:00
2023-11-18 02:27:06 +00:00
def whoami(bot: bare.bot, chan: str, name: str, message: str) -> None:
2024-04-07 05:38:49 +00:00
bot.msg(
2024-04-29 21:31:05 +00:00
f"I think you are {name}{' (bridge)' if bot.current == 'bridge' else '@'+bot.tmpHost}",
2024-04-07 05:38:49 +00:00
chan,
)
2023-11-18 02:27:06 +00:00
2023-11-07 05:19:59 +00:00
def markov(bot: bare.bot, chan: str, name: str, message: str) -> None:
word = None
if " " in message:
word = message.split()[1]
proposed = bot.markov.generate_text(word)
if proposed == word:
2024-05-11 01:51:14 +00:00
proposed = f'Chain failed. (Firepup has never been recorded saying "{word}")'
bot.msg(proposed, chan)
2024-05-11 01:51:14 +00:00
def setStatus(bot: bare.bot, chan: str, name: str, message: str) -> None:
2024-05-24 17:06:41 +00:00
user, stat, reas = ("", 0, "")
try:
2024-05-24 17:06:41 +00:00
if message.split(" ")[1] == "help":
bot.msg(
"Assuming you want help with status codes. 1 is Available, 2 is Busy, 3 is Unavailable, anything else is Unknown.",
chan,
)
return
2024-05-24 17:06:41 +00:00
message = message.split(" ", 1)[1]
user = message.split(" ")[0].lower()
stat = int(message.split(" ")[1])
reas = message.split(" ", 2)[2]
except IndexError:
2024-05-24 17:06:41 +00:00
bot.msg(
f"Insufficent information to set a status. Only got {len(message.split(' ')) - (1 if '.sS' in message else 0)}/3 expected args.",
chan,
)
return
except ValueError:
bot.msg("Status parameter must be an int.", chan)
return
match stat:
case 1:
2024-05-24 17:06:32 +00:00
stat = "Available"
case 2:
stat = "Busy"
case 3:
2024-05-24 04:47:40 +00:00
stat = "Unavailable"
case _:
stat = "Unknown"
if user in ["me", "my", "I"]:
2024-05-24 17:06:41 +00:00
user = "firepup"
bot.statuses[user] = {"status": stat, "reason": reas}
bot.msg(f"Status set for '{user}'. Raw data: {bot.statuses[user]}", chan)
def getStatus(bot: bare.bot, chan: str, name: str, message: str) -> None:
2024-05-24 17:06:41 +00:00
user = ""
try:
2024-05-24 17:06:41 +00:00
user = message.split(" ")[1]
except IndexError:
2024-05-24 17:06:41 +00:00
user = "firepup"
if bot.statuses.get(user) is None:
bot.msg("You've gotta provide a nick I actually recognize.", chan)
return
2024-05-24 17:06:41 +00:00
bot.msg(
f"{user}'s status: {'Unknown' if not bot.statuses[user].get('status') else bot.statuses[user]['status']} - {'Reason unset' if not bot.statuses[user].get('reason') else bot.statuses[user]['reason']}",
chan,
)
def check(bot: bare.bot, chan: str, name: str, message: str) -> None:
try:
2024-05-24 17:06:41 +00:00
msg = message.split(" ", 1)[1]
nick = msg.split("!")[0]
host = msg.split("@", 1)[1]
2024-06-10 09:20:45 +00:00
cache = host in bot.dns
2024-05-23 18:12:46 +00:00
dnsbl, raws = conf.dnsblHandler(bot, nick, host, chan)
2024-06-10 09:20:54 +00:00
bot.msg(
f"Blacklist check: {'(Cached) ' if cache else ''}{dnsbl if dnsbl else 'Safe.'} ({raws})",
chan,
)
except IndexError:
try:
2024-06-10 09:20:45 +00:00
host = message.split(" ", 1)[1]
cache = host in bot.dns
2024-06-10 09:20:54 +00:00
dnsbl, raws = conf.dnsblHandler(
bot, "thisusernameshouldbetoolongtoeveractuallybeinuse", host, chan
)
bot.msg(
f"Blacklist check: {'(Cached) ' if cache else ''}{dnsbl if dnsbl else 'Safe.'} ({raws})",
chan,
)
except Exception as E:
bot.msg("Blacklist Lookup Failed. Error recorded to bot logs.", chan)
bot.log(str(E), "FATAL")
except Exception as E:
bot.msg("Blacklist lookup failed. Error recorded to bot logs.", chan)
bot.log(str(E), "FATAL")
def slap(bot: bare.bot, chan: str, name: str, message: str) -> None:
msg = message.split(" ")
if len(msg) > 1:
msg = " ".join(msg[1:]).strip()
if msg == bot.nick or not msg:
msg = name
else:
msg = name
bot.msg(
f"\x01ACTION slaps {msg} around a bit with {r.choice(['a firewall', 'a fireball', 'a large trout', 'a computer', 'an rpi4', 'an rpi5', 'firepi', name])}\x01",
chan,
)
def morning(bot: bare.bot, chan: str, name: str, message: str) -> None:
msg = message.split(" ")
addresse = " ".join(msg[2:]).strip().lower()
postfix = ""
if addresse and addresse[-1] in ["!", ".", "?"]:
postfix = addresse[-1]
addresse = addresse[:-1]
elif message[-1] in ["!", ".", "?"]:
postfix = addresse[-1]
addresse = addresse[:-1]
if addresse not in ["everyone", "people", bot.nick.lower(), ""]:
return
bot.msg(f"Good morning {name}{postfix}", chan)
def night(bot: bare.bot, chan: str, name: str, message: str) -> None:
msg = message.split(" ")
addresse = " ".join(msg[2:]).strip().lower()
postfix = ""
if addresse and addresse[-1] in ["!", ".", "?"]:
postfix = addresse[-1]
addresse = addresse[:-1]
elif message[-1] in ["!", ".", "?"]:
postfix = addresse[-1]
addresse = addresse[:-1]
if addresse not in ["everyone", "people", bot.nick.lower(), ""]:
return
bot.msg(f"Good night {name}{postfix}", chan)
def afternoon(bot: bare.bot, chan: str, name: str, message: str) -> None:
msg = message.split(" ")
addresse = " ".join(msg[2:]).strip().lower()
postfix = ""
if addresse and addresse[-1] in ["!", ".", "?"]:
postfix = addresse[-1]
addresse = addresse[:-1]
elif message[-1] in ["!", ".", "?"]:
postfix = addresse[-1]
addresse = addresse[:-1]
if addresse not in ["everyone", "people", bot.nick.lower(), ""]:
return
bot.msg(f"Good afternoon {name}{postfix}", chan)
2023-11-09 03:20:50 +00:00
data: dict[str, dict[str, Any]] = {
2023-11-07 05:19:59 +00:00
"!botlist": {"prefix": False, "aliases": []},
"bugs bugs bugs": {"prefix": False, "aliases": []},
"hi $BOTNICK": {"prefix": False, "aliases": ["hello $BOTNICK"]},
2023-11-08 02:30:35 +00:00
# [npbase, su]
2024-04-12 02:45:40 +00:00
"restart": {
"prefix": True,
"aliases": ["reboot", "stop", "hardreload", "hr"],
"check": checks.admin,
},
"uptime": {"prefix": True, "aliases": ["u"]},
2024-05-23 18:12:46 +00:00
"raw": {"prefix": True, "aliases": ["cmd "], "check": checks.admin},
2024-04-07 05:38:49 +00:00
"debug": {"prefix": True, "aliases": ["dbg", "d"], "check": checks.admin},
2024-05-24 17:06:41 +00:00
"debugInternal": {
"prefix": True,
"aliases": ["dbgInt", "dI"],
"check": checks.admin,
},
"debugEval": {"prefix": True, "aliases": ["dbgEval", "dE"], "check": checks.admin},
2023-11-07 13:06:55 +00:00
"8ball": {"prefix": True, "aliases": ["eightball", "8b"]},
2024-05-23 18:12:46 +00:00
"join": {"prefix": True, "aliases": ["j"], "check": checks.admin},
2023-11-07 13:53:14 +00:00
"quote": {"prefix": True, "aliases": ["q"]},
"goat.mode.activate": {"prefix": True, "aliases": ["g.m.a"], "check": checks.admin},
2024-04-07 05:38:49 +00:00
"goat.mode.deactivate": {
"prefix": True,
"aliases": ["g.m.d"],
"check": checks.admin,
},
"help": {"prefix": True, "aliases": ["?", "list", "h"]},
2023-11-15 04:47:51 +00:00
"amiadmin": {"prefix": True, "aliases": []},
2023-11-07 13:06:55 +00:00
"ping": {"prefix": True, "aliases": []},
2023-11-20 23:52:18 +00:00
"op me": {"prefix": False, "aliases": [], "check": checks.admin},
2023-11-18 02:27:06 +00:00
"whoami": {"prefix": True, "aliases": []},
"fpmp": {"prefix": True, "aliases": []},
2024-04-07 05:38:49 +00:00
"version": {"prefix": True, "aliases": ["ver", "v"]},
2024-04-07 04:45:38 +00:00
"np": {"prefix": True, "aliases": []},
2024-05-11 01:51:14 +00:00
"markov": {"prefix": True, "aliases": ["m"]},
"setStatus": {"prefix": True, "aliases": ["sS"], "check": checks.admin},
"getStatus": {"prefix": True, "aliases": ["gS"]},
"check": {"prefix": True, "aliases": [], "check": checks.admin},
"slap": {"prefix": True, "aliases": ["s"]},
"good morning": {
"prefix": False,
"aliases": ["g'morning", "morning", "mornin'", "good mornin'"],
},
"good night": {"prefix": False, "aliases": ["g'night", "night"]},
"good afternoon": {"prefix": False, "aliases": ["g'afternoon", "afternoon"]},
2023-11-07 05:19:59 +00:00
}
2023-11-20 23:52:18 +00:00
regexes: list[str] = [conf.npbase, conf.su]
2023-11-13 21:42:46 +00:00
call: dict[str, Callable[[bare.bot, str, str, str], None]] = {
2023-11-07 05:19:59 +00:00
"!botlist": botlist,
"bugs bugs bugs": bugs,
"hi $BOTNICK": hi,
conf.npbase: nowplaying,
conf.su: sudo,
2023-11-07 13:06:55 +00:00
"restart": reboot,
"uptime": uptime,
2024-05-23 18:12:46 +00:00
"raw": raw,
2023-11-07 13:06:55 +00:00
"debug": debug,
"debugInternal": debugInternal,
"debugEval": debugEval,
2023-11-07 13:06:55 +00:00
"8ball": eball,
2024-05-23 18:12:46 +00:00
"join": join,
2023-11-07 13:06:55 +00:00
"quote": quote,
"goat.mode.activate": goatOn,
"goat.mode.decativate": goatOff,
"help": help,
2023-11-15 04:47:51 +00:00
"amiadmin": isAdmin,
2023-11-07 13:06:55 +00:00
"ping": ping,
"op me": op,
2023-11-18 03:24:10 +00:00
"whoami": whoami,
"fpmp": fpmp,
"version": version,
2024-04-07 04:45:38 +00:00
"np": fmpull,
"markov": markov,
"setStatus": setStatus,
"getStatus": getStatus,
"check": check,
"slap": slap,
"good morning": morning,
"good night": night,
"good afternoon": afternoon,
2023-11-07 05:19:59 +00:00
}