min/modules/admin.py

196 lines
6.1 KiB
Python
Raw Normal View History

2020-04-05 23:05:07 +00:00
2021-01-31 02:05:34 +00:00
import importlib, time, asyncio, random
from bot import *
quitmessages = [
"time to die",
'you can hide, but you can not run!',
"you're next",
'bye',
'the balun has been popped.',
]
2020-04-05 23:27:06 +00:00
2020-04-05 23:52:31 +00:00
async def commit(self, chan, source, msg):
await self.quit('{} told me to commit {}'.format(source,msg))
2020-04-05 23:27:06 +00:00
async def quit(self, chan, source, msg):
2021-01-31 02:05:34 +00:00
await self.send(build("QUIT",[random.choice(quitmessages)]))
2020-04-05 23:27:06 +00:00
2020-04-05 23:52:31 +00:00
2020-04-05 23:27:06 +00:00
async def reloadmods(self, chan, source, msg):
2020-07-12 12:21:53 +00:00
await self.message(chan, '[\x036admin\x0f] reloading modules...')
2021-01-31 02:05:34 +00:00
shared.oldcmd = shared.commands
shared.commands = {}
shared.rawm = {}
shared.listeners = []
shared.help = {}
2020-07-12 12:21:53 +00:00
try:
2021-01-31 02:05:34 +00:00
for i in shared.modules:
importlib.reload(shared.modules[i])
await shared.modules[i].init(self)
2020-07-12 12:21:53 +00:00
#await self.message(chan, '[\x036admin\x0f] load {} sucess!'.format(i))
2021-01-31 02:05:34 +00:00
await self.message(chan, '[\x036admin\x0f] done! {} modules reloaded!'.format(len(shared.modules)))
2020-07-12 12:21:53 +00:00
except:
await self.message(chan, '[\x036admin\x0f] reload failed... attempting to recover...')
2021-01-31 02:05:34 +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):
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):
2021-01-31 02:05:34 +00:00
await self.message(chan, '[\x036admin\x0f] joining slowly as to not flood...')
2020-07-12 12:21:53 +00:00
for i in self.chandb.all():
2021-01-31 02:05:34 +00:00
await self.send(build("JOIN",[i['name']]))
await asyncio.sleep(1)
print('joined {}'.format(i['name']))
await self.message(chan, '[\x036admin\x0f] Sucess! i may be laggy for a bit while i sort through all these channels...')
2020-04-06 20:32:51 +00:00
2020-04-24 16:13:46 +00:00
async def aexec(self, code):
# Make an async function with the code and `exec` it
exec(
f'async def __ex(self): ' +
''.join(f'\n {l}' for l in code.split('\n'))
)
# Get `__ex` from local variables, call it and return the result
return await locals()['__ex'](self)
2020-04-06 20:32:51 +00:00
async def ev(self, chan, source, msg):
msg = msg.split(' ')
2020-07-12 12:21:53 +00:00
try:
await self.message(chan, '[\x036admin\x0f] ok, output: {}'.format(
str(await aexec(self, ' '.join(msg)))[:400]
))
except:
await self.message(chan, '[\x036admin\x0f] exception in eval!')
2020-04-06 20:32:51 +00:00
2020-04-17 20:12:50 +00:00
async def send(self, c, n, m):
msg = m.split(' ')
await self.message(msg.pop(0), ' '.join(msg))
2020-07-12 12:21:53 +00:00
await self.message(c, '[\x036admin\x0f] sent')
2020-04-17 20:12:50 +00:00
async def shut(self, c, n, m):
2021-01-31 02:05:34 +00:00
shared.qtime[c] = time.time()+(60*10)
2020-07-12 12:21:53 +00:00
await self.message(c, '[\x036admin\x0f] Ok, il be back in 10 minutes')
async def schans(self, c, n, m):
self.chandb.delete()
for i in self.channels:
self.chandb.insert(dict(name=i))
await self.message(c, '[\x036admin\x0f] Ok')
2020-10-04 16:26:20 +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
if al in self.cmd:
await self.message(c,'[\x036admin\x0f] no dont overwrite a command dummy')
return
self.cmd[al]=Alias(m).alias
await self.message(c,'[\x036admin\x0f] added "{}" alias for "{}"'.format(al,m))
2021-01-31 02:05:34 +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
if al in self.rawm:
await self.message(c,'[\x036admin\x0f] no dont overwrite a command dummy')
return
self.rawm[al]=Ot(m,al).ot
await self.message(c,'[\x036admin\x0f] added "{}" trigger for "{}"'.format(al,m))
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
if al in self.rawm:
await self.message(c,'[\x036admin\x0f] no dont overwrite a command dummy')
return
self.rawm[al]=Trigger(m,al).trigger
await self.message(c,'[\x036admin\x0f] added "{}" trigger for "{}"'.format(al,m))
class Ot():
def __init__(self, ms, al):
self.ms = str(ms)
self.al = str(al)
async def ot(alself,self,c,n,m):
if alself.al in m and n != self.nickname:
asyncio.create_task(self.on_message(c,n,alself.ms.format(m)))
self.rawm.pop(alself.al)
class Trigger():
def __init__(self, ms, al):
self.ms = str(ms)
self.al = str(al)
async def trigger(alself,self,c,n,m):
if alself.al in m:
asyncio.create_task(self.on_message(c,n,alself.ms.format(m)))
2020-10-04 16:26:20 +00:00
class Alias():
def __init__(self, ms):
self.ms = str(ms)
async def alias(alself,self,c,n,m):
2021-01-31 02:05:34 +00:00
asyncio.create_task(self.on_message(c,n,alself.ms.format(m)))
2020-07-12 12:21:53 +00:00
2020-04-05 23:27:06 +00:00
commands = {
'quit': quit,
2020-04-05 23:52:31 +00:00
'reload': reloadmods,
2020-04-06 00:21:10 +00:00
'commit': commit,
2021-01-31 02:05:34 +00:00
'raw': rawcmd,
2020-04-06 20:32:51 +00:00
'eval': ev,
2020-04-17 20:12:50 +00:00
'send': send,
'joins': joins,
2020-07-12 12:21:53 +00:00
'shut': shut,
2020-10-04 16:26:20 +00:00
'schans': schans,
2021-01-31 02:05:34 +00:00
'addalias': addalias,
'addtrigger': addtrigger,
'addot': addot,
2020-04-05 23:27:06 +00:00
}
2020-04-05 23:05:07 +00:00
2021-01-31 02:05:34 +00:00
@command('admin')
@is_admin
2020-04-05 23:05:07 +00:00
async def adminHandle(self, chan, source, msg):
2020-04-05 23:27:06 +00:00
msg = msg.split(' ')
if len(msg) < 1 or not msg[0] in commands:
2020-07-12 12:21:53 +00:00
await self.message(chan, '[\x036admin\x0f] Invalid command')
2020-04-05 23:27:06 +00:00
return
2020-04-05 23:52:31 +00:00
print('[ADMIN MODULE] {} told me to {}!!!'.format(source,msg[0]))
2021-01-31 02:05:34 +00:00
asyncio.create_task(commands[msg.pop(0)](self, chan, source, ' '.join(msg)))
2020-04-05 23:05:07 +00:00
async def init(self):
2021-01-31 02:05:34 +00:00
self.chandb = shared.db['chan']
self.admins = ['lickthecheese']
return
2020-04-05 23:05:07 +00:00
self.cmd['admin'] = adminHandle
2020-04-07 01:32:57 +00:00
2020-04-17 20:12:50 +00:00
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']
2020-04-07 01:32:57 +00:00
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']
2020-04-17 20:12:50 +00:00
self.help['admin send'] = ['admin send <channel> <message> - send a message', 'lmao']
2020-07-12 12:21:53 +00:00
self.help['admin schans'] = ['admin schans - save the commands to join',';p;']
2020-04-06 20:32:51 +00:00