min/modules/admin.py

322 lines
8.8 KiB
Python
Raw Normal View History

2022-04-28 17:47:47 +00:00
import importlib, time, asyncio, random, sys
2021-01-31 02:05:34 +00:00
from bot import *
quitmessages = [
2022-01-27 02:26:57 +00:00
"time to die",
"you can hide, but you can not run!",
"you're next",
"bye",
"the balun has been popped.",
]
2021-01-31 02:05:34 +00:00
2020-04-05 23:27:06 +00:00
2020-04-05 23:52:31 +00:00
async def commit(self, chan, source, msg):
2022-04-28 17:47:47 +00:00
await self.send(build("QUIT", [f"{source} told me to commit {msg}!"]))
sys.exit()
2020-04-05 23:52:31 +00:00
2020-04-05 23:27:06 +00:00
2022-01-27 02:26:57 +00:00
async def quit(self, chan, source, msg):
await self.send(build("QUIT", [random.choice(quitmessages)]))
2022-04-28 17:47:47 +00:00
sys.exit()
2020-04-05 23:52:31 +00:00
2020-04-05 23:27:06 +00:00
async def reloadmods(self, chan, source, msg):
2022-04-28 17:47:47 +00:00
await self.message(chan, "reloading modules...")
2022-01-27 02:26:57 +00:00
shared.oldcmd = shared.commands
shared.commands = {}
shared.rawm = {}
shared.listeners = []
shared.help = {}
try:
for i in shared.modules:
importlib.reload(shared.modules[i])
await shared.modules[i].init(self)
2022-04-28 17:47:47 +00:00
# await self.message(chan, 'load {} sucess!'.format(i))
2022-01-27 02:26:57 +00:00
await self.message(
2022-04-28 17:47:47 +00:00
chan, "done! {} modules reloaded!".format(len(shared.modules))
2022-01-27 02:26:57 +00:00
)
except:
await self.message(
2022-04-28 17:47:47 +00:00
chan, "reload failed... {}...".format(repr(sys.exc_info()[1]))
2022-01-27 02:26:57 +00:00
)
shared.commands = shared.oldcmd
2020-04-06 20:32:51 +00:00
2021-01-31 02:05:34 +00:00
async def rawcmd(self, chan, source, msg):
2022-01-27 02:26:57 +00:00
await self.send_raw(msg)
2020-04-05 23:27:06 +00:00
2020-04-06 20:32:51 +00:00
async def joins(self, chan, source, msg):
2022-04-28 17:47:47 +00:00
await self.message(chan, "joining slowly as to not flood...")
2022-01-27 02:26:57 +00:00
for i in self.chandb.all():
await self.send(build("JOIN", [i["name"]]))
await asyncio.sleep(1)
print("joined {}".format(i["name"]))
await self.message(
chan,
2022-04-28 17:47:47 +00:00
"Sucess! i may be laggy for a bit while i sort through all these channels...",
2022-01-27 02:26:57 +00:00
)
2020-04-06 20:32:51 +00:00
2022-04-28 17:47:47 +00:00
async def aexec(self, code, chan=None, source=None, msg=None):
2020-04-24 16:13:46 +00:00
# Make an async function with the code and `exec` it
2022-04-28 17:47:47 +00:00
exec(
f"async def __ex(self, chan, source, msg): "
+ "".join(f"\n {l}" for l in code.split("\\n"))
)
2020-04-24 16:13:46 +00:00
# Get `__ex` from local variables, call it and return the result
2022-04-28 17:47:47 +00:00
return await locals()["__ex"](self, chan, source, msg)
2020-04-24 16:13:46 +00:00
2020-04-06 20:32:51 +00:00
async def ev(self, chan, source, msg):
2022-01-27 02:26:57 +00:00
msg = msg.split(" ")
try:
await self.message(
2022-04-28 17:47:47 +00:00
chan, "ok, output: {}".format(str(await aexec(self, " ".join(msg)))[:400])
2022-01-27 02:26:57 +00:00
)
except:
2022-04-28 17:47:47 +00:00
await self.message(chan, "ut oh! {}".format(repr(sys.exc_info()[1])))
2022-01-27 02:26:57 +00:00
2020-04-06 20:32:51 +00:00
2020-04-17 20:12:50 +00:00
async def send(self, c, n, m):
2022-01-27 02:26:57 +00:00
msg = m.split(" ")
await self.message(msg.pop(0), " ".join(msg))
2022-04-28 17:47:47 +00:00
await self.message(c, "sent")
2022-01-27 02:26:57 +00:00
2020-04-17 20:12:50 +00:00
async def shut(self, c, n, m):
2022-01-27 02:26:57 +00:00
shared.qtime[c] = time.time() + (60 * 10)
2022-04-28 17:47:47 +00:00
await self.message(c, "Ok, il be back in 10 minutes")
2022-01-27 02:26:57 +00:00
2020-07-12 12:21:53 +00:00
2022-05-18 03:04:29 +00:00
async def deleteword(self, c, n, m):
starttime = time.time()
deleteme = m.split()
shared.db["conver"].delete(pre=deleteme)
shared.db["prew"].delete(pre=deleteme)
shared.db["conver"].delete(pro=deleteme)
shared.db["prew"].delete(pro=deleteme)
shared.db["noun"].delete(word=deleteme)
shared.db["beg"].delete(word=deleteme)
shared.db["end"].delete(word=deleteme)
await self.message(
c, f"word(s) deleted in {round(time.time()-starttime,2)} seconds"
)
2020-07-12 12:21:53 +00:00
async def schans(self, c, n, m):
2022-01-27 02:26:57 +00:00
self.chandb.delete()
for i in self.channels:
self.chandb.insert(dict(name=i))
2022-04-28 17:47:47 +00:00
await self.message(c, "Ok")
2022-01-27 02:26:57 +00:00
async def addalias(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
2022-04-28 17:47:47 +00:00
Alias(al, m)
await self.message(c, 'added "{}" alias for "{}"'.format(al, m))
2020-10-04 16:26:20 +00:00
2022-04-28 17:47:47 +00:00
async def addcommand(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
Command(al, m)
await self.message(c, 'added "{}" alias for "{}"'.format(al, m))
2020-10-04 16:26:20 +00:00
2022-01-27 02:26:57 +00:00
async def addot(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
2021-11-12 01:16:32 +00:00
if al in shared.rawm:
2022-04-28 17:47:47 +00:00
await self.message(c, "no dont overwrite a command dummy")
2021-01-31 02:05:34 +00:00
return
2022-04-28 17:47:47 +00:00
Ot(al, m)
2021-01-31 02:05:34 +00:00
2022-04-28 17:47:47 +00:00
await self.message(c, 'added "{}" trigger for "{}"'.format(al, m))
2021-01-31 02:05:34 +00:00
2022-01-27 02:26:57 +00:00
async def addspook(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
2021-11-12 01:16:32 +00:00
if al in shared.rawm:
2022-04-28 17:47:47 +00:00
await self.message(c, "no dont overwrite a command dummy")
2021-11-12 01:16:32 +00:00
return
2022-04-28 17:47:47 +00:00
Spook(al, m)
2022-01-27 02:26:57 +00:00
2022-04-28 17:47:47 +00:00
await self.message(c, 'added "{}" trigger for "{}"'.format(al, m))
2021-11-12 01:16:32 +00:00
2021-01-31 02:05:34 +00:00
2022-01-27 02:26:57 +00:00
async def addtrigger(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
2021-11-12 01:16:32 +00:00
if al in shared.rawm:
2022-04-28 17:47:47 +00:00
await self.message(c, "no dont overwrite a command dummy")
2021-01-31 02:05:34 +00:00
return
2022-04-28 17:47:47 +00:00
Trigger(al, m)
2021-01-31 02:05:34 +00:00
2022-04-28 17:47:47 +00:00
await self.message(c, 'added "{}" trigger for "{}"'.format(al, m))
2021-01-31 02:05:34 +00:00
2022-01-27 02:26:57 +00:00
class Ot:
2022-04-28 17:47:47 +00:00
ots = {}
def __init__(self, al, ms):
2021-01-31 02:05:34 +00:00
self.ms = str(ms)
self.al = str(al)
2022-05-12 14:38:29 +00:00
self.__class__.ots[self.al] = ms
shared.rawm[self.al] = self.ot
2022-01-27 02:26:57 +00:00
async def ot(alself, self, c, n, m):
2021-01-31 02:05:34 +00:00
if alself.al in m and n != self.nickname:
2022-01-27 02:26:57 +00:00
asyncio.create_task(
self.on_privmsg(
build("PRIVMSG", [c, alself.ms.format(m)], n + "!spoof@spoof")
)
)
2021-11-12 01:16:32 +00:00
shared.rawm.pop(alself.al)
2021-01-31 02:05:34 +00:00
2022-01-27 02:26:57 +00:00
class Spook:
2022-04-28 17:47:47 +00:00
spooks = {}
def __init__(self, al, ms):
2021-11-12 01:16:32 +00:00
self.ms = str(ms)
self.al = str(al)
2022-05-12 14:38:29 +00:00
self.__class__.spooks[self.al] = ms
shared.rawm[self.al] = self.spook
2022-01-27 02:26:57 +00:00
async def spook(alself, self, c, n, m):
2021-11-12 01:16:32 +00:00
if alself.al in m and n != self.nickname:
2022-05-12 14:38:29 +00:00
self.send(build("PRIVMSG", [c, alself.ms.format(m)]))
2021-11-12 01:16:32 +00:00
shared.rawm.pop(alself.al)
2021-01-31 02:05:34 +00:00
2022-01-27 02:26:57 +00:00
class Trigger:
2022-04-28 17:47:47 +00:00
triggers = {}
def __init__(self, al, ms):
2021-01-31 02:05:34 +00:00
self.ms = str(ms)
self.al = str(al)
2022-04-28 17:47:47 +00:00
self.__class__.triggers[al] = ms
shared.rawm[al] = self.trigger
2022-01-27 02:26:57 +00:00
async def trigger(alself, self, c, n, m):
2021-01-31 02:05:34 +00:00
if alself.al in m:
2022-01-27 02:26:57 +00:00
asyncio.create_task(
self.on_privmsg(
build("PRIVMSG", [c, alself.ms.format(m)], n + "!spoof@spoof")
)
)
2021-01-31 02:05:34 +00:00
2022-04-28 17:47:47 +00:00
class Command:
commands = {}
def __init__(self, cmd, ms):
self.ms = str(ms)
self.__class__.commands[cmd] = ms
shared.commands[cmd] = self.command
async def command(alself, self, chan, source, msg):
try:
out = await aexec(self, alself.ms, chan, source, msg)
except:
await self.message(chan, "ut oh! {}".format(repr(sys.exc_info()[1])))
else:
if out is not None and len(out) > 0:
await self.message(chan, str(out))
2022-01-27 02:26:57 +00:00
class Alias:
2022-04-28 17:47:47 +00:00
aliases = {}
def __init__(self, cmd, ms):
2020-10-04 16:26:20 +00:00
self.ms = str(ms)
2022-04-28 17:47:47 +00:00
self.__class__.aliases[cmd] = ms
shared.commands[cmd] = self.alias
2020-07-12 12:21:53 +00:00
2022-01-27 02:26:57 +00:00
async def alias(alself, self, c, n, m):
asyncio.create_task(
self.on_privmsg(
build("PRIVMSG", [c, alself.ms.format(m)], n + "!spoof@spoof")
)
)
2020-07-12 12:21:53 +00:00
2020-04-05 23:27:06 +00:00
commands = {
2022-01-27 02:26:57 +00:00
"quit": quit,
"reload": reloadmods,
"commit": commit,
"raw": rawcmd,
"eval": ev,
"send": send,
"joins": joins,
"shut": shut,
2022-05-18 03:04:29 +00:00
"deleteword": deleteword,
2022-01-27 02:26:57 +00:00
"schans": schans,
"addalias": addalias,
2022-04-28 17:47:47 +00:00
"addcommand": addcommand,
2022-01-27 02:26:57 +00:00
"addtrigger": addtrigger,
"addot": addot,
"addspook": addspook,
2020-04-05 23:27:06 +00:00
}
2020-04-05 23:05:07 +00:00
2022-01-27 02:26:57 +00:00
@command("admin")
2021-01-31 02:05:34 +00:00
@is_admin
2020-04-05 23:05:07 +00:00
async def adminHandle(self, chan, source, msg):
2022-01-27 02:26:57 +00:00
msg = msg.split(" ")
2020-04-05 23:27:06 +00:00
if len(msg) < 1 or not msg[0] in commands:
2022-04-28 17:47:47 +00:00
await self.message(chan, "Invalid command")
2022-01-27 02:26:57 +00:00
return
print("[ADMIN MODULE] {} told me to {}!!!".format(source, msg[0]))
asyncio.create_task(commands[msg.pop(0)](self, chan, source, " ".join(msg)))
2020-04-05 23:05:07 +00:00
async def init(self):
2022-01-27 02:26:57 +00:00
self.chandb = shared.db["chan"]
self.admins = ["xfnw"]
return
self.help["admin"] = [
"admin - various bot owner commands (more for subcommands)",
"sub-commands of admin, for more info do help admin <command>: quit reload commit part join joins eval send",
]
self.help["admin quit"] = ["admin quit <message> - make the bot disconnect", "no"]
self.help["admin reload"] = [
"admin reload - reload the modules and configs",
"nothing to see here",
]
self.help["admin commit"] = [
"admin commit <action> - oh no (more)",
"suggested with <3 by khux",
]
self.help["admin part"] = ["admin part <channel> - leave a channel", ":o"]
self.help["admin join"] = [
"admin join <channel> - make the bot join a channel",
"...",
]
self.help["admin joins"] = [
"admin joins - join more channels",
"dont reconnect to a bunch of chans when the bots crashing etc",
]
self.help["admin eval"] = [
"admin eval <command> - absolute power corrupts absolutely",
"lmao",
]
self.help["admin send"] = [
"admin send <channel> <message> - send a message",
"lmao",
]
self.help["admin schans"] = ["admin schans - save the commands to join", ";p;"]