2020-04-05 23:05:07 +00:00
2020-07-12 12:21:53 +00:00
import importlib , time , asyncio , pydle
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 ) :
await self . quit ( ' {} told me to {} ' . format ( source , msg ) )
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 , ' [ \x03 6admin \x0f ] reloading modules... ' )
self . oldcmd = self . cmd
2020-04-05 23:27:06 +00:00
self . cmd = { }
2020-10-04 16:09:40 +00:00
self . rawm = { }
2020-04-05 23:27:06 +00:00
self . help = { }
2020-07-12 12:21:53 +00:00
try :
for i in self . modules :
importlib . reload ( self . modules [ i ] )
await self . modules [ i ] . init ( self )
#await self.message(chan, '[\x036admin\x0f] load {} sucess!'.format(i))
await self . message ( chan , ' [ \x03 6admin \x0f ] done! {} modules reloaded! ' . format ( len ( self . modules ) ) )
except :
await self . message ( chan , ' [ \x03 6admin \x0f ] reload failed... attempting to recover... ' )
self . cmd = self . oldcmd
2020-04-06 20:32:51 +00:00
2020-04-06 00:21:10 +00:00
async def part ( self , chan , source , msg ) :
2020-07-12 12:21:53 +00:00
await self . message ( chan , ' [ \x03 6admin \x0f ] bye {} ' . format ( msg ) )
2020-04-06 00:21:10 +00:00
await self . part ( msg )
2020-04-05 23:27:06 +00:00
2020-04-06 00:21:10 +00:00
async def join ( self , chan , source , msg ) :
2020-07-12 12:21:53 +00:00
self . t = time . time ( ) + 1
await self . message ( chan , ' [ \x03 6admin \x0f ] joined {} ' . format ( msg ) )
2020-04-06 00:21:10 +00:00
await self . join ( msg )
2020-04-05 23:27:06 +00:00
2020-04-06 20:32:51 +00:00
async def joins ( self , chan , source , msg ) :
2020-07-12 12:21:53 +00:00
await self . message ( chan , ' [ \x03 6admin \x0f ] I will drop commands for some seconds to ignore chanhistory... ' )
for i in self . chandb . all ( ) :
2020-10-04 16:26:20 +00:00
self . t = time . time ( ) + 5
2020-07-12 12:21:53 +00:00
try :
await self . join ( i [ ' name ' ] )
2020-10-04 16:26:20 +00:00
await asyncio . sleep ( 3 )
2020-07-12 12:21:53 +00:00
print ( ' joined {} ' . format ( i [ ' name ' ] ) )
except pydle . client . AlreadyInChannel :
print ( ' I am already in {} ' . format ( i [ ' name ' ] ) )
2020-10-04 16:26:20 +00:00
await asyncio . sleep ( 3 )
2020-07-12 12:21:53 +00:00
await self . message ( chan , ' [ \x03 6admin \x0f ] Sucess! ' )
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 , ' [ \x03 6admin \x0f ] ok, output: {} ' . format (
str ( await aexec ( self , ' ' . join ( msg ) ) ) [ : 400 ]
) )
except :
await self . message ( chan , ' [ \x03 6admin \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 , ' [ \x03 6admin \x0f ] sent ' )
2020-04-17 20:12:50 +00:00
2020-04-24 14:19:54 +00:00
async def shut ( self , c , n , m ) :
self . qtime [ c ] = time . time ( ) + ( 60 * 10 )
2020-07-12 12:21:53 +00:00
await self . message ( c , ' [ \x03 6admin \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 , ' [ \x03 6admin \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 , ' [ \x03 6admin \x0f ] no dont overwrite a command dummy ' )
return
self . cmd [ al ] = Alias ( m ) . alias
await self . message ( c , ' [ \x03 6admin \x0f ] added " {} " alias for " {} " ' . format ( al , m ) )
class Alias ( ) :
def __init__ ( self , ms ) :
self . ms = str ( ms )
async def alias ( alself , self , c , n , m ) :
asyncio . create_task ( self . parseCommand ( c , n , alself . ms . format ( m ) ) )
2020-07-12 12:21:53 +00:00
2020-04-24 14:19:54 +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 ,
' part ' : part ,
2020-04-06 20:32:51 +00:00
' join ' : join ,
' eval ' : ev ,
2020-04-17 20:12:50 +00:00
' send ' : send ,
2020-04-24 14:19:54 +00:00
' joins ' : joins ,
2020-07-12 12:21:53 +00:00
' shut ' : shut ,
2020-10-04 16:26:20 +00:00
' schans ' : schans ,
' addalias ' : addalias
2020-04-05 23:27:06 +00:00
}
2020-04-05 23:05:07 +00:00
async def adminHandle ( self , chan , source , msg ) :
2020-04-05 23:27:06 +00:00
if await self . is_admin ( source ) :
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 , ' [ \x03 6admin \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 ] ) )
2020-04-05 23:27:06 +00:00
await commands [ msg . pop ( 0 ) ] ( self , chan , source , ' ' . join ( msg ) )
else :
2020-07-12 12:21:53 +00:00
await self . message ( chan , ' [ \x03 6admin \x0f ] You do not have permission to do this ' )
2020-04-05 23:05:07 +00:00
async def init ( self ) :
2020-07-12 12:21:53 +00:00
self . chandb = self . db [ ' chan ' ]
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